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/...Exception.html
EDIT: Oh and remove this line :
JMenuBar MenuBar = new JMenuBar();
from method buildMenus() as you don't want to initialize MenuBar twice!
Last edited by majestic0110; Sep 22nd, 2009 at 6:02 pm. Reason: see edit note
Reputation Points: 256
Solved Threads: 72
Nearly a Posting Virtuoso
Offline 1,306 posts
since Oct 2007