aspose file tools*
The moose likes Beginning Java and the fly likes Java.Lang.NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java.Lang.NullPointerException" Watch "Java.Lang.NullPointerException" New topic
Author

Java.Lang.NullPointerException

Gary Ba
Ranch Hand

Joined: Oct 23, 2009
Posts: 161

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


star chaser..
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

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.


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Gary Ba
Ranch Hand

Joined: Oct 23, 2009
Posts: 161

Yea I see the problem.

Thanks,
John
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39380
    
  28
Since lots of people who are less experienced won't know why, please tell us
Gary Ba
Ranch Hand

Joined: Oct 23, 2009
Posts: 161

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

Joined: Oct 13, 2005
Posts: 39380
    
  28
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

Joined: Oct 23, 2009
Posts: 161

Hi Campbell,

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

John
Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 213
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.


SCJP 6, SCWCD 5, SCBCD 5
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39380
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java.Lang.NullPointerException