• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help desperately: "NullPointerException"  RSS feed

 
Michael Ferguson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


How can I get this loop not to throw this exception?
 
Michael Ferguson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Tim Moores
Saloon Keeper
Posts: 4024
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which line of the code does the exception occur? Post the full stack trace, and indicate that line. Which object is null?
 
Michael Ferguson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:In which line of the code does the exception occur? Post the full stack trace, and indicate that line. Which object is null?


It occurs on the line "NodeData = CurrentNode.getData();" that is inside the while(true) statement. I do not know how to look at a stack trace for this, I'm running it from the command line.

I'm also not certain about what object is null. This is the reason I am so confused.

My program is trying to create a Hash table using the chaining method. I've created an array of nodes that all point to 'null' and have the value of '0'. It should be setting NodeData equal to 0 or an integer X on the line that the exception is thrown.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Ferguson wrote:It occurs on the line "NodeData = CurrentNode.getData();" that is inside the while(true) statement.

Then, most likely, CurrentNode is null.

One of the most common reasons for a NullPointerException is when you try to call a method on a variable that is null.

You'll need to check that CurrentNode is not null before calling a method on it, and think about the logic of your program - under what circumstances is CurrentNode null at that point, what does that mean and what should happpen if that's the case?
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the full stack trace?
You can copy the text in the screen by doing Ctrl-M and then highlighting the text you want to copy, then <Enter>.

Because there is no way that CurrentNode can be null on line 16.

ETA: Gah.  Scratch that last line, as line 15 reassigns it...


Which means the value returned by getLink can be null.
So you need to handle the case where you are at the end of your nodes.
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a side note, you have to work on your naming conventions, names such as "HashSize", "NodeData", "CurrentNode", etc. are not the standard way to name variables in java. While the code makes sense to you, it will really confuse another programmer reading your code.

Have a look at the java tutorial : https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
Java Tutorial wrote:If the name you choose consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word. The names gearRatio and currentGear are prime examples of this convention. If your variable stores a constant value, such as static final int NUM_GEARS = 6, the convention changes slightly, capitalizing every letter and separating subsequent words with the underscore character. By convention, the underscore character is never used elsewhere.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!