• Post Reply Bookmark Topic Watch Topic
  • New Topic

Null pointer exception  RSS feed

 
Christopher Laurenzano
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I've been struggling with this for a bit -- still my little horse racing program.

I have these lines of code which testes if the name the user has entered for the horses in my virtual race is the proper length:



It all works fine -- it compiles and runs -- as long as you enter something.
But if one accidentally hits enter without entering anything, then this is what happens:

Enter the number of horses in the race: 2
There are 2 horses in the race.
Enter the name of the horse in post position 1: Swale
Name of horse number 1: Swale
Enter the name of the horse in post position 2:
Exception in thread "main" java.lang.NullPointerException
at thoroughbred.setName(thoroughbred.java:14)
at Thoroughbredtestdrive.main(Thoroughbredtestdrive.java:21)

Now I don't know how to fix this -- what I would like to happen is for the loop to run even if nothing is entered
I want the the loop to ask again for the name of the horse, but I don't know how to test for that in the if statement.
Any advice? Maybe I'm just not at the point in my java education where I've learned that yet.
I hope I've kept the amount of lines to a minimum that is still understandable for the question I'm asking.
 
Barry Burd
Author
Ranch Hand
Posts: 133
10
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you using to input horsename? Scanner? JOptionPane? or something else?
 
Gopinath anumalasetty
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris,

Just write a if condition to verify the "horsename" is null or not before that while condition you mentioned in your post like below.


code snippet:


if(horsename!=null){

while(horsename.length()==0 || horsename.length()>18)){

---
---
--

}
}
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gopinath anumalasetty wrote:Hi Chris,

Just write a if condition to verify the "horsename" is null or not before that while condition you mentioned in your post like below.

if he wants to ask for the name to be re-entered if horsename is null then he needs to include that test in the while loop

But it does seem strange that whatever input method he is using returns null for an empty string so I think it is best to wait until Chris answers Barry's question before we make too many suggestions.
There may be a way to stop horsename being null in the first place which is always a better solution.
 
Gopinath anumalasetty
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Small code changes


if(horesname==null){
horsename = (entry.getUserInput("Please reenter: "));
}else{
while(horsename.length()==0 || horsename.length()>18)){
---
---
horsename = (entry.getUserInput("Please reenter: "));
}

}

 
Barry Burd
Author
Ranch Hand
Posts: 133
10
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris, It's probably easier to give you a good answer if you can post the rest of the code (including the getUserInput method declaration).
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gopinath anumalasetty wrote:
if(horesname==null){
horsename = (entry.getUserInput("Please reenter: "));
}else{
while(horsename.length()==0 || horsename.length()>18)){
---
---
horsename = (entry.getUserInput("Please reenter: "));
}

}

Thanks for trying to help the OP but Joanne has already explained why this approach is wrong and also quite rightly suggested we wait for more information before confusing the OP with guesses.

BTW if you aren't sure why the changes you made to your original suggestion are still wrong think about this: From what the OP has said it appears that getUserInput can return null so work through your code and for each section that has a call to getUserInput think what will happen if it returns null.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!