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块
提前感谢尝试向上转换到泛型类型,这样会更容易。尝试向上转换到泛型类型,这样会更容易。