java null pointer exception JMenuBar

stackoverflow321

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?

Eran
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).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

null pointer exception in java .

From Dev

Java Multithreading null pointer exception

From Dev

Null pointer exception in java ArrayList

From Dev

Android - Java null pointer exception

From Dev

JAVA: Null Pointer Exception error

From Dev

Java Multithreading null pointer exception

From Dev

Weird Java Null Pointer Exception

From Dev

Null Pointer Exception (Java) [EDIT!]

From Dev

Java null pointer exception after checking if null

From Dev

Java Generic Object Null Pointer Check Throwing Null Pointer Exception

From Dev

Java - Object Array - Null Pointer Exception

From Dev

Java replaceAll throwing Null Pointer Exception

From Dev

How to avoid null pointer exception in java

From Dev

Null Pointer Exception from Java UDP

From Dev

Java ServerSocket throws null pointer exception

From Dev

java Null pointer exception after onClick

From Dev

java Null Pointer Exception passing object

From Dev

Java - Null Pointer Exception passing records to a database

From Dev

Trouble Resolving a Null Pointer Exception in Java

From Dev

java ternary conditions strange null pointer exception

From Dev

java 10 compilaton Null Pointer Exception

From Dev

null pointer exception in java when coding

From Dev

java error message null pointer exception

From Dev

Null Pointer Exception Error in java arrays

From Dev

java: JOptionPane.showInputDialog null pointer exception

From Dev

Null pointer Exception at ContactFragment.java

From Dev

Java - Null Pointer Exception on String array

From Dev

deep cloning in java showing null pointer exception

From Dev

java arraylist add null pointer exception