java.lang.NullPointerException在一个奇怪的情况下

时间:2017-08-27 04:43:55

标签: hibernate jsp nullpointerexception

我有一个非常奇怪的情况。我正在调用hibernate函数来读取数据库中的条目,它在大多数我称之为的地方都有效,但只在一个地方,它不起作用,给我{{ 1}}。我认为代码中缺少一些非常小的东西,但我不知道它是什么。看看这里:

我称之为函数的NullPointerException

Servlet

hibernate方法:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String dataA = request.getParameter("answers");
        String dataQ = request.getParameter("quests");
        String user = request.getParameter("usern");
        String chap = request.getParameter("chapt");

        String[] answers = dataA.split(",");
        String[] quests = dataQ.split("&");


        TestHandler tHandler = new TestHandler(5);
        UserDAO u=new UserDAO();
        UserChapterDAO uc = new UserChapterDAO();


        ChapterDAO chdao = new ChapterDAO();
        Chapter myCh=chdao.findTChapter(chap+"");

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println(chap);
        out.println(myCh.getId());

错误追踪:

 @SuppressWarnings("unchecked")
        public Chapter findTChapter(String title) {

            List<Chapter> chapter = null;
            Transaction tx=null;

              try{
                    Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
                    SessionFactory sessionFactory = configuration.buildSessionFactory();
                    Session session = sessionFactory.openSession();
                    tx = session.beginTransaction();

                Query query = session.createQuery("FROM Chapter WHERE title = :title");
                query.setParameter("title", title);
                chapter = query.list();
                tx.commit();
            } catch (HibernateException e) {
                if (tx != null) {
                    tx.rollback();
                }
                LOGGER.error("", e);
            } 

              return chapter != null && !chapter.isEmpty() ? chapter.get(0) : null;
        }

servlet映射如下:

SEVERE: Servlet.service() for servlet [TestServlet] in context with path [/HibernateWebApp] threw exception
java.lang.NullPointerException
    at controller.TestServlet.doPost(TestServlet.java:76)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

调用servlet的jsp部分:

<servlet>
    <display-name>Test</display-name>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>controller.TestServlet</servlet-class>
  </servlet>
   <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/test</url-pattern>
  </servlet-mapping>

错误出现在 out.println(myCh.getId()),因此id实际上并没有从DB读取章节并存储在myCh对象中。 我提到在我使用它的其他地方,它完美无缺。另外,如果我通过将String chap硬编码到实际String中来调用该函数,它就可以了。而且我也确定String chap完全取自jsp,因为我用out.println(chap)打印它并且它是必须的。

我真的被困在那里。有什么想法吗?

3 个答案:

答案 0 :(得分:0)

尝试此查询

Query query = session.createQuery("FROM Chapter c WHERE c.title = :title");

答案 1 :(得分:0)

第一件事: String chap = request.getParameter("chapt");

将其更改为: String chap = request.getParameter("chapter");

同时确认您的操作网址是否为以下格式: 的 http://appurl/test?chapter=abcd

答案 2 :(得分:0)

错字。

String chap = request.getParameter("chapt");

<input type="hidden" id="chatp" name="chapt" value="<%out.println(request.getParameter("chapter")); %>">   

尝试让您的ID,名称和参数名称达成一致。