aspose file tools*
The moose likes Beginning Java and the fly likes Trying to find cause of NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trying to find cause of NullPointerException" Watch "Trying to find cause of NullPointerException" New topic
Author

Trying to find cause of NullPointerException

Jacob Steingart
Ranch Hand

Joined: Mar 28, 2008
Posts: 63
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!


I never know what to put in my signature...
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

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

Joined: Mar 28, 2008
Posts: 63
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

Joined: Aug 10, 2005
Posts: 690
    
    1
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

Joined: Oct 13, 2005
Posts: 39834
    
  28
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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 ]

Steve
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
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

Joined: Mar 28, 2008
Posts: 63
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trying to find cause of NullPointerException