JDK-4479682 : Program gets java.lang.NullPointerException, works with -classic mode
  • Type: Bug
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 1.3.1
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_nt
  • CPU: x86
  • Submitted: 2001-07-13
  • Updated: 2004-11-03
  • Resolved: 2001-07-31
Related Reports
Relates :  
Relates :  
Description
Name: rmT116609			Date: 07/13/2001


java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)


If I compile this program with no options, and run it with no
options, it gets a NullPointerException.  If I run it with
-classic, it runs to completion.  The latter behavior appears
to be the correct one.

$ d:/jdk1.3.1/bin/javac Bug.java
$ d:/jdk1.3.1/bin/java -cp . Bug
java.lang.NullPointerException
	at java.util.HashMap.get(HashMap.java:266)
	at Bug$ObjectSet.get(Bug.java:15)
	at Bug$ObjectSet.representation(Bug.java:28)
	at Bug$ObjectSet.write(Bug.java:42)
	at Bug.main(Bug.java:67)
Exception in thread "main"

import java.lang.reflect.*;
import java.lang.*;
import java.io.*;
import java.util.*;

public class Bug
{
   private static class ObjectSet {

      private HashMap        objectMap = new HashMap();
      private static int     objectCount = 0;
      private LinkedList     workList = new LinkedList();

      Integer        get(Object o) {
         Integer ref = (Integer) objectMap.get(o);

         if (ref == null) {
            ref = new Integer(++ objectCount);
            objectMap.put(o, ref);
            workList.add(o);
         }
         return ref;
      }

      void representation(Object o)
      {
         if (o != null) {
            get(o);
         }
      }

      void write() throws Exception
      {
         while (! workList.isEmpty()) {
            Object o = (Object) workList.removeFirst();
            Class clazz = o.getClass();

            if (clazz.isArray()) {
               final int length = Array.getLength(o);
               // For every member of the array...
               for (int i = 0; i < length; i++) {
                  representation(Array.get(o, i));
               }
            } // end array
            else {
               while (clazz != null) {
                  Field fields [] = clazz.getDeclaredFields();

                  // For each field of this object...
                  for (int i = 0; i < fields.length; i++) {
                     Field f = fields[i];
                     f.setAccessible(true);
                     representation(f.get(o));
                  }
                  clazz = clazz.getSuperclass();
               }
            } // end non-array object
         } // end worklist loop
      } // end write
   } // end ObjectSet

   public static void main (String [] args) throws Exception
   {
      ObjectSet os = new ObjectSet();
      Exception e = new Exception("here");
      os.get(e);
      os.write();
   }
}



With JDK 1.4.0-beta:
=========================

C:\>java Bug
#
# HotSpot Virtual Machine Error, Internal Error
# Please report this error at
# http://java.sun.com/cgi-bin/bugreport.cgi
#
# Error ID: 43113F32554E54494D45110E4350500314
#
# Problematic Thread: prio=5 tid=0x00761990 nid=0xd5 runnable
#



C:>java -server Bug
#
# HotSpot Virtual Machine Error, Internal Error
# Please report this error at
# http://java.sun.com/cgi-bin/bugreport.cgi
#
# Error ID: 53484152454432554E54494D450E435050014C
#
# Problematic Thread: prio=5 tid=0x00761990 nid=0xa1 runnable
#
(Review ID: 127894) 
======================================================================
###@###.### 11/3/04 00:32 GMT

Comments
EVALUATION This is broken in all JVM's for JDK 1.4. Works fine with classic 1.3.1 but not 1.3.1 hotspot or server. We seem to failing while getting the elements of Array.get(object o, index i) Simple modifying the Java program shows that we fail while iterating over objectMap. It looks like we have 3 elements in an array but the 2nd element seems to print incorrectly I think then whenwe access the 3rd element we get NPE. Please see modified testcase at: /net/curious-george.east/disk2/Tests/4479682 gary.collins@East 2001-07-23 karen.kinnear@East 2001-07-26 Ken - please check if this is a compatability issue or if the spec needs clarification. --- The problem is occurring because the test case attempts to reflectively access the private "backtrace" field in java.lang.Throwable, which in the Java HotSpot virtual machine implementation is not a "real" java.lang.Object. There are no guarantees that using reflection to read or write private fields of the Java platform implementation will work. If the submitter is attempting to write a custom serialization mechanism, skipping transient fields will avoid the problem. kenneth.russell@eng 2001-07-31
31-07-2001