TypedQuery给出NullPointerException

时间:2013-12-02 07:15:54

标签: java spring jpa

用Spring MVC和JPA做了一些代码,我偶然发现了这个问题。

在尝试输出我的一个类时,在这种情况下,我在TypedQuery代码行中得到了一个N​​ullPointerException

StudentDAO.java:

// Retrieves all the users:
public List<Student> getAllStudents() {
    TypedQuery<Student> query = em.createQuery("SELECT s FROM Student AS s", Student.class);
    return query.getResultList();
}

下面是出现的错误(编辑:抱歉正在测试另一个查询函数,将其更改为正确的错误消息)

java.lang.NullPointerException
at com.prototype.spring.StudentDAO.getAllStudents(StudentDAO.java:23)
at org.apache.jsp.WEB_002dINF.view.addStudent_jsp._jspService(org.apache.jsp.WEB_002dINF.view.addStudent_jsp:101)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:93)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:470)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:364)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:726)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:285)
at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:126)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:726)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:206)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:829)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:514)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)

但是,当我使用另一个类(在这种情况下为CreateSession)完全相同的代码时,代码输出完全没有错误。任何人都可以帮我理解为什么会有错误吗?

public List<CreateSession> getAllSessions() {
    TypedQuery<CreateSession> query = em.createQuery("SELECT s FROM CreateSession AS s", CreateSession.class);
    return query.getResultList();
}

(EDIT2:以下是StudentDAO.java)

package com.prototype.spring;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class StudentDAO {
    // Injected database connection:
    @PersistenceContext private EntityManager em;

    // Stores a new User:
    @Transactional
    public void persist(Student student) {
        em.persist(student);
    }

    // Retrieves all the users:
    @Transactional
    public List<Student> getAllStudents() {
        TypedQuery<Student> query = em.createQuery("SELECT s FROM Student AS s", Student.class);
        return query.getResultList();
    }

    public List<Student> getFromQuery(String q)
    {
            TypedQuery<Student> query = em.createQuery(q, Student.class);
            return query.getResultList();
    }
}

1 个答案:

答案 0 :(得分:1)

我认为s表中的Student包含null值。由于null属于Student类型,因此可以使用,但当您尝试拨打NullPointerException时会收到getResultList()