从jsp调用时,在hibernate dao中获取空指针

时间:2012-02-24 09:04:21

标签: java hibernate spring nullpointerexception

这是我在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;
        }
    }

1 个答案:

答案 0 :(得分:1)

你在这里得到一个例外:

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

您捕获异常并返回null,然后该null结果在您的其他类中导致NullPointerException。您需要找到要打印的堆栈跟踪(e.printStackTrace())以了解导致该异常的原因。