解决this.getHibernateTemplate().save();报空指针异常问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39533140/article/details/78264949

接上篇博文
在整合ssh写了一个小例子的时候出现了这个问题:
代码如下
package com.crm.biz;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.crm.info.Cuser;

@Transactional
@Service(“cuserBiz”)
public class CuserBiz extends HibernateDaoSupport{

public void add(Cuser cuser){
    System.out.println(cuser.getCrm_name());
    System.out.println("--------------");
    try{
        this.getHibernateTemplate().save(cuser);
    }catch(Exception e){
        e.printStackTrace();
    }
}

}

我在运行的时候this.getHibernateTemplate().save(cuser);这一块报指针异常的情况;后来经过长时间的调试,终于找到了问题所在;
一开始我在Action中是这样创建的CuserBiz类,代码如下
package com.crm.action;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.crm.biz.CuserBiz;
import com.crm.info.Cuser;
import com.opensymphony.xwork2.ActionSupport;

@Scope(“prototype”)
@Controller(“cuserAction”)
public class CuserAction extends ActionSupport{
/**
*
*/

private static final long serialVersionUID = 1L;
private Cuser cuser;

public Cuser getCuser() {
    return cuser;
}

public void setCuser(Cuser cuser) {
    this.cuser = cuser;
}

public String add(){
    System.out.println(userBiz);
    System.out.println(cuser.getCrm_name());
    new CuserBiz().add(cuser);
    return "cc";
}

}

直接new的CuserBIz对象,这样一来创建对象就不经过spring管理,所以创建的一个新的对象,我们都知道spring配置文件中将sessionFactory注入给HibernateDaoSupport,然后就可以通过getHibernateTemplate来获得HibernateTemplate;
但是上面调用的add方法的时候直接是new的CuserBiz对象故没有依赖注入,所以自然而然的this.getHibernateTemplate().save(cuser);会报空指针错误;
只要把new CuserBiz改成CuserBiz userBiz写上set方法就可以了;如下
package com.crm.action;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.crm.biz.CuserBiz;
import com.crm.info.Cuser;
import com.opensymphony.xwork2.ActionSupport;

@Scope(“prototype”)
@Controller(“cuserAction”)
public class CuserAction extends ActionSupport{
/**
*
*/

private static final long serialVersionUID = 1L;
private Cuser cuser;
private CuserBiz userBiz;

public void setCuserBiz(CuserBiz cuserBiz) {
    this.userBiz = cuserBiz;
}

public Cuser getCuser() {
    return cuser;
}

public void setCuser(Cuser cuser) {
    this.cuser = cuser;
}

public String add(){
    System.out.println(userBiz);
    System.out.println(cuser.getCrm_name());
    userBiz.add(cuser);
    return "cc";
}

}
此也是我看到另一片博文才想到的,链接如下:
http://blog.csdn.net/gladiator_wyj/article/details/54915403
写此篇博文希望对大家有所帮助,也给自己的一个记录吧。

展开阅读全文

this.getHibernateTemplate() 空指针

03-13

[code="java"]rn public Manuscript selectByMaid(int id) throws Exception rn Manuscript manuscript = (Manuscript) this.getHibernateTemplate().get(rn Manuscript.class, id);rn return manuscript;rn rn[/code]rnrn这段代码是运行成功的rnrn[code="java"]rn public void exportMan(int maid, String name, OutputStream os) throws Exceptionrn Manuscript m = (Manuscript) this.getHibernateTemplate().get(rn Manuscript.class, maid);rn rn[/code]rn这两个方法是属于同一class里面的 但是报下列错误rnrn[code="java"]rn java.lang.NullPointerExceptionrn at com.kinrich.mediaStorage.daoimp.ManuscriptDaoImp.exportMan(ManuscriptDaoImp.java:99)rn at com.kinrich.mediaStorage.action.ManuscriptAction.getDownloadFile(ManuscriptAction.java:266)rn at org.apache.jsp.templates.downinfo_jsp._jspService(downinfo_jsp.java:93)rn at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)rn at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)rn at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)rn at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)rn at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at com.kinrich.mediaStorage.Interceptor.CheckInterceptor.doFilter(CheckInterceptor.java:38)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)rn at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)rn at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)rn at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)rn at java.lang.Thread.run(Thread.java:722)rn rn[/code]rnrnrnrn请问这个问题是出在哪里 我调试的时候显示的是this.getHibernateTemplate()为空 但是同一类里面 为什么会有两个结果呢? 问答

没有更多推荐了,返回首页