<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent changes to 40: removeSessionListener, get NullPointerExce</title><link href="https://sourceforge.net/p/beepcore-java/bugs/40/" rel="alternate"/><link href="https://sourceforge.net/p/beepcore-java/bugs/40/feed.atom" rel="self"/><id>https://sourceforge.net/p/beepcore-java/bugs/40/</id><updated>2005-11-23T23:25:14Z</updated><subtitle>Recent changes to 40: removeSessionListener, get NullPointerExce</subtitle><entry><title>removeSessionListener, get NullPointerExce</title><link href="https://sourceforge.net/p/beepcore-java/bugs/40/" rel="alternate"/><published>2005-11-23T23:25:14Z</published><updated>2005-11-23T23:25:14Z</updated><author><name>Pete Siemsen</name><uri>https://sourceforge.net/u/siemsen/</uri></author><id>https://sourceforge.net892f9d639d369da1c5eb0c76a38bdf2cb8cf76f2</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;If you&lt;/p&gt;
&lt;p&gt;1. open a session&lt;br /&gt;
2. add a sessionlistener&lt;br /&gt;
3. remove the sessionListener&lt;br /&gt;
4. close the session&lt;/p&gt;
&lt;p&gt;...you'll get a NullPointerException.&lt;/p&gt;
&lt;p&gt;The problem is in SessionImpl.java, and affects both&lt;br /&gt;
sessions and channels, which use similar code to&lt;br /&gt;
maintain lists of listeners.  For some reason, a&lt;br /&gt;
list is maintained and an array is maintained&lt;br /&gt;
containing the same information.  The problem comes&lt;br /&gt;
from this odd implementation (why not use just a&lt;br /&gt;
List?).&lt;br /&gt;
In removeSessionListener, the object is removed from&lt;br /&gt;
the list, and then the list is converted to the array,&lt;br /&gt;
but a zero-length List doesn't get converted to an&lt;br /&gt;
empty array.  From the docs for toArray():&lt;/p&gt;
&lt;p&gt;If the list fits in the specified array with room&lt;br /&gt;
to spare (i.e., the array has more elements than&lt;br /&gt;
the list), the element in the array immediately&lt;br /&gt;
following the end of the collection is set to null.&lt;/p&gt;
&lt;p&gt;The above is always true in removeSessionListener,&lt;br /&gt;
so the array gets one more element than the writer&lt;br /&gt;
intended.  Later, when, say, fireSessionTerminated&lt;br /&gt;
is called, it tests for an array length of zero.&lt;br /&gt;
The array has one (null) element, the test fails,&lt;br /&gt;
the null is accessed, and a NullPointerException&lt;br /&gt;
happens.&lt;/p&gt;
&lt;p&gt;I fixed this by changing the tests in the six fireXXX&lt;br /&gt;
methods to make them check for a list size of zero&lt;br /&gt;
instead of an array length of zero.  It seems that&lt;br /&gt;
this code and the corresponding add/remove methods&lt;br /&gt;
would be better if Lists were used and the arrays&lt;br /&gt;
were dropped.&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>