Search code examples
javastruts2nullpointerexceptionstruts

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?


Solution

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