Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException

 
Jeff Ciaccio
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!

 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool - thanks!!
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic