Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java.Lang.NullPointerException

 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am having a
Exception in thread "main" java.lang.NullPointerException
at Pizza.order(Pizza.java:17)
at Main.main(Main.java:6)
whenever I ran my small program. Below is three of the files from my simple code:



The exception happens on pizzaType_.add(type); from Pizza.java. Can anyone tell me what is wrong with the code and why I am getting an exception.

Thanks,
John
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your pizzaType_ and toppings_ are still null. In Try to read it carefully and tell me why it is so

Hint: look at the problem in your constructor.
 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea I see the problem.

Thanks,
John
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since lots of people who are less experienced won't know why, please tell us
 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is where I made a mistake on. On Pizza.java, I had declared two class attribute called toppings_ and pizzatype_ of type ArrayList. When I created the constructor, I allocated/created memory (new) for those two attributes however, I made a mistake where I did declare toppings_ and pizzatype_ again inside the constructor. What happen next is when I used those two inside the class i.e. pizza:: order(), it ran out of scope thats why the compiler was telling me that the pointer where null.
The compiler was not able to recognize the declaration twice(Always blame someone else for my mistake lmao).

Hmmm. I wonder what would have happen if I move the class attribute below the contructor?

Please correct me if I am wrong, I do not know much.

John


 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. If you declare the identifiers again inside the constructor (or a method) you end up with a local variable. Most programming languages permit local variables with the same name as "non-local" variables. Whether that is a good thing, I am never quite sure.

By moving the class attribute, do you mean what difference would it make if you declared the fields last in the class? Or something similar? Try it. I think you will find it makes no difference at all.

A similar problem people sometimes get is with set methods like thisTry it. See what happens. See how you can correct the error.
 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,

I tried switching the above and it turned out that the same NULL error was created.

John
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Miska,

You need to pick up the knowledge of local variable, instance variable (non-static) , class variable (static).
Also, clarify the concept on when does this three different variable initialize and destroyed (out of scope),
so it will help you understand and build the strong foundation.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the things about field declarations is that their location is of no grammatical significance. If you got an exception when the declaration preceded the constructor, you will suffer the same exception if the declaration follows the constructor.
I didn't see anything about local variables in the Java Tutorials, but you can try the Java Language Specification (JLS) (here is the index: would you believe try under "L"). Warning: the JLS is by no means easy reading.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic