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]

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!
> 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.