aspose file tools*
The moose likes Cattle Drive and the fly likes NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "NullPointerException" Watch "NullPointerException" New topic
Author

NullPointerException

Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Ladies and Gentlemen -

I am incredibly confused at the following:

I created a class; within the class is a static array; some of the references are intentionally null. That is, for some i and j, array[i][j] is null; it does not point to anything. I then create an object of the class and call a method which tests the array elements using a statement like:

if(array[i][j] == null) {do stuff}

This all works just as I had hoped.

However, if I then try to run the program without first creating an object, by making all methods static without changing the array definition at all, the very same statement gives me a NullPointerException runtime error on the null test.

Clearly there's something fundamental that I don't understand. I haven't posted the actual code here because it's playing around with assignment 4, but I would be happy to post it if you want. Could someone shed any light on this?

Thanks much!
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1364
    
  17
Hi Jinny,

No assignment code allowed, but if you can drudge up some pseudo-code, that will help! Or just some code that says nothing about the assignment but displays your problem.

Katrina
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
[B] ... gives me a NullPointerException[/B]

I'm not clear on how testing if something is null gives you a NPE. Usually a NPE is thrown if you try to call a method on an object that is null.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Thanks! I will "dredge" - & come back w/ something (I hope!)
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
What is the exact error message (i.e. copy and paste the error message)
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Marilyn de Queiroz:
[B] ... gives me a NullPointerException[/B]

I'm not clear on how testing if something is null gives you a NPE. Usually a NPE is thrown if you try to call a method on an object that is null.


Arrays are objects.
If the array itself is null, it will throw a NullPointerException when you attempt to dereference one of its elements.

IE: line 5 in the following example:


If there is a chance that the array itself could be null, then you would want to test for both:


[ September 25, 2007: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Thank you all!!!

I found my problem - which is to say, my kid found my problem for me. I am using an initialization block. I didn't know that I needed to "staticify" the block - so I didn't. Since I wasn't instantiating an object, the block just REFUSED to run (obnoxious thing!) so my whole array was probably null.

So Ben's response hit the nail on the head. Except, of course, I would have still been in the dark over why the array was null when it wasn't supposed to be!

Now that I've made my initialization block static, everything works as expected!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Jinny Morris:

... Except, of course, I would have still been in the dark over why the array was null when it wasn't supposed to be!


Do you understand it now?
If not, in this thread Ernest Freidman-Hill does a nice job of explaining what happens to initialization blocks and static initialization blocks when a Java class is compiled.
[ September 25, 2007: Message edited by: Ben Souther ]
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Yes, I think so - when the initialization block is part of a constructor (ie when it's not labelled "static"), then it won't run until an object is instantiated. If no object is instantiated, then the initialization block never happens either.

However, thanks for the reference! I'm learning Java because I'm quite clueless about OOP (obviously, I'm afraid!) so I can learn from anything written on parts of the subject.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Fear not.
When Marilyn's done with you, you'll have a nice solid foundation.

It sounds like you're already doing quite well.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: NullPointerException