I'm using documentation on sun.com to try and learn Java. I was trying a simple Notepad type program but cannot get the menus to display. I'm receiving no errors and cannot figure out what I've done wrong and/or left out.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;



public class NotepadInJava //implements ActionListener
{
    //preparing container
    JFrame notepadFrame;
    JTextArea theTextArea;
    JScrollPane scrollPane = new JScrollPane(theTextArea);


    //File menu items
    JMenu file;
    JMenuItem open;
    JMenuItem save;
    JMenuItem exit;

    //Edit menu items
    JMenu edit;
    JMenuItem copy;
    JMenuItem cut;
    JMenuItem selectAll;
    JMenuItem paste;

    public static void main(String[] args)
    {
        NotepadInJava app = new NotepadInJava();
    }

    public NotepadInJava()
    {
        notepadFrame = new JFrame();
        notepadFrame.addWindowListener(null);
        notepadFrame.setSize(600,600);
        notepadFrame.setLocation(40,20);
        notepadFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        notepadFrame.setVisible(true);
        buildMenus();
    }

    private void buildMenus()
    {
      //create menu bar
        JMenuBar MenuBar = new JMenuBar();
      //notepadFrame.setJMenuBar(FileMenuBar);

      //build File menu
        file = new JMenu("File");
        MenuBar.add(file);
      //add Open to File menu
        open = new JMenuItem("Open");
        open.addActionListener(null);
        MenuBar.add(open);
      //add Save to File category
        save = new JMenuItem("Save");
        save.addActionListener(null);
        MenuBar.add(save);
      //add Exit to File category
        exit = new JMenuItem("Exit");
        exit.addActionListener(null);
        MenuBar.add(exit);

      //build Edit menu
        edit = new JMenu("Edit");
        MenuBar.add(edit);
      //add Copy to Edit category
        copy = new JMenuItem("Copy");
        copy.addActionListener(null);
        MenuBar.add(copy);
      //add Cut to Edit category
        cut = new JMenuItem("Cut");
        cut.addActionListener(null);
        MenuBar.add(cut);
      //add Select All to Edit category
        selectAll = new JMenuItem("Select All");
        selectAll.addActionListener(null);
        MenuBar.add(selectAll);
      //add Paste to Edit category
        paste = new JMenuItem("Paste");
        paste.addActionListener(null);
        MenuBar.add(paste);
    }
}

Peter_budo you beat me to it! :) Kahaj, what you could do is :

declare and initialize MenuBar as a class field, instead of a local variable in method buildMenus() :

JMenuBar MenuBar;

you can then initialize this variable in the class constructor:

MenuBar = new JMenuBar()

then add MenuBar to notepadFrame before you call method buildMenus():

notepadFrame.add(MenuBar);

Hope that helps.
P.S. you might want to look at layout managers for the menu items, I recomend the tutorials here:
http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

commented: Small medicine for your injury ;) +23

I'm sure once I've dealt with menus more, they'll be easier. But right now, they're really a pain in the butt.

Here are the changes I made:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;



public class NotepadInJava //implements ActionListener
{
    //preparing container
    JFrame notepadFrame;
    JMenuBar MenuBar;
    JTextArea theTextArea;
    JScrollPane scrollPane = new JScrollPane(theTextArea);


    //File menu items
    JMenu file;
    JMenuItem open;
    JMenuItem save;
    JMenuItem exit;

    //Edit menu items
    JMenu edit;
    JMenuItem copy;
    JMenuItem cut;
    JMenuItem selectAll;
    JMenuItem paste;

    public static void main(String[] args)
    {
        NotepadInJava app = new NotepadInJava();
    }

    public NotepadInJava()
    {
        notepadFrame = new JFrame();
        notepadFrame.addWindowListener(null);
        notepadFrame.setSize(600,600);
        notepadFrame.setLocation(40,20);
        notepadFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        notepadFrame.setVisible(true);
        buildMenus();
    }

    private void buildMenus()
    {
      //create menu bar
        JMenuBar MenuBar = new JMenuBar();
        notepadFrame.setJMenuBar(MenuBar);

      //build File menu
        file = new JMenu("File");
        MenuBar.add(file);
      //add Open to File menu
        open = new JMenuItem("Open");
        open.addActionListener(null);
        MenuBar.add(open);
      //add Save to File category
        save = new JMenuItem("Save");
        save.addActionListener(null);
        MenuBar.add(save);
      //add Exit to File category
        exit = new JMenuItem("Exit");
        exit.addActionListener(null);
        MenuBar.add(exit);

      //build Edit menu
        edit = new JMenu("Edit");
        MenuBar.add(edit);
      //add Copy to Edit category
        copy = new JMenuItem("Copy");
        copy.addActionListener(null);
        MenuBar.add(copy);
      //add Cut to Edit category
        cut = new JMenuItem("Cut");
        cut.addActionListener(null);
        MenuBar.add(cut);
      //add Select All to Edit category
        selectAll = new JMenuItem("Select All");
        selectAll.addActionListener(null);
        MenuBar.add(selectAll);
      //add Paste to Edit category
        paste = new JMenuItem("Paste");
        paste.addActionListener(null);
        MenuBar.add(paste);
    }
}

You need to add:

notepadFrame.add(MenuBar);

before you set notepadFrame.setVisible(true);. This adds the MenuBar to the Frame.

Here's the code I have so far:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;



public class NotepadInJava //implements ActionListener
{
    //preparing container
    JFrame notepadFrame;
    JMenuBar MenuBar;
    JTextArea theTextArea;
    JScrollPane scrollPane = new JScrollPane(theTextArea);


    //File menu items
    JMenu file;
    JMenuItem open;
    JMenuItem save;
    JMenuItem exit;

    //Edit menu items
    JMenu edit;
    JMenuItem copy;
    JMenuItem cut;
    JMenuItem selectAll;
    JMenuItem paste;

    public static void main(String[] args)
    {
        NotepadInJava app = new NotepadInJava();
    }

    public NotepadInJava()
    {
        buildMenus();
        notepadFrame = new JFrame();
        notepadFrame.add(MenuBar);
        notepadFrame.addWindowListener(null);
        notepadFrame.setSize(600,600);
        notepadFrame.setLocation(40,20);
        notepadFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        notepadFrame.setVisible(true);      
    }

    private void buildMenus()
    {
      //create menu bar
        JMenuBar MenuBar = new JMenuBar();
        notepadFrame.setJMenuBar(MenuBar);

      //build File menu
        file = new JMenu("File");
        MenuBar.add(file);
      //add Open to File menu
        open = new JMenuItem("Open");
        open.addActionListener(null);
        MenuBar.add(open);
      //add Save to File category
        save = new JMenuItem("Save");
        save.addActionListener(null);
        MenuBar.add(save);
      //add Exit to File category
        exit = new JMenuItem("Exit");
        exit.addActionListener(null);
        MenuBar.add(exit);

      //build Edit menu
        edit = new JMenu("Edit");
        MenuBar.add(edit);
      //add Copy to Edit category
        copy = new JMenuItem("Copy");
        copy.addActionListener(null);
        MenuBar.add(copy);
      //add Cut to Edit category
        cut = new JMenuItem("Cut");
        cut.addActionListener(null);
        MenuBar.add(cut);
      //add Select All to Edit category
        selectAll = new JMenuItem("Select All");
        selectAll.addActionListener(null);
        MenuBar.add(selectAll);
      //add Paste to Edit category
        paste = new JMenuItem("Paste");
        paste.addActionListener(null);
        MenuBar.add(paste);
    }
}

...and here's what the Debugger is yelling at me. To be totally honest, I'm not grasping what it is not liking about it.

run:
Exception in thread "main" java.lang.NullPointerException
        at NotepadInJava.buildMenus(NotepadInJava.java:50)
        at NotepadInJava.<init>(NotepadInJava.java:36)
        at NotepadInJava.main(NotepadInJava.java:31)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)

Ok the problem there is that you have not initialized the variable MenuBar before you call the method buildMenus() on it, therefore you get a null pointer exception. You need to initialize MenuBar before you can perform any action on it. Change your constructor from this :

public NotepadInJava()
    {
        buildMenus();
        notepadFrame = new JFrame();
        notepadFrame.add(MenuBar);
        notepadFrame.addWindowListener(null);
        notepadFrame.setSize(600,600);
        notepadFrame.setLocation(40,20);
        notepadFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        notepadFrame.setVisible(true);      
    }

to this :

public NotepadInJava()
    {
        MenuBar = new JMenuBar();
        notepadFrame = new JFrame();
        notepadFrame.add(MenuBar);
        buildMenus();
        notepadFrame.addWindowListener(null);
        notepadFrame.setSize(600,600);
        notepadFrame.setLocation(40,20);
        notepadFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        notepadFrame.setVisible(true);
    }

This will solve your null pointer exception. Look here for more information regarding null pointer exceptions :

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/NullPointerException.html


EDIT: Oh and remove this line :

JMenuBar MenuBar = new JMenuBar();

from method buildMenus() as you don't want to initialize MenuBar twice!

commented: very helpful +5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.