You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-dev@xml.apache.org by Jeff Suttor <Je...@Sun.COM> on 2002/07/11 09:11:09 UTC

NPE in DocumentCache$CacheKey.equals()

we notice that a NullPointerException is thrown in 
DocumentCache$CacheKey.equals() on a regular basis when using JDK1.4 and 
Solaris/Linux.  note though that there has also been a report on this 
list of the same NPE using JDK1.3 on Windows.

a null Object is being passed into the DocumentCache$CacheKey.equals() 
causing an NPE when an attempt is made to test its collection value.  as 
the cache is a WeakHashMap, I'm wondering if it was possible that the 
object has been garbage collected.  should a WeakHashMap be used in an 
environment with recreatable keys?

listed below is a simple fix that simply checks for a null incoming 
object and returns false, i.e.  the instance says that it isn't possible 
for me to be equal to null.

could this please be committed to cvs repository?  it passes all of 
tests  for our application that used to reliably cause the NPE.

this was harder to track down that it should have been as 
FaultCodes.createAPIException() was returning it as a generic 
RuntimeException with an empty message.

listed below is a simple change that provides more information when the 
Exception isn't an instanceof APIException.  it uses 
Exception.toString() v. Exception.getMessage() as that should at least 
get you the Exception's class name.  I think an 
Exception.printStackTrace() should be done as this is an unanticipated 
error event.

could this also please be committed to cvs repository?  it makes it a 
lot easier to debug.

thanks!

-- 
Jeff Suttor <Je...@Sun.com>


cvs diff -u -r1.1.1 -r1.2 DocumentCache.java
Index: DocumentCache.java
===================================================================
RCS file: 
/jeeves/registry-server/xml-xindice/java/src/org/apache/xindice/core/DocumentCache.java,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -r1.1.1.1 -r1.2
--- DocumentCache.java	2002/07/10 21:39:54	1.1.1.1
+++ DocumentCache.java	2002/07/11 05:44:45	1.2
@@ -167,6 +167,12 @@
        }

        public boolean equals(Object o) {
+ 
// on Solaris/Linux with JDK1.4 Object is sometimes null
+ 
// is WeakHashMap being garbage collected more aggressively?
+ 
if ( o == null ) {
+ 
   return false;
+ 
}
+
           CacheKey comp = (CacheKey)o;
           if ( col != comp.col )
              return false;



cvs diff -u -r1.1.1 -r 1.2 FaultCodes.java
Index: FaultCodes.java
===================================================================
RCS file: 
/jeeves/registry-server/xml-xindice/java/src/org/apache/xindice/core/FaultCodes.java,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -r1.1.1.1 -r1.2
--- FaultCodes.java	2002/07/10 21:39:54	1.1.1.1
+++ FaultCodes.java	2002/07/11 05:44:45	1.2
@@ -203,9 +203,13 @@
        if ( e instanceof APIException )
           return (APIException)e;
        else {
-         String m = e.getMessage();
-         if ( m == null )
-            m = "";
+ 
// provide as much information as possible about the exception
+        String m = e.toString();
+ 
if ( m == null ) {
+ 
   // should never happen
+ 
   m = "Exception.toString() returned null in 
FaultCodes.APIException(Exception)";
+ 
}
+ 
e.printStackTrace();

           if ( e instanceof RuntimeException )
              return new APIException(JAVA_RUNTIME_ERROR, 
getMessage(JAVA_RUNTIME_ERROR), m);


Re: NPE in DocumentCache$CacheKey.equals()

Posted by Kimbro Staken <ks...@xmldatabases.org>.
Thanks, it's in.

On Thursday, July 11, 2002, at 12:11  AM, Jeff Suttor wrote:

> we notice that a NullPointerException is thrown in DocumentCache$CacheKey.
> equals() on a regular basis when using JDK1.4 and Solaris/Linux.  note 
> though that there has also been a report on this list of the same NPE 
> using JDK1.3 on Windows.
>
> a null Object is being passed into the DocumentCache$CacheKey.equals() 
> causing an NPE when an attempt is made to test its collection value.  as 
> the cache is a WeakHashMap, I'm wondering if it was possible that the 
> object has been garbage collected.  should a WeakHashMap be used in an 
> environment with recreatable keys?
>
> listed below is a simple fix that simply checks for a null incoming 
> object and returns false, i.e.  the instance says that it isn't possible 
> for me to be equal to null.
>
> could this please be committed to cvs repository?  it passes all of tests 
>  for our application that used to reliably cause the NPE.
>
> this was harder to track down that it should have been as 
> FaultCodes.createAPIException() was returning it as a generic 
> RuntimeException with an empty message.
>
> listed below is a simple change that provides more information when the 
> Exception isn't an instanceof APIException.  it uses Exception.toString() 
> v. Exception.getMessage() as that should at least get you the Exception's 
> class name.  I think an Exception.printStackTrace() should be done as 
> this is an unanticipated error event.
>
> could this also please be committed to cvs repository?  it makes it a lot 
> easier to debug.
>
> thanks!
>
> -- Jeff Suttor <Je...@Sun.com>
>
>
> cvs diff -u -r1.1.1 -r1.2 DocumentCache.java
> Index: DocumentCache.java
> ===================================================================
> RCS file: /jeeves/registry-server/xml-xindice/java/src/org/apache/xindice/
> core/DocumentCache.java,v
> retrieving revision 1.1.1.1
> retrieving revision 1.2
> diff -u -r1.1.1.1 -r1.2
> --- DocumentCache.java	2002/07/10 21:39:54	1.1.1.1
> +++ DocumentCache.java	2002/07/11 05:44:45	1.2
> @@ -167,6 +167,12 @@
>        }
>
>        public boolean equals(Object o) {
> + // on Solaris/Linux with JDK1.4 Object is sometimes null
> + // is WeakHashMap being garbage collected more aggressively?
> + if ( o == null ) {
> +   return false;
> + }
> +
>           CacheKey comp = (CacheKey)o;
>           if ( col != comp.col )
>              return false;
>
>
>
> cvs diff -u -r1.1.1 -r 1.2 FaultCodes.java
> Index: FaultCodes.java
> ===================================================================
> RCS file: /jeeves/registry-server/xml-xindice/java/src/org/apache/xindice/
> core/FaultCodes.java,v
> retrieving revision 1.1.1.1
> retrieving revision 1.2
> diff -u -r1.1.1.1 -r1.2
> --- FaultCodes.java	2002/07/10 21:39:54	1.1.1.1
> +++ FaultCodes.java	2002/07/11 05:44:45	1.2
> @@ -203,9 +203,13 @@
>        if ( e instanceof APIException )
>           return (APIException)e;
>        else {
> -         String m = e.getMessage();
> -         if ( m == null )
> -            m = "";
> + // provide as much information as possible about the exception
> +        String m = e.toString();
> + if ( m == null ) {
> +   // should never happen
> +   m = "Exception.toString() returned null in 
> FaultCodes.APIException(Exception)";
> + }
> + e.printStackTrace();
>
>           if ( e instanceof RuntimeException )
>              return new APIException(JAVA_RUNTIME_ERROR, 
> getMessage(JAVA_RUNTIME_ERROR), m);
>
>
Kimbro Staken
Java and XML Software, Consulting and Writing http://www.xmldatabases.org/
Apache Xindice native XML database http://xml.apache.org/xindice
XML:DB Initiative http://www.xmldb.org