This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance and Implementations  RSS feed

 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, where am I going wrong here? The only ouput I get is:

Tigger chases Steve
Tigger chases Bambi
Exception in thread "main" java.lang.NullPointerException
at Human.eatPrey(Human.java:25)
at Human.playDead(Human.java:50)
at Human.isCaught(Human.java:63)
at AnimalKingdomDriver.main(AnimalKingdomDriver.java:20)





 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the null pointer exception. I will fix that later, just added so it would compile
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ian Burres wrote:I get the null pointer exception. I will fix that later, just added so it would compile


Wait, so what's your actual question? Is it about the NPE you're getting? If so, and if the line in question is this line:

then it's because p is null. We can tell that because p being null is the only thing that could possibly cause NPE on that line.

Or when you say you "get" the NPE, do you mean that you understand why you're getting it, and your real question is something else. If so, what exactly is that question?
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm saying I know why im getting the error, just not how to fix it. I want to call that method eatPrey when the human is caught by a tiger.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, then, like I said, if I got the right line, then we know that p must be null, so somewhere you're calling eatPrey() and passing a null argument. If you can't figure out where/how that's happening by reviewing your code, then add a bunch of println() statements so you can see what's happening at each step of the way--which code paths are being followed and what various relevant values are.

Or, alternatively, if it makes logical sense to pass null to eatPrey(), then you need to change eatPrey()'s logic so that it doesn't just blindly dereference its argument. If it's valid to eat a null thing, then eatPrey() needs to handle that appropriately for whatever it's semantics are.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem as I see it is that your setter method is misbehaving. It doesn't set a value. A setter method should return VOID and should change the value of the field it claims to set. Check out what your setCaught method does -- or rather, what it DOESN'T do. It doesn't change the state of the object.

In general you have burdened your methods with way too much responsibility. runAway, playDead -- why are they trying to be so clever and accomplish so much? Your Human class is tripping all over itself trying to get so much done in a single method. It seems to me, too, that there is a fair amount of redundancy in your class and your driver. Decide where activities belong, put them there and keep them there. You should break out all this functionality into discrete, well-defined tasks. Use pencil and paper to do this. Use diagrams to help conceptualize your design. Keep it simple!
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so if under playDead the human fails to pass the test, the eatPrey method should be called to reflect the fact he/she has been eaten. I do not want a null value, but eatPrey() does not compile either. I want it to execute when the human has failed the test to play dead. Do I need to reference the Tiger class to make it work or something?
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to step back from your code and think about the problem in its own terms. Don't worry about whether something compiles. Map out events, interactions and responsibilities. Human fails to play dead successfully. This does not end well for the human, but is it the Human's responsibility to make sure he gets eaten? The less you think in terms of Java, the more progress you'll make toward solving your problem.
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are so right. It's not his responsibility.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ian Burres wrote:I do not want a null value,


Then why are you calling on line 50?

but eatPrey() does not compile either.


Of course not, because the method requires something to be passed to it. I don't think anybody suggested you just don't pass any argument. As already suggested, before you start worrying about syntax and null and exceptions, back up and ask yourself, "What is eatPrey()'s job? What task is this step in my process expected to perform, and what information to I need to give it for it to be able to do that task?"
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!