hkk
hkk

Reputation: 45

I have a NullPointerException report in Struts 2

struts.xml:

<package name="backpower" namespace="/backpower" extends="struts-default">
    <default-interceptor-ref name="defaultStack"></default-interceptor-ref>
    <!-- backup battery manager -->
    <action name="backpower" class="com.cps.backpower.action.BackpowerAction">
        <result name="addBattery">/WEB-INF/views/backpower/info/addBattery.jsp</result>
        <result name="editBattery">/WEB-INF/views/backpower/info/editBattery.jsp</result>
        <result>/WEB-INF/views/backpower/info/list.jsp</result>
    </action>
</package>

add.jsp:

    <div class="form-actions">
        <button name="method:save" type="submit" class="btn btn-primary">
            save
        </button>
        <button type="reset" class="btn">
            reset
        </button>
    </div>

my BackpowerAction class

public class BackpowerAction extends BaseManageAction{

private static final long serialVersionUID=1L;
public BackpowerService backpowerService;
public PaginationSupport backpowerPagination;
public PageTip pageTip;
public BackpowerInfo backpowerInfo;
public Long id;


public String execute() throws Exception{
    return search();
}

public String search(){
    String hql="from BackpowerInfo where 1=1";
    backpowerPagination=backpowerService.getPage(hql,getPageNo(),getPageSize());
    return SUCCESS;
}

public void setBackpowerInfo(BackpowerInfo backpowerInfo){
    this.backpowerInfo=backpowerInfo;
}

public String add(){
    return "addBattery";
}

public String save() {
    if (backpowerInfo.getId() == null) {
        try {
            backpowerService.save(backpowerInfo);
            return SUCCESS;
        } catch (Exception e) {
            return add();
        }
    } else {
        try {
            backpowerService.update(backpowerInfo.getId(),backpowerInfo.getName(),backpowerInfo.getLocation(),backpowerInfo.getManager());
            pageTip.setOk(true);
            pageTip.setTip("修改用户信息成功");
            return search();
        } catch (Exception e) {
            e.printStackTrace();
            pageTip.setOk(false);
            pageTip.setTip("修改用户信息失败");
            return edit();
        }
    }

}

public String edit() {
    backpowerInfo=backpowerService.findById(getId());

    return "editBattery";
}

public String del() throws Exception {
    try {
        backpowerService.delete(getId());
        pageTip.setOk(true);
        pageTip.setTip("删除成功");
    } catch (Exception e) {
        pageTip.setOk(false);
        pageTip.setTip("删除失败");
    }
    return search();
}

public BackpowerInfo getBackpowerInfo(){
    return backpowerInfo;
}




public PaginationSupport getBackpowerPagination(){

    return backpowerPagination;
}

public void setBackpowerPagination(PaginationSupport backpowerPagination){
    this.backpowerPagination=backpowerPagination;
}

public BackpowerService getBackpowerService(){
    return backpowerService;
}

public void setBackpowerService(BackpowerService backpowerService){
    this.backpowerService=backpowerService;
}

public PageTip getPageTip(){
    return pageTip;
}

public void setPageTip(PageTip pageTip){
    this.pageTip=pageTip;
}

public long getId(){
    return id;
}

public void setId(long id){
    this.id=id;
}
}

java.lang.NullPointerException
com.cps.backpower.action.BackpowerAction.save(BackpowerAction.java:39)

When I click save button on the web page, it gives Struts2 problem report, where am I wrong, how should I correct it?

Upvotes: 0

Views: 354

Answers (3)

hkk
hkk

Reputation: 45

name of the instance object of the model BackpowerInfo in BackpowerAction should be same with the name used in add.jsp

Upvotes: 0

Ankur Lathi
Ankur Lathi

Reputation: 7836

I think backpowerInfo is null in your code.

You haven't populated or instantiated it before calling getters.

Upvotes: 1

Roman C
Roman C

Reputation: 1

I thought backpowerService is null, after counting lines in your code. You should inject service before use.

I saw there's a setter for backpowerService but it should be called before any action is executed. I don't know which way you instantiate a BackpowerService bean, which container and DI. But you don't do it with interceptor, as your configuration doesn't reflect it.

My suggestions is to use some DI framework and place your service bean there and use @Inject on setter or field to inject the service as a dependency, i.e.

@Inject
public void setBackpowerService(BackpowerService backpowerService){
    this.backpowerService=backpowerService;
}

Upvotes: 0

Related Questions