Strange nullPointer ?

Hi there!

Im new to this forum, so I dont know if i'm doing this correctly.

I've searcher the forum, but couldnt find this problem (probably not the right search-words :P)

This is my code

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.io.*;

//class

public class database implements Serializable{

private LinkedList users = new LinkedList();

private ListIterator iterator = users.listIterator();

//Constructor

public database(){

}

public void addUser(user giveUser){

try {ObjectInputStream in = new ObjectInputStream(new FileInputStream("database.dat"));

users=(LinkedList)in.readObject();

in.close();

System.out.println("Laden succesvol");

}

catch(Exception p) {

System.out.println("It wont work");

}

users.add(giveUser); //this gives me a nullPointer exeption

System.out.println(""+giveUser); //this doesnt give me a nullpointer exeption and just prints the user on the screen

try {ObjectOutputStream uit = new ObjectOutputStream(new FileOutputStream("database.dat"));

uit.writeObject(users);

uit.flush();

uit.close();

System.out.println("Opslaan Succesvol");

}

catch(IOException p) {

System.out.println("Hij doet het niet");

}

}

}

I dont know why i'm getting the Nullpointer at line 26 (added the comment to it)

Note that "user" is a self-made class.... I beleve there's a standard one... dunno about that though... but whatever :-) Just so you know.

can anyone help me?

thanks in advance

[1670 byte] By [Qternocq_a] at [2007-10-2 9:55:19]
# 1
Are you sure that line is the problem? A LinkedList permits nulls.Copy and paste the actual exception back here; also double check what line is throwing it, and copy and paste that line.
ChuckBinga at 2007-7-17 0:00:02 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2
When you post code, to retain your code formatting, select the code and click the "code" button just above the typing area. Then preview the post.
ChuckBinga at 2007-7-17 0:00:02 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 3

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.io.*;

//klasse

public class database implements Serializable{

private LinkedList users = new LinkedList();

private ListIterator iterator = users.listIterator();

//Constructor

public database(){

}

public void addUser(user geefUser){

try {ObjectInputStream in = new ObjectInputStream(new FileInputStream("database.dat"));

users=(LinkedList)in.readObject();

in.close();

System.out.println("Laden succesvol");

}

catch(Exception p) {

System.out.println("Hij doet het niet");

}

users.add(geefUser);

System.out.println(""+geefUser);

try {ObjectOutputStream uit = new ObjectOutputStream(new FileOutputStream("database.dat"));

uit.writeObject(users);

uit.flush();

uit.close();

System.out.println("Opslaan Succesvol");

}

catch(IOException p) {

System.out.println("Hij doet het niet");

}

}

}

gives me this exeption:

http://82.173.133.142/jon/yesnullpointer.png

and with the "users.add(geefUser) commented away, i got this output, which is good...

http://82.173.133.142/jon/nonullpointer.png

So... I hope this will help in your conclusions.

These things occur when I use the addUser method with another class (client.java), but im sure everyone understood that. If anyone want me to post that code too, its no problem... But the only thing there is: database.addUser(tempUser);

So thats easy :-)

Thanks a lot!

Qternocq_a at 2007-7-17 0:00:02 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 4
Add "System.out.println(users);" right before the current line #26. I believe it will display null. If it does, then you probably need to go trace the code from this lineusers=(LinkedList)in.readObject();I am guessing that in.readObject() returns null.
atmguya at 2007-7-17 0:00:02 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 5
So the problem might not be that geefUser = null, but that users = null ? So users isnt even a linkedlist? If it IS a linkedlist, would it matter if it were null? I'm only adding something to it....Im a bit confused :)
Qternocq_a at 2007-7-17 0:00:02 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 6

> Add "System.out.println(users);" right before the

> current line #26. I believe it will display null.

> If it does, then you probably need to go trace the

> code from this lineusers=(LinkedList)in.readObject();

> I am guessing that in.readObject() returns null.

Did you try this? Yes, users might be null and if it is, you will get a null pointer exception if you call a method using it.

atmguya at 2007-7-17 0:00:02 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 7
Yep, i tried it... And you were right! Thanks a lot!
Qternocq_a at 2007-7-17 0:00:02 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...