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

NullPointerException

Jeff Ciaccio
Greenhorn

Joined: Nov 26, 2008
Posts: 25
when I run Ttest1.java I get Exception in thread "main" java.lang.NullPointerException
at cpu.newAddr(cpu.java:63)
at Ttest1.main(cpu.java:32)

Can anybody help me with this?

Thanks!!



Jeff Ciaccio, Java novice <br />Physics and AP Physics Teacher<br />Sprayberry High School <br />Marietta, GA
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
(Line 32 doesn't call newAddr(), so I think your call stack doesn't match the lines in your listing...)

newAddr() has the statement "randNum.nextInt(32)", but you never initialize randNum. You should probably change line 48 to "public Random randNum = new Random();".


Jeff Ciaccio
Greenhorn

Joined: Nov 26, 2008
Posts: 25
Cool - thanks!!
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
I'll just expand on what Max has said. Probably the most common cause of a null pointer exception is that you are referencing an object variable that has been declared but not linked to an actual object. You can do the link by instantiating the object using a new operator, as was done here, or you can link the variable to an existing object of type Random by using an assignment statement.

Note that if you did not have the line Random randNum, you would have received a different error, a compile error "Symbol not found" when you tried to compile addr = randNum.nextInt(32);
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
Yeah, non-primitive variables are just references. They are able to reference an object, but don't reference anything unless your code puts something in them. So you either initialize them as you define them, like I did, or you initialize them in the constructor.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Max Rahder wrote:Yeah, non-primitive variables are just references. They are able to reference an object, but don't reference anything unless your code puts something in them. So you either initialize them as you define them, like I did, or you initialize them in the constructor.


ok, we are talking the same thing, but I think here your use here of "reference an object" has a slightly different connotation than mine. By reference an object I meant using an instance method or instance variable of an object that has not been initialized, which can't be done. Perhaps yours is a more accepted use of the terminology, I don't know.

And I agree with you that in the context of this example, there are two different locations where you might initialize the randNum object. I was speaking more generally about the ways it can be done.

may as well be precise.

regards.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException