调用getObjectSize()时的NPE

时间:2014-01-22 14:48:53

标签: java nullpointerexception

参考:Calculate size of Object in Java

我尝试运行此程序并获取创建对象的大小

  //main 
 while(true){
        Integer x=new Integer(50);
        System.out.println("HashCode of this object="+x.hashCode()+" 
                  ,  with Size of "+ObjectSizeFetcher.getObjectSize(x));
        i++;
}

 The output -- NullPointerException is thrown when getObjectSize() is invoked.

   new Integer() -->

应该已经创建了一个新的包装器对象。相反,它创造了NPE。为什么?

java版“1.6.0_65” Apple OSX - 10.9.1 --Ignore语法错误

1 个答案:

答案 0 :(得分:1)

因为当您致电ObjectSizeFetcher.instrumentation时,静态字段nullObjectSizeFetcher.getObjectSize(x)

您之前应使用非空ObjectSizeFetcher.premain(String args, Instrumentation inst)参数调用方法inst