File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException

 
Jinny Morris
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 1367
18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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.
 
Jinny Morris
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! I will "dredge" - & come back w/ something (I hope!)
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the exact error message (i.e. copy and paste the error message)
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jinny Morris
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fear not.
When Marilyn's done with you, you'll have a nice solid foundation.

It sounds like you're already doing quite well.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic