This question already has answers here:
What is a NullPointerException, and how do I fix it?
                                
                                    (12个答案)
                                
                        
                5年前关闭。
            
        

我写了这段代码

List<Object> result = null;

....
Object resultObject =method.invoke(o, "test", "test");
System.out.println(resultObject);
result.add(resultObject);


resultObject包含我期望的结果,但最后一行导致NullpointerException。

最佳答案

List<Object> result = null;


变量result现在包含null

result.add(resultObject);


您尝试在add()上调用result,但是由于resultnull,因此将引发NullPointerException

您应该创建列表。将第一行替换为:

List<Object> result = new ArrayList<Object>();

10-07 16:23
查看更多