在加载列表的时候 getHibernateTemplate()不为空,在列表页面上点击编辑再进行查找的时候getHibernateTemplate()就为空了!为什么?望高手指点。
解决方案 »
- 关于表单验证的问题!!!
- Myeclipse7.5 怎么调试JS?
- JTree放入JTabbedPane后,不能实现资源管理器
- hibernate奇怪问题,求助
- 100分求解Hibernatepack级联删除为什么总是提示org.hibernate.ObjectDeletedException: deleted object would be re-saved By Cascade
- 请问如何修改servlet的加载参数?
- 哪位高手帮我看看这个小程序怎么编
- 我将是这样学习java的!!!
- soap 如何把返回的xml文档在浏览器上显示?!多谢!
- 小妹请各位SCJDgg帮个忙
- struts2启动问题,在线等~~
- web开发的一个问题
如果有應該沒注入
要是没有注入列表也不应该出来的啊
for (OnlinetestQuestion question : questionList) {
if( "1".equals(question.getKnowledgeType())){
question.setKnowLedgeName(getKnowledgeName(question.getKnowledge()));
}else {
question.setKnowLedgeName(getQualityNamegetQualityName(question.getKnowledge()));
}
question.setKindName(getKindName(question.getKind()));
question.setContentSimple(getContentSimple(question.getContent()));
lazyRoles.add(question);
}以上列表调用的地方 是正确的 public Object findObjById(Object cls, String id) {
if(this.getHibernateTemplate() == null){
System.out.println("this.getHibernateTemplate()为空");
}
return this.getHibernateTemplate().get(cls.getClass(), id);
}以上是报异常Dao的方法 private synchronized String getKnowledgeName(String id) {
CmKnowledge knowledge = (CmKnowledge) this.konwledgeService
.findObjById(new CmKnowledge(), id);
return knowledge == null ? "" : knowledge.getName();
}调用的地方this.onlinetestQuestion.setKnowLedgeName(getKnowledgeName(this.onlinetestQuestion.getKnowledge()));以上是编辑的时候再一次调用的地方
java.lang.NullPointerException
at com.kccbest.hrams.cnodc.dao.BaseDAO.findObjById(BaseDAO.java:150)
at com.kccbest.hrams.cnodc.service.impl.KnowledgeServiceImpl.findObjById(KnowledgeServiceImpl.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy14.findObjById(Unknown Source)
at com.kccbest.hrams.cnodc.view.QuestionsMgrBean.getKnowledgeName(QuestionsMgrBean.java:340)
at com.kccbest.hrams.cnodc.view.QuestionsMgrBean.editQuestion(QuestionsMgrBean.java:262)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.el.parser.AstValue.invoke(AstValue.java:191)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:148)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
at javax.faces.component.UICommand.broadcast(UICommand.java:300)
at javax.faces.component.UIData.broadcast(UIData.java:1093)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)2011-8-11 15:40:00 com.sun.faces.context.AjaxExceptionHandlerImpl log
严重: JSF1073:在 INVOKE_APPLICATION 5 的处理过程中捕捉到 javax.faces.event.AbortProcessingException:UIComponent-ClientId=j_idt22:0,Message=/pages/testMgr/questionsMgr.xhtml @95,86 actionListener="#{questionsMgrBean.editQuestion}": java.lang.NullPointerException
2011-8-11 15:40:00 com.sun.faces.context.AjaxExceptionHandlerImpl log
严重: /pages/testMgr/questionsMgr.xhtml @95,86 actionListener="#{questionsMgrBean.editQuestion}": java.lang.NullPointerException
javax.faces.event.AbortProcessingException: /pages/testMgr/questionsMgr.xhtml @95,86 actionListener="#{questionsMgrBean.editQuestion}": java.lang.NullPointerException
at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:182)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
at javax.faces.component.UICommand.broadcast(UICommand.java:300)
at javax.faces.component.UIData.broadcast(UIData.java:1093)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
at com.kccbest.hrams.cnodc.dao.BaseDAO.findObjById(BaseDAO.java:150)
at com.kccbest.hrams.cnodc.service.impl.KnowledgeServiceImpl.findObjById(KnowledgeServiceImpl.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy14.findObjById(Unknown Source)
at com.kccbest.hrams.cnodc.view.QuestionsMgrBean.getKnowledgeName(QuestionsMgrBean.java:340)
at com.kccbest.hrams.cnodc.view.QuestionsMgrBean.editQuestion(QuestionsMgrBean.java:262)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:148)
... 29 more
以上为异常
code=Java]
package com.kccbest.hrams.cnodc.dao;import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;@SuppressWarnings({"unchecked", "rawtypes", "deprecation"})
public abstract class BaseDAO extends HibernateDaoSupport implements
DAOInterface { /**
* 统计某对象所有的数据值
*
* @return int 条数
*/
public long count(final Object obj) {
HibernateCallback cb = new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
Criteria cr = session.createCriteria(obj.getClass());
cr.setProjection(Projections.rowCount());
Long ii = (Long) cr.uniqueResult();
return ii;
} // end function
};// end callback
return (Long) this.getHibernateTemplate().execute(cb);
} /**
* 根据查询选择条件,计算总数
*
* @param condition
* 查询的条件
* @return int 返回符合条件的 总数
*/
public long countAccording(final String tableName, final String condition) {
HibernateCallback cb = new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
return session.createQuery(tableName + condition).list().size();
} // end function
};// end callback
return (Integer) this.getHibernateTemplate().execute(cb);
} /**
* 修改某个对象属性值
*
* @param obj
* Object 所要修改的对象
*/
public void editRecord(Object obj) {
this.getHibernateTemplate().update(obj);
} /**
* 执行指定的Sql(select)语句
*
* @param sql
* 对应的SQL语句
* @return int 返回值
*/
public List executeSelectSql(final String sql) {
HibernateCallback cb = new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
List list = session.createQuery(sql).list();
//session.close();
return list;
} // end function
};// end callback
return (List)this.getHibernateTemplate().execute(cb);
}
/**
* 执行指定的Sql(create, update, delete)语句(同时也可调用简单的存储过程)
*
* @param sql
* 对应的SQL语句
* @return int 返回值
*/
public void executeCUDSql(final String sql) {
HibernateCallback cb = new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
boolean flag = session.connection().
createStatement().execute(sql);
return flag;
} // end function
};// end callback
this.getHibernateTemplate().execute(cb);
} /**
* 返回List集合
*
* @param cls
* Class 查询对象
*
* @return List 查询的结果集
*/
public List<?> findListByCls(final Object cls) {
HibernateCallback cb = new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
Criteria cr = session.createCriteria(cls.getClass());
// cr.addOrder(Order.desc("addtime"));
return cr.list();
} // end function
};// end callback
return (List<?>) this.getHibernateTemplate().execute(cb);
} /**
* 返回List集合
*
* @param queryString
* String 查询语句 例如:"from User u where u.name='123'"
*
* @return List 查询的结果集
*/
public List<?> findListByQueryString(String queryString) {
return this.getHibernateTemplate().find(queryString);
} /**
* 通过ID获取某个对象值
*
* @param cls
* 对象
* @param id
* 对象ID
* @return Object 返回对应的值
*/
public Object findObjById(Object cls, Integer id) {
return this.getHibernateTemplate().get(cls.getClass(), id);
} public Object findObjById(Object cls, String id) {
return this.getHibernateTemplate().get(cls.getClass(), id);
} /** 150行
* 分页实现
*
* @param cls
* Class 查询对象
* @param perpage
* int 每页显示多少条
* @param current
* int 当前是第几页
* @return List 指定页面的 对象集合
*/
public List<?> findPaginatedList(final Object cls, final int perpage,
final int current) {
HibernateCallback cb = new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
Criteria cr = session.createCriteria(cls.getClass());
cr.setFirstResult(current * perpage);
cr.setMaxResults(perpage);
// cr.addOrder(Order.desc("addtime"));
return cr.list();
} // end function
};// end callback
return (List<?>) this.getHibernateTemplate().execute(cb);
} /**
* 动态查询的分页实现
*
* @param perpage
* int 每页显示多少条
* @param current
* int 当前是第几页
* @param condition
* String 所选择的查询条件
* @return List 指定页面的 对象集合
*/
public List<?> findPaginatedListAccording(final String tableName,
final int perpage, final int current, final String condition) {
HibernateCallback cb = new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
return session.createQuery(tableName + condition)
.setFirstResult(perpage * current)
.setMaxResults(perpage).list(); } // end function
};// end callback
return (List<?>) this.getHibernateTemplate().execute(cb);
} /**
* 插入一条数据到相应的表中
*
* @param obj
* 插入的对象
*/
public void insert(Object obj) {
this.getHibernateTemplate().save(obj);
} /**
* 插入多条数据到相应的表中
*
* @param ids
* ArrayList ID列表
* @return
*/
public abstract int insertRecords(ArrayList<?> datas);
public abstract List<?> executeSelectDistinctSql(final String sql); /**
* 通过ID删除一条信息
*
* @param id
* 对象ID
*/
public void removeById(Object id) {
this.getHibernateTemplate().delete(id);
} /**
* 通过多条数据的ID删除对应的数据
*
* @param ids
* ArrayList 数据ID列表
* @return
*/
public abstract int removeRecordsById(ArrayList<?> ids); /**
* 插入/修改相应数据.
*
* @param obj
* 插入/修改的对象
*/
public void saveOrUpdate(Object obj) {
this.getHibernateTemplate().saveOrUpdate(obj);
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub }}[/code]
<bean id="ckDao" class="com.kccbest.hrams.cnodc.dao.hibernate.CmCompetenceKnowledgeDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
package com.kccbest.hrams.cnodc.dao.hibernate;import java.util.ArrayList;
import java.util.List;import com.kccbest.hrams.cnodc.dao.BaseDAO;@SuppressWarnings("unchecked")
public class CmCompetenceKnowledgeDao extends BaseDAO { @Override
public int insertRecords(ArrayList<?> datas) {
// TODO Auto-generated method stub
return 0;
} @Override
public List<?> executeSelectDistinctSql(String sql) {
// TODO Auto-generated method stub
return null;
} @Override
public int removeRecordsById(ArrayList<?> ids) {
// TODO Auto-generated method stub
return 0;
}}继承的DAO
這個類也要。
package com.kccbest.hrams.cnodc.service.impl;import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;import com.kccbest.hrams.cnodc.dao.hibernate.KnowledgeDao;
import com.kccbest.hrams.cnodc.entity.CmKnowledge;
import com.kccbest.hrams.cnodc.service.KnowledgeService;public class KnowledgeServiceImpl implements KnowledgeService,Serializable { private static final long serialVersionUID = -262537323034874049L;
private KnowledgeDao knowledgeDao; @Override
public List<CmKnowledge> getAllCmKnowledge() {
return (List<CmKnowledge>)this.knowledgeDao.findListByCls(new CmKnowledge());
} @Override
public void executeCUDSql(String sql) {
this.knowledgeDao.executeCUDSql(sql);
} @Override
public void saveOrUpdate(Object obj) {
this.knowledgeDao.saveOrUpdate(obj);
} @Override
public Object findObjById(Object cls, Integer id) {
return this.knowledgeDao.findObjById(cls, id);
} @Override
public Object findObjById(Object cls, String id) {
return this.knowledgeDao.findObjById(cls, id);//38行
} @Override
public long countAccording(String sql, String condition) {
return this.knowledgeDao.countAccording(sql, condition);
} @Override
public List<CmKnowledge> getByGroupId(String group_id) {
return (List<CmKnowledge>)this.knowledgeDao.findObjById(new CmKnowledge(), group_id);
} @Override
public void insert(CmKnowledge knowledge) {
this.knowledgeDao.insert(knowledge);
} @Override
public List<CmKnowledge> findPaginatedList(int pageIndex, int pageCount) {
return (List<CmKnowledge>)this.knowledgeDao.findPaginatedList(new CmKnowledge(), pageCount, pageIndex -1);
} @Override
public Long getCountForKnowledgePageList() {
return this.knowledgeDao.count(new CmKnowledge());
} @Override
public List<?> getDistinctKnowledge(String sql) {
return this.knowledgeDao.executeSelectDistinctSql(sql);
} @Override
public int removeRecordsById(ArrayList<String> ids) {
return this.knowledgeDao.removeRecordsById(ids);
} @Override
public int insertRecords(CmKnowledge knowledge, ArrayList<CmKnowledge> datas) {
return this.knowledgeDao.insertRecords(datas);
} @Override
public List<?> findPaginatedListAccording(String tableName, int perpage,
int current, String condition) {
return this.knowledgeDao.findPaginatedListAccording(tableName, perpage, current - 1, condition);
} @Override
public void deleteByKnowledgeId(Object obj) {
this.knowledgeDao.removeById(obj);
} @Override
public List<?> executeSelectDistinctSql(String sql) {
return this.knowledgeDao.executeSelectDistinctSql(sql);
} @Override
public List executeSelectSql(String sql) {
return this.knowledgeDao.executeSelectSql(sql);
} @Override
public List<CmKnowledge> getByIds(String ids) {
return this.knowledgeDao
.executeSelectSql("from CmKnowledge where id in (" + ids + ")");
} public KnowledgeDao getKnowledgeDao() {
return knowledgeDao;
} public void setKnowledgeDao(KnowledgeDao knowledgeDao) {
this.knowledgeDao = knowledgeDao;
}}
<bean id="knowledgeDao" class="com.kccbest.hrams.cnodc.dao.hibernate.KnowledgeDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>刚那个spring部分发错。