Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying to find cause of NullPointerException

 
Jacob Steingart
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have yet another problem with the code I posted earlier in the thread titled "Strange Compiler Error." I sized my array from... whatever large number it had been earlier down to 50 because no one will actually be using it. Now the stack is happy isn't trying to load up with empty slots in an array, but I'm getting a NullPointerException in runtime. I think there is some bug in the initialization of a User object inside createAccount(), or one in saveUser(). Here is the error:

Exception in thread "main" java.lang.NullPointerException
at AccountStuff.saveUser(AccountStuff.java:74)
at AccountStuff.createAccount(AccountStuff.java:66)
at AccountStuff.createAccountDll(AccountStuff.java:37)
at AccountStuff.logIn(AccountStuff.java:24)
at AccountStuff.main(AccountStuff.java:12)


And here is the code:




I'll be very grateful if someon can figure this out. Thanks in advance!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jacob Steingart:
...Exception in thread "main" java.lang.NullPointerException
at AccountStuff.saveUser(AccountStuff.java:74)...

Line 74, where the NullPointerException is occurring, is...

What do you see in this line that could be null?

Hint: You mentioned "empty slots" in the array...
[ July 31, 2008: Message edited by: marc weber ]
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As another hint, take a look a look at the hint I provided to someone else that had a very similar problem recently.
 
Jacob Steingart
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now see what I'm doing, I just don't know an efficient way to solve it. I have loads of undeclared slots in my array, and I'm asking the program to do something with them, and that won't work because they don't exist, they only have space to exist. Basically I'm trying to write a partially null array to a file, am I correct? How could I give the arrays a default value? Could I walk through them using a for loop and check if they have a value, and if they don't assign them one? That assumes that the empty slots are the problem...
 
Norm Radder
Ranch Hand
Posts: 895
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you skip over the empty slot or output a place holder?
In your for loop, test if user[i] == null and either continue the loop or output a place holder in stead of the name etc
 
Campbell Ritchie
Sheriff
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You needn't use the multiplication for random digits; there is a method in the java.util.Random class with gives a pseudo-random int number much more simply.

Where do you initialise the individual members of the users array? Both Marcs have already told you where the problem is, and given hints about what it is. If you go back to their hints, you will be able to sort out the error and get code which works.

The if (...!=null) solution doesn't really help preventing a NullPointerException; it simply misses out the offending bit of code and obscures the cause.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jacob Steingart:
Now see what I'm doing, I just don't know an efficient way to solve it. I have loads of undeclared slots in my array, and I'm asking the program to do something with them, and that won't work because they don't exist, they only have space to exist. Basically I'm trying to write a partially null array to a file, am I correct? How could I give the arrays a default value? Could I walk through them using a for loop and check if they have a value, and if they don't assign them one? That assumes that the empty slots are the problem...


One way to solve it would be to keep track of the number of users you are actually have, then only iterate over those users. You already even have a variable that looks like it is made to keep track of the number of users.

An example might be like this (hacked away at the unimportant parts and didn't test code, but hopefully you will see the idea)

[ August 03, 2008: Message edited by: Steve Luke ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Luke:


One way to solve it . . .
The bit about new User() ought to prevent null values, but there is another problem.

Why are you trying to save the whole array? Why don't you simply save the new User?
[ August 04, 2008: Message edited by: Campbell Ritchie ]
 
Jacob Steingart
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought that saving the whole array would make the file reading a bit easier... oh my <censor></censor> G-d! I used numOfUsers instead of 50 in the file and it actually wrote the file. I'll probably need to clear the file before I write the users to the variable, and I'll also need to make several more methods. Well, that's all I have for now. One bug down, who-knows-how-many more to go...
[ August 04, 2008: Message edited by: Jacob Steingart ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic