• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException  RSS feed

 
Christopher Goad
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How could I fix this? I understand the problem but I just don't know what to do about it...

The error is-


Exception in thread "main" java.lang.NullPointerException
at TestCandidate7.getTotal(TestCandidate7.java:14)
at TestCandidate7.printResults(TestCandidate7.java:28)
at TestCandidate7.main(TestCandidate7.java:197)


Here is my code-



 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at line 14. Is there a reference to the left of a dot which might be null? Hint: try yes as the answer.

Why on earth have you got a candidates array in the main method? That looks like non‑object‑oriented programming to me.
 
Christopher Goad
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean?

And yes I know... this is just what the directions stated to do.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Goad wrote:What do you mean?


This:

Exception in thread "main" java.lang.NullPointerException
at TestCandidate7.getTotal(TestCandidate7.java:14)
at TestCandidate7.printResults(TestCandidate7.java:28)
at TestCandidate7.main(TestCandidate7.java:197)


tells you that at line 28 of TestCandidate7.java, in its printResults() method, you called TestCandidate.getTotal(), and that method, at line 14 of TestCandidate7.java, tried to dereference a null pointer.

So we look at that line, and, assuming the line numbers in your post match the line numbers in your actual file, that would be this:


Looking at that line, we see that you dereference 2 pointers there: candidates and candidates[ i ]. So one of those to must be null.

A little deductive reasoning tells us that it can't be candidates, otherwise we'd have gotten a NullPointerException on for(int i = 0; i <candidates.length; i++)

So we know that candidates[ i ] is null. The way to fix it is to make sure it's not null before you call that method. Or else to test it for null before you try to call getVotes(). Which approach is the correct one depends on your specific requirements. In particular, if that array is allowed to be only partially populated, vs. if it's expected to be full of Candidate references.>
 
Christopher Goad
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply! That makes sense..but how would you do it? Could you demonstrate?
 
Christopher Goad
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just used if(candidates[i] == null){} to seperate all the nulls works perfect now! Thanks!
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Often, using an == null test is not a good idea. Not if you can avoid it. What you are doing is introducing nulls into that array in other methods. If you can avoid getting nulls into the array anywhere, you can avoid the == null test.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!