Nullpointerexception while setting a bean

后端 未结 1 849
灰色年华
灰色年华 2020-12-07 00:16

I have an action URL after clicking a hyper link like so

/SocialStupendous/GetProfile.action?slno=3&slno=3

In my execute m

相关标签:
1条回答
  • 2020-12-07 00:42

    The bean is not initialized. You should initialize it somehow in the action

    private ProfileBean bean = new ProfileBean(); 
    //and add getter ans setter
    

    the better approach, however is let the container to do it for you. You just need to create a bean configuration in the struts.xml

    <bean class="com.yourpackagename.ProfileBean" scope="default"/>
    

    then you would have

    private ProfileBean bean;
    
    @Inject
    public void setProfileBean(ProfileBean bean) {
      this.bean = bean;
    }
    

    and you don't need to parse request for parameters, this is already done by the params interceptor which is a part of defaultStack that your action should run. You should create properties in your action to hold parameter values.

    private Integer slno;
    
    public Integer getSlno() {
        return slno;
    }
    
    public void setSlno(Integer uslno) {
        this.slno = slno;
    }
    

    and the action will look like

    public String execute() {
    
       if (slno != null) {
         System.out.println(slno)
         bean.setUslno(slno);
       }
    
       ......
       return SUCCESS;
    }
    
    0 讨论(0)
提交回复
热议问题