Somehow, I'm getting a null pointer exception here with JDK 1.6.0_14:
HttpSession session = request.getSession(true);
LinkedList<MyObject> list = (LinkedList<MyObject>) session.getAttribute(MY_LIST_KEY);
....
list.addFirst( new MyObject(str1, str2, map) );
Then, I get this:
at java.util.LinkedList.addBefore(LinkedList.java:779)
Here's the method:
private Entry<E> addBefore(E e, Entry<E> entry) {
Entry<E> newEntry = new Entry<E>(e, entry, entry.previous);
newEntry.previous.next = newEntry;//this line NPEs
newEntry.next.previous = newEntry;
size++;
modCount++;
return newEntry;
}
which is called by
public void addFirst(E e) {
addBefore(e, header.next);
}
Is there any weird way the list can be serialized/deserialized to break the header entry to cause this to happen? I don't see how this could possibly be failing.
Here's the serialization methods for LinkedList
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
// Write out any hidden serialization magic
s.defaultWriteObject();
// Write out size
s.writeInt(size);
// Write out all elements in the proper order.
for (Entry e = header.next; e != header; e = e.next)
s.writeObject(e.element);
}
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in any hidden serialization magic
s.defaultReadObject();
// Read in size
int size = s.readInt();
// Initialize header
header = new Entry<E>(null, null, null);
header.next = header.previous = header;
// Read in all elements in the proper order.
for (int i=0; i<size; i++)
addBefore((E)s.readObject(), header);
}