-1

I am trying to develop a small pizza ordering app. I have 3 classes so far and the purpose of them right now is to display 3 menus with the menu items accessible.

Previously I had only one class and that worked just fine however issues started occurring when I split the program to use several classes.

Main.java: http://pastebin.com/bZJ7Pgdt

MakeFileMenu.java: http://pastebin.com/wv2Smm6E

MakeFrame.java: http://pastebin.com/J7DV294P

The errors I am getting are:

Exception in thread "main" java.lang.NullPointerException
at main.MakeFileMenu.MakeMenu(MakeFileMenu.java:24)
at main.Main.start(Main.java:26)
at main.Main.main(Main.java:16)

Furthermore in the Makeframe.java file lines 37, 42, 49 and 57 should be uncommented. I commented them out as they were giving me errors so I thought I will at least try to get the menus to appear.

Anything you guys see out of the ordinary? I double checked the code I can't seem to figure out what could be wrong with it.

Thanks for your help!

JackSparrow123
  • 1,360
  • 2
  • 18
  • 23
  • The exception is telling you that the problem arose on line 24 of MakeFileMenu, in the MakeMenu() method, when it was called by Main.start(). Examine that line and determine which variable could possibly be null. Run in a debugger, or add printouts, to confirm that guess. Fix it. – keshlam Feb 22 '14 at 01:48
  • possible duplicate of [What is a Null Pointer Exception?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception) – Brian Roach Feb 22 '14 at 02:07
  • What is your Class strategy? You seem to be splitting classes based on some "function" you want each class to perform, rather than creating classes based on an object methodology. You might want to consider a GeneralFrame class (that provides methods to create a frame with characteristics (size, visibility); optional menubar; ability to add elements to the menubar; mechanism to add items to a menubar element; and a Order Class that handles your other work. – ErstwhileIII Feb 22 '14 at 02:53
  • It's probably better I just start from afresh with separate classes to begin with rather than splitting a single one and it becoming a convoluted mess... Zibi and Joao will get a thumbs up for trying to help me. – JackSparrow123 Feb 22 '14 at 03:04

1 Answers1

1

The problem is in line:

frame.frame.setJMenuBar(menuBar);

this is because variable frame in frame (frame.frame) is not initialized. You have to initialize it before using.

You either forgot to call makeFrame on frame in MakeFileMenu or you should rename makeFrame to MakeFrame to make it a constructor.

zibi
  • 3,183
  • 3
  • 27
  • 47
  • frame is initialized in MakeFrame.java. Furthermore in MakeFileMenu.java I have used the constructor to create a MakeFrame object called 'frame'. I am simply calling the 'frame' method using the object called 'frame.' – JackSparrow123 Feb 22 '14 at 01:53
  • You are wrong, you initialized the first frame, but frame.frame is not. The method initializing it in MakeFrame is never called in scope of MakeFileMenu. You have makeFrame called in Main, but those are 2 different things. Also, you have not used constructor in MakeFileMenu. I hope this helps! – zibi Feb 22 '14 at 01:56
  • Thanks but to be honest I am still kind of confused. Isn't "MakeFrame frame = new MakeFrame();" using the constructor? – JackSparrow123 Feb 22 '14 at 02:08
  • 1
    Yes, you are constructing MakeFrame, but what makes the frame inside it to be instantiated is the method makeFrame. If you wanted it to be a constructor, you should write it as public MakeFrame() – renke Feb 22 '14 at 02:17
  • Now I understand your confusion. In class MakeFrame you have a method makeFrame, which is not a constructor, you have to rename it to: MakeFrame - it's case sensitive. – zibi Feb 22 '14 at 02:17