Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

i get null expression when trying to call from another class

 
Sid James
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
m
 
James Ward
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change this line inside the constructor of Dice class (the class level variable mRandom will remain null otherwise):



to this:


 
Sid James
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

It still says NullPointerException when i call AttackValue (after i changed the Random mRandom to just mRandom)
Please let me know if you can see something else that could be the problem.

 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same mistake with Dice in the constructor of class Hero...you declare a local variable of type Dice there, but probably want to assign it to your instance variable.
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One problem I see is about the missing code tags.

Next, you instead of refereing to instance variables in the constructors, create new automatic ones.
 
Sid James
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D Ogranos - do you mean mSides?

like Dice mDice = new Dice(mSides)? (in the constructor of Hero)

or should i change the type of Dice?
 
Sid James
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himanshu - like this?

(constructor)
Dice mDice = new Dice()

Im sorry that may be the way i already written. How do you mean?
 
James Ward
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the constructor of Hero class.

Change this line:

to this:


 
Sid James
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think ive fixed it!!! Thank you so much. I made the method create a new Dice. Allthough now it is returning the same numbers, but that isnt a big problem. Thank you!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic