2012-02-24 49 views
2

調用的時候,這裏是我在JSP代碼,獲得空指針處於休眠DAO從JSP

<% 
     JSonHandler jSonHandler = new JSonHandler(); 
     jSonHandler.setList(jSonHandler.getHolidayDetails()); 
     JSONArray obj = jSonHandler.getJsonArray(); 
    %> 

而且從JSonHandler類,我調用一個方法的HibernateDAO返回對象的ArrayList。但是我得到了一個空指針。

這是我JSonHandler類

public class JSonHandler 
{ 
    private LeaveAdminDAO leaveAdminDAO = new LeaveAdminHibernateDAO(); 
    private Map<String, String> map; 
    private List<Map<String, String>> list; 
    public List<Map<String, String>> getHolidayDetails() 
    { 
     Calendar currentDate = Calendar.getInstance(); 
     int year = currentDate.get(Calendar.YEAR); 
     List<Holidays> holidayDetails = leaveAdminDAO.getHolidayDetails(year); 
     map = new TreeMap<String, String>(); 
     list = new ArrayList<Map<String, String>>(); 
     for(int i=0; i<holidayDetails.size(); i++) 
     { 
      map.put("title", holidayDetails.get(0).getHolidayDescription()); 
      map.put("start", new SimpleDateFormat("yyyy-MM-dd").format(holidayDetails.get(0).getHolidayDate())); 
      list.add(map); 
     } 
     return list; 
    } 
    public List<Map<String, String>> getList() 
    { 
     return list; 
    } 

    public void setList(List<Map<String, String>> list) 
    { 
     this.list = list; 
    } 

    public JSONArray getJsonArray() 
    { 
     JSONArray obj = JSONArray.fromObject(list); 
     return obj; 
    } 

有人能幫助我在此先感謝。

堆棧跟蹤:

java.lang.NullPointerException 
    com.experion.lts.util.JSonHandler.getHolidayDetails(JSonHandler.java:28) 
    org.apache.jsp.views.markholidays_jsp._jspService(markholidays_jsp.java:72) 
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386) 
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) 
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
    org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:968) 
    org.apache.jasper.runtime.PageContextImpl.doInclude(PageContextImpl.java:653) 
    org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:647) 
    org.apache.tiles.jsp.context.JspTilesRequestContext.include(JspTilesRequestContext.java:103) 
    org.apache.tiles.jsp.context.JspTilesRequestContext.dispatch(JspTilesRequestContext.java:96) 
    org.apache.tiles.renderer.impl.TemplateAttributeRenderer.write(TemplateAttributeRenderer.java:44) 
    org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:106) 
    org.apache.tiles.renderer.impl.ChainedDelegateAttributeRenderer.write(ChainedDelegateAttributeRenderer.java:76) 
    org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:106) 
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:670) 
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:336) 
    org.apache.tiles.template.InsertAttributeModel.renderAttribute(InsertAttributeModel.java:210) 
    org.apache.tiles.template.InsertAttributeModel.end(InsertAttributeModel.java:126) 
    org.apache.tiles.jsp.taglib.InsertAttributeTag.doTag(InsertAttributeTag.java:311) 
    org.apache.jsp.views.layout.MainLayout_jsp._jspx_meth_tiles_005finsertAttribute_005f2(MainLayout_jsp.java:196) 
    org.apache.jsp.views.layout.MainLayout_jsp._jspService(MainLayout_jsp.java:111) 
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386) 
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) 
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
    org.apache.tiles.servlet.context.ServletTilesRequestContext.forward(ServletTilesRequestContext.java:241) 
    org.apache.tiles.servlet.context.ServletTilesRequestContext.dispatch(ServletTilesRequestContext.java:222) 
    org.apache.tiles.renderer.impl.TemplateAttributeRenderer.write(TemplateAttributeRenderer.java:44) 
    org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:106) 
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:670) 
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:690) 
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:644) 
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:627) 
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:321) 
    org.springframework.web.servlet.view.tiles2.TilesView.renderMergedOutputModel(TilesView.java:124) 
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250) 
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047) 
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817) 
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719) 
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644) 
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
    com.experion.lts.filters.SecurityFilter.doFilter(SecurityFilter.java:48) 

DAO代碼:

public List<Holidays> getHolidayDetails(int year) 
    { 
     try 
     { 
      List<Holidays> holidayDetails = getHibernateTemplate().find("from Holidays where holidayId = 1"); 
      return holidayDetails; 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
      return null; 
     } 
    } 
+0

您可以編輯您的問題把堆棧跟蹤? – Pablo 2012-02-24 09:09:05

+0

使用調試器並粘貼stacktrace可能會對您有所幫助。 – Ved 2012-02-24 09:11:43

+0

你使用的是OpenSessionInViewFilter嗎?這會在呈現JSP時保持hibernate會話打開。 – Russell 2012-02-24 09:14:39

回答

1

如果您聽到這裏異常:

try 
    { 
     List<Holidays> holidayDetails = getHibernateTemplate().find("from Holidays where holidayId = 1"); 
     return holidayDetails; 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
     return null; 
    } 

你捕獲異常,並返回null,然後是空的結果在其他類中導致NullPointerException。您需要找到要打印的堆棧跟蹤(e.printStackTrace())以瞭解導致該異常的原因。

+1

這個答案是沒有幫助多少反正感謝 – 2012-02-24 09:48:28

+1

它看起來像一個相當有用的答案給我!你嘗試過巴勃羅建議的嗎? – Russell 2012-02-24 12:15:38