Menu

#40 removeSessionListener, get NullPointerExce

open
Core (20)
5
2005-11-23
2005-11-23
No

If you

1. open a session
2. add a sessionlistener
3. remove the sessionListener
4. close the session

...you'll get a NullPointerException.

The problem is in SessionImpl.java, and affects both
sessions and channels, which use similar code to
maintain lists of listeners. For some reason, a
list is maintained and an array is maintained
containing the same information. The problem comes
from this odd implementation (why not use just a
List?).
In removeSessionListener, the object is removed from
the list, and then the list is converted to the array,
but a zero-length List doesn't get converted to an
empty array. From the docs for toArray():

If the list fits in the specified array with room
to spare (i.e., the array has more elements than
the list), the element in the array immediately
following the end of the collection is set to null.

The above is always true in removeSessionListener,
so the array gets one more element than the writer
intended. Later, when, say, fireSessionTerminated
is called, it tests for an array length of zero.
The array has one (null) element, the test fails,
the null is accessed, and a NullPointerException
happens.

I fixed this by changing the tests in the six fireXXX
methods to make them check for a list size of zero
instead of an array length of zero. It seems that
this code and the corresponding add/remove methods
would be better if Lists were used and the arrays
were dropped.

Discussion


Log in to post a comment.