Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Null Pointer Exception

Status
Not open for further replies.

JavaDude32

Programmer
Aug 25, 2001
180
US
I'm implementing a single Linked List in my program to handle the connected Users. My problem is that a NullPointerException is thrown when trying to access a value that is null, I guess it is to be expected, should I try to do the addition code in a catch statement or is there a better way to do a linked list in Java?
btw the equals method threw the same exception

....
//Register user with the connection pool
private synchronized void addUser(String theUserName, Socket b) {
if(list_start == null) {
list_start = new JocusUser(b, theUserName);
} else {

JocusUser theUser;
for(theUser = list_start; theUser != null; theUser = theUser.next()) {
}
theUser.setNext(new JocusUser(b, theUserName));
}
}
....

public class JocusUser {
private Socket s;
private String name;
private JocusUser nextUser;

JocusUser(Socket b, String userName) {
s = b;
name = userName;
nextUser = null;
}

public void setNext(JocusUser nextUser) {
this.nextUser = nextUser;
}

public JocusUser next() {
return nextUser;
}

....
}
 
Quick question: Is this an academic exercise? If not then you should be using the Collection Framework in the Java API. No use reinventing the wheel.

I am going to assume that this is just an exercise. In this case you have two options:
1) Since you are always adding to the end of the list then you should just keep a last node reference and jump immediately to the end of the list.
2) Base you for loop on theUser.next() != null. You want to stop ON the last node, not AFTER the last node. If you do this then you will need to make a special case if you list is of size 1 similar to the special case you currently have to size 0 lists.
2b) This will mean you need to keep the size of the list throughout. You should be doing this already though.
 
I noticed that later on last night, but was offline. Thanks for the advice, this is just my own hobbial project, I will be using such next year in my programming class so I figured I'd get a little practice and implement a linked list. I don't quite the see the special case for size 1 though since it shouldn't execute the loop at all, just assign the reference to the first link if it is size 1?
 
In a singly linked list, you shouldn't use a loop to add nodes at all. Like wushutwist said, you always add at the end of the list, so just keep a reference to the last node. The trade off here is the memory involved in one reference versus the CPU time to find the end of your list. Spend the memory.

I don't think there should be a special case for when the size equals one. In this case, the next reference on the only node is null, the pointer to the end of the list references the single node as does the first pointer. A loop that checks whether the next reference is null will return this solitary node.

Just a tip, but you may find it is easier to deal with a linked list if you make a LinkedList class in addition to a Node class. This LinkedList class can handle all of the overhead for you, such as keeping track of how many nodes there are, the pointers to the first and last nodes, methods for searching the list, as well as removing nodes. When you want to use the linked list, you use the LinkedList class as an interface, which builds the linked list out of Node objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top