Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

dereferenced errors.....ugh  RSS feed

 
Edward Knight
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to let you know I have been programming Java for 4 weeks........
Now that you have stoped cringing I have a problem that I am sure everyone but me knows how to fix.
I am trying an IF loop to input employee.getName in the program file and save that and other information into a EmployeeRecord.java file.
Here is the if loop;
if ( ( employee.getName() ).equalsIgnoreCase( STOP ) )
{
break; //Exits Main Loop
}// End IF
That is supposed to have the user be in a loop inputing information until they enter stop.....

Yea, so I am clueless and very much so noobtastic, some one guide me with their elite Java Force....PLEASE!
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the exception or specific problem you are encountering? I couldn't determine that from the post.



Is dangerous because employee.getName() may be NULL. Calling .equalsIgnoreCase() on a NULL reference will result in a NullPointerException. (I'm assuming that you've already checked for a NULL employee reference.)

To eliminate the possiblity of a NPE, you'll need to add a check for employee.getName() == NULL before the if statement. Or, what I do is reverse the comparision:



STOP will not be NULL because you defined it as a final static String and initialized it, right?
 
Edward Knight
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used your suggestion of inverting the STOP in the .equalsIgnoreCase and then changed from void to Static in the other file. It has taken my error away, thank you sooo much . I really appreciate the help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!