java null pointer exception JMenuBar [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
Im stuck today with a java error on an eclipse 3.8 platform.
I try to generate a simple MenuBar on a JFrame. The JMenuBar contains the JMenu("Help") and the JMenu contains a JMenuItem("Quit"). The class extends a JFrame. With the function (this.)setJMenuBar(MenuBar); I try to set my MenuBar to my JFrame, which should work fine as long I just have one MenuBar.
GUI-class code:
import javax.swing.*;
import java.awt.*;
public class Taschenrechner extends JFrame{
//Instanzvariablen im Zusamenhang mit dem Menu
private JMenuBar MenuBar;
private JMenu MenuHelp;
private JMenuItem MenuHelpQuit;
public Taschenrechner() {
super();
//Flaeche festlegen
this.setSize(300,300);
//Menukomponenten definieren
MenuHelp= new JMenu("Help");
MenuHelpQuit=new JMenuItem("Quit");
//Menukomponenten zusammensetzen
MenuHelp.add(MenuHelpQuit);
MenuBar.add(MenuHelp);
this.setJMenuBar(MenuBar);
}
}
my main-class code (declare objekt and setVisible):
public class Taschenrechnerstart {
public static void main(String[] args) {
Taschenrechner taschenrechner1 = new Taschenrechner();
taschenrechner1.setVisible(true);
}
}
Now on starting the code I get a NullPointerException-error in the GUI-class on line:
MenuBar.add(MenuHelp);
and a NullPointerException-error in the main-class on the line:
Taschenrechner taschenrechner1 = new Taschenrechner();
Does someone have an idea why my code isn't working?

MenuBar.add(MenuHelp);
You are accessing the MenuBar variable without initializing it.
You are missing a
MenuBar = new JMenuBar (..);
BTW, your code would be more readable if you use Java naming conventions (variables should start with a lower case letter).

Related

JTabbedPane Malfunctioning

I have a problem with my code. I am trying to get into user interfaces in Java, and have started using JOptionPane. At the moment, I'm experimenting with JTabbedPane, but I have a problem with it. My problem is the following: when I try to run the code, so that a panel with one tab appears reading 'You are using the program "CarDealership".', nothing happens. I don't know what's the problem, but my terminal reads the following when I compile and run my program...:
Terminal...: TerminalOutput
My code...: ProgramCode
I'm coding in Visual Studio Code, and, as you can tell by reviewing the code, I am from México. I translated the variables used in the program in a comment. Thank you for reading!
Code in text...:
/*
The name of the program translates to 'ProgCarDealership'.
Variables used...:
Example: 'Type/NameInSpanish/NameInEnglish'
- JTabbedPane/Pestañas/Tabs
- JLabel/Contenido/Content
- JPanel/Pestaña1/Tab1
*/
import javax.swing.*;
public class ProgLoteDeCarros extends JFrame
{
public VentanaPrincipal()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState(MAXIMIZED_BOTH);
setVisible(true);
setTitle("Programa Lote de Carros");
JTabbedPane Pestañas = new JTabbedPane();
JPanel Pestaña1 = new JPanel();
JLabel Contenido = new JLabel ("Estas usando el programa 'Lote de Carros'.");
Pestaña1.add(Contenido);
Pestañas.addTab("Información", Pestaña1);
getContentPane().add(Pestañas);
}
public static void main (String [] Yoshi)
{
ProgLoteDeCarros VentanaPrincipal = new ProgLoteDeCarros();
}

contentPane cannot be null when using IDEA's content designer

So I'm trying out IntelliJ IDEA's content designer for a simple GUI, and I followed all guides on using it, but it when run from IDEA (not compiled into a JAR yet) it comes back with the following error:
Exception in thread "main" java.awt.IllegalComponentStateException: contentPane cannot be set to null.
at java.desktop/javax.swing.JRootPane.setContentPane(JRootPane.java:594)
at java.desktop/javax.swing.JFrame.setContentPane(JFrame.java:679)
at com.AdamT.MergeSortGui.<init>(MergeSortGui.java:18)
at com.AdamT.MergeSortGui.main(MergeSortGui.java:13)
My code:
package com.AdamT;
import javax.swing.*;
class MergeSortGui extends JFrame {
private JPanel panel;
private JTextField inputList;
private JButton submitButton;
private JLabel inputLabel;
private JLabel outputLabel;
public static void main(String[] args) {
new MergeSortGui();
}
MergeSortGui() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(panel);
this.setTitle("MergeSort GUI");
this.add(inputList);
this.add(submitButton);
this.add(inputLabel);
this.add(outputLabel);
this.pack();
this.setVisible(true);
}
}
I would add my form, too, but I can't because then this woudld be mostly code... I uploaded all of my project here. From what I have seen from the error, it is because it's assuming that the content of each variable is null, even though it's taken care of by my .form file. Any ideas?
(I know this is a dupe of this, but that has no working answer so I really am at completely lost)

JCheckBoxMenuItem cannot be added to Menu

The src code of JCheckBoxMenuItem has the following class head:
public class CheckboxMenuItem extends MenuItem implements ItemSelectable, Accessible
So when a class extends another one it means that it should also inherit its type. Or not?
My problem is that I can't add the JCheckBoxMenuItem to a JMenu (it needs a MenuItem to be added).
The following code does not work for me:
private void initMenu()
{
menuBar = new JMenuBar();
setJMenuBar(menuBar);
mnFile = new JMenu("File");
menuBar.add(mnFile);
mnAudio = new JMenu("Audio");
menuBar.add(mnAudio);
mnitmQuit = new JMenuItem("Quit");
mnFile.add(mnitmQuit);
rmnitmNoice = new CheckboxMenuItem("Noice");
// Eclipse gives error Message below *
mnAudio.add(rmnitmNoice);
rmnitmNuke = new JRadioButtonMenuItem("Nuke");
// Same here
mnAudio.add(rmnitmNuke);
}
The method add(JMenuItem) in the type JMenu is not applicable for the
arguments (CheckboxMenuItem)
I m quite sure that I used this one before and had no problems with it. But since I started to use Marven I get strange behaviour sometimes (other example: #Override does not work for Methods, that implement interfaces anymore)
Anyone with the same issue or the solution?
As VGR explained in his comment: The mistake was trying to add a CheckboxMenuItem instead of a JCheckBoxMenuItem.

Class with JFrame [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I'm beginner of java and I study about JFrame now but I have an issue. I made a constructor with parameters of JPanel but when I invoke it with arguments, an error is happened. Could you help me to find any solutions??
import javax.swing.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.*;
public class JFlexiblePanel extends JFrame{
private Color col1;
private Color col2;
private Font font1;
private String str;
private JPanel panel1;
private JLabel label1;
public JFlexiblePanel(Color col1, Color col2, Font font1, String str) {
this.col1 = col1;
this.col2 = col2;
this.font1 = font1;
this.str = str;
panel1.setBackground(this.col1);
panel1.setForeground(this.col2);
label1.setFont(this.font1);
label1.setText(this.str);
panel1.add(label1);
}
}
In different class to invoke this constructor
JFlexiblePanel p1 = new JFlexiblePanel(Color.BLUE, Color.RED, new Font("Arial",Font.BOLD,12), "America");
Your class name implies that it's a JPanel class.
So it can't extends a JFrame, but have to extend JPanel. I think you are misunderstanding how they both work.
To be short : JFrame is the top Level of a "window" and is composed by JPanel.
Moreover, your Jpanel1 and JLabel1 are never instanciated, then the error might be here :
panel1.setBackground(this.col1);
panel1.setForeground(this.col2);
panel1.add(label1);
But if you want more help you have to give more information like the type of the error. Also consider this tutorial : link to tutorial

JDIalog throws NullPointerException when accessed from JFrame [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
My JDialog throws a NullPointerException when I access it through Jframe but works fine when I run the JDialog class itself.
I have a jMenuItem called "modify" in my jFrame that accesses jDialog.
private void modifyActionPerformed(java.awt.event.ActionEvent evt){
Modify mod = new Modify(this,true); //"Modify" is my jDialog class name.
mod.setVisible(true);
}
Modify throws a NullPointerException when I query the database when I access the dialog from jframe but I can query the database successfully from Modify when I run the class itself.
please Check your code somthing else in your code .
check Modify Class.
This Simple example shows JDialog opening.
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
class DiagonalLineDemo extends JFrame implements ActionListener {
public DiagonalLineDemo()
{
setVisible(true);
setSize(100,100);
JMenuBar s=new JMenuBar();
JMenu m=new JMenu("Open ");
JMenuItem s1=new JMenuItem("Dialog");
m.add(s1);
s.add(m);
setJMenuBar(s);
s1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
JDialog j=new JDialog(this,true);
j.setVisible(true);
}
public static void main(String args[]) {
DiagonalLineDemo f=new DiagonalLineDemo();
}
}
Open Frame click Menu open and MenuItem Dialog.
After Click Dialog The DialogBox is shows.
Fixed it. Turns out I just needed to set my jDialog database connection to non-static so I can initialize the class from my main interface. Thanks anyway.

Categories