急!ssh+json 空指针异常(net.sf.json.JSONException: java.lang.NullPointerException)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了急!ssh+json 空指针异常(net.sf.json.JSONException: java.lang.NullPointerException)相关的知识,希望对你有一定的参考价值。

net.sf.json.JSONException: java.lang.NullPointerException
at net.sf.json.JSONObject._fromBean(JSONObject.java:959)
at net.sf.json.JSONObject.fromObject(JSONObject.java:194)
at net.sf.json.JSONArray._processValue(JSONArray.java:2562)
at net.sf.json.JSONArray.processValue(JSONArray.java:2593)
at net.sf.json.JSONArray.addValue(JSONArray.java:2580)
at net.sf.json.JSONArray._fromCollection(JSONArray.java:1084)
at net.sf.json.JSONArray.fromObject(JSONArray.java:147)
at gah.ajaxAction.GetVillageAction.execute(GetVillageAction.java:71)
at org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:110)
at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at gah.service.MuserFilter.doFilter(MuserFilter.java:44)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
注:已经从hibernate 那里拿到了数据,这里从打印出来的:viialge222 name=哈哈
这个是action类中主要部分(太长了粘不上),运行到json=JSONArray.fromObject(vs,config);这里就报错了,但vs里是有数据的,都能打印出来
GetVillageForm gvf=(GetVillageForm) form;
if(null!=gvf)
vs=ajaxDao.getVillage(gvf.getDId());
System.out.println("vl="+vs);
Iterator<Village> i=vs.iterator();
while(i.hasNext())
System.out.println("viialge222 name="+i.next().getAName());

JsonConfig config = new JsonConfig();
config.setJsonPropertyFilter(new PropertyFilter()

public boolean apply(Object source, String name, Object value)
System.out.println("village name:"+name);
if(name.equals("district")||name.equals("ccompanies"))
return true;
else
return false;


);try
json=JSONArray.fromObject(vs,config);
response.getWriter().write(json.toString());
catch (Exception e)
e.printStackTrace();

net.sf.json.JSONException: java.lang.NullPointerException
at net.sf.json.JSONObject._fromBean(JSONObject.java:959)
at net.sf.json.JSONObject.fromObject(JSONObject.java:194)
at net.sf.json.JSONArray._processValue(JSONArray.java:2562)
at net.sf.json.JSONArray.processValue(JSONArray.java:2593)
at net.sf.json.JSONArray.addValue(JSONArray.java:2580)
at net.sf.json.JSONArray._fromCollection(JSONArray.java:1084)
at net.sf.json.JSONArray.fromObject(JSONArray.java:147)
at gah.ajaxAction.GetVillageAction.execute(GetVillageAction.java:71)
at org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:110)
at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at gah.service.MuserFilter.doFilter(MuserFilter.java:44)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
注:已经从hibernate 那里拿到了数据,这里从打印出来的:viialge222
参考技术A 这只是一个简单的空指针报错

只能看出是 GetVillageAction.java的71行 某个对象执行方法时 由于这个对象是null 才报的错

要更详细的 请把代码贴出来看看

---------------------------------------
你就是想用ID查询这个数据

然后用获取的对象 筛选掉district 和ccompanies属性

然后返回到页面请求 是吗?

我看来看去可能的错误就是在apply里面

这个System.out.println("village name:"+name); 有打出null的时候吗?
最好是在apply方法里面打个断点debug一下!
参考技术B 请问楼主解决了吗?我这问题和你这里的很像

百度问问java如何判断对象是不是为空时,报空指针异常,如何解决?

我实例了一个对象
book book=new book();
查询:
book=bookService.findBook(xxxx);
if(!book.equals("")&&book!=null)

报空指针异常,如何解决,才不会报空指针异常?

比较好的写法
book=bookService.findBook(xxxx);

if(book != null && !book.equals(""))



Java逻辑判断都是从左边开始的,所以如果book为null那么后面的equals就不会执行,也就不会报nullPointerException了
参考技术A if(!book.equals("")&&book!=null)这一段修改一下即可:
第一种:if(book!=null&&!book.equals(""))。就是说先判空,再比较
第二种:if(!"".equals(book))。这样也可以。就是说让equals前边的那个参数为一个确认非null的值
参考技术B if(!book.equals("")&&book!=null)

改成
if(book!=null)
参考技术C if(bookService!=null)
book=bookService.findBook(xxxx);
if(book!=null)
//dosomething

以上是关于急!ssh+json 空指针异常(net.sf.json.JSONException: java.lang.NullPointerException)的主要内容,如果未能解决你的问题,请参考以下文章

进行jdbc时出现异常,我检查了,表名没有错误,却报异常。急!急!急!

JSONObject 创建抛出空指针异常

百度问问java如何判断对象是不是为空时,报空指针异常,如何解决?

java解析json字符串

在扩展数组适配器时自定义适配器的getView()中的空指针异常

jsp 505空指针异常