I'm struggling to get to the bottom of a null pointer exception that happens when I try to run a HQL query with createQuery().
The code to run the query is pretty simple. Originally I had a named query that I was calling, but just to make things more simple and eliminate any complications I'm doing this (springwildlife is the package and Species is the class/entity):
Query q = session.createQuery("SELECT s FROM springwildlife.Species s");
(BTW: I've also tried 开发者_开发问答it with and without the package name in there. And I've also tried doing a simpler "FROM springwildlife.Species" query)
I've created an XML mapping (species.hbm.xml) like so:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="springwildlife.Species" table="species">
<id name="id" type="java.lang.Long" column="id" >
<generator class="native">
<param name="sequence">species_id_seq</param>
</generator>
</id>
<!-- in the actual file I have one these for each item I want to be mapped -->
<property name="propertyFromClass" type="java.lang.String">
<column name="sql_table_column_name" />
</property>
</hibernate-mapping>
In my hibernate.cfg.xml file I pull in the mapping like so:
My class/entity looks like this:
package springwildlife;
public class Species
{
// properties here
public Species()
{
}
// sets, gets, etc.
}
Does anyone have any idea as to what is happening? I've spent a lot of time trying to track it down and I'm stymied.
(I'm not sure it is relevant, but just in case, I'm using Resin as my server and IntelliJ as my IDE)
This is the stack trace (line 68 in SpeciesFactory.java is the call to createQuery):
[11-06-02 11:19:26.726] {http://*:8080-2} org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549) at javax.servlet.http.HttpServlet.service(HttpServlet.java:119) at javax.servlet.http.HttpServlet.service(HttpServlet.java:96) at com.caucho.server.dispatch.ServletFilterChain.doFilter(ServletFilterChain.java:109) at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:156) at com.caucho.server.webapp.AccessLogFilterChain.doFilter(AccessLogFilterChain.java:95) at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:287) at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:792) at com.caucho.network.listen.TcpSocketLink.dispatchRequest(TcpSocketLink.java:730) at com.caucho.network.listen.TcpSocketLink.handleRequest(TcpSocketLink.java:689) at com.caucho.network.listen.TcpSocketLink.handleRequestsImpl(TcpSocketLink.java:669) at com.caucho.network.listen.TcpSocketLink.handleRequests(TcpSocketLink.java:617) at com.caucho.network.listen.AcceptTask.doTask(AcceptTask.java:104) at com.caucho.network.listen.ConnectionReadTask.runThread(ConnectionReadTask.java:98) at com.caucho.network.listen.ConnectionReadTask.run(ConnectionReadTask.java:81) at com.caucho.network.listen.AcceptTask.run(AcceptTask.java:67) at com.caucho.env.thread.ResinThread.runTasks(ResinThread.java:164) at com.caucho.env.thread.ResinThread.run(ResinThread.java:130) Caused by: java.lang.NullPointerException at springwildlife.SpeciesFactory.getSpeciesLister(SpeciesFactory.java:68) at springwildlife.LifelistController.handleRequestInternal(LifelistController.java:26) at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153) at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644) ... 18 more
In my case the Session object was valid but I still got the NullPointerException
.
Turned out to be an error in my HQL (SELECT TCompany FROM TCompany c...
instead of SELECT c FROM TCompany c...
).
So if the Session is valid, you might check your HQL...
Since NullPointerException
is thrown at the line with createQuery()
, the only possible cause is that session
is null
.
精彩评论