Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java fromObject(null)不';你不会抛出任何异常吗?_Java_Json - Fatal编程技术网

Java fromObject(null)不';你不会抛出任何异常吗?

Java fromObject(null)不';你不会抛出任何异常吗?,java,json,Java,Json,我一直在尝试运行以下代码块。我发现了这种行为 net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(null); if(json!=null) { System.out.println(json); } 它不会抛出任何异常。输出为“空” 当我添加尺寸检查时 net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(null); if(j

我一直在尝试运行以下代码块。我发现了这种行为

net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(null);
if(json!=null) {
        System.out.println(json);
}
它不会抛出任何异常。输出为“空”

当我添加尺寸检查时

net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(null);
if(json!=null && json.size()>0) {
         System.out.println(json);
}
它抛出这样的异常

线程“main”net.sf.json.JSONException中的异常:null对象 在net.sf.json.JSONObject.verifyIsNull(JSONObject.java:2856) net.sf.json.JSONObject.size(JSONObject.java:2516)

我花了很长时间寻找这个,但没有运气。有人能解释这种行为吗?其他人重定向到apt站点。 还可以帮助我如何使用正确的null和empty检查执行这个if块

提前感谢

尝试向上转换到泛型类型,这样会更容易。尝试向上转换到泛型类型,这样会更容易。