比較hibernate查詢結果中的空值如此簡單!
hibernate查詢資料庫欄位時報錯:java.lang.NullPointerException,經檢查,原來是某些欄位為空值,如何處理呢?...
緣起:
hibernate查詢資料庫欄位時報錯:java.lang.NullPointerException,經檢查,原來是某些欄位為空值,如何處理呢?...
修改前的語句(報錯):
Session newSession = HibernateUtil.getSessionFactory().openSession();
Transaction newTransaction = newSession.beginTransaction();
List etlTables = newSession.createQuery("from Table ").list();
for (Iterator iter = eTables.iterator(); iter.hasNext();) {
ETable eTable = (ETable) iter.next();
if (eTable.getNumRowsCurrSource() <= 10000 ) ) {
......
處理後的語句(正常執行,允許空值出現):
Session newSession = HibernateUtil.getSessionFactory().openSession();
Transaction newTransaction = newSession.beginTransaction();
List eTables = newSession.createQuery("from Table ").list();
for (Iterator iter = eTables.iterator(); iter.hasNext();) {
ETable eTable = (ETable) iter.next();
if ((eTable.getNumRowsCurrSource() == null )||
(eTable.getNumRowsCurrSource() <= 10000 ) ) {
......
關鍵點:
可以直接比較一個物件和null
走過的彎路: eTable.getNumRowsCurrSource().equals(null) 等均為錯誤的。
我用Hibernate與資料庫取資料,當取得資料庫的某條記錄都有值的時候,一切正常,而某條為NULL值是就報錯
嚴重: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at org.apache.jsp.testHibernate_jsp._jspService(org.apache.jsp.testHibernate_jsp:90)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:99)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:325)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:738)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
請問有誰知道這是為什麼嗎?
我用Hibernate與資料庫取資料,
當取得資料庫的某條記錄都有值的時候,一切正常,
而當某條記錄中的某個欄位值為NULL值是就報錯以上錯誤
org.hibernate.Session s = HibernateUtil.currentSession();
String hql1 =" from Cat";
try {
Query query = s.createQuery(hql1);
List catlist = query.list();
Cat c=(Cat)catlist.get(0);
CatId cat = ((Cat)catlist.get(0)).getId();
out.println(cat.getName());
out.println("<br/>");
} catch (org.hibernate.HibernateException e) {
e.printStackTrace();
}
HibernateUtil.closeSession();
=======================
我給他的解答:
對out.println(cat.getName());進行處理:if cat.getName() != null
then{ 列印}
--by chq
http://www.chq.name