• <tfoot id='zhml7'></tfoot>
      <bdo id='zhml7'></bdo><ul id='zhml7'></ul>

    <legend id='zhml7'><style id='zhml7'><dir id='zhml7'><q id='zhml7'></q></dir></style></legend>
    1. <i id='zhml7'><tr id='zhml7'><dt id='zhml7'><q id='zhml7'><span id='zhml7'><b id='zhml7'><form id='zhml7'><ins id='zhml7'></ins><ul id='zhml7'></ul><sub id='zhml7'></sub></form><legend id='zhml7'></legend><bdo id='zhml7'><pre id='zhml7'><center id='zhml7'></center></pre></bdo></b><th id='zhml7'></th></span></q></dt></tr></i><div id='zhml7'><tfoot id='zhml7'></tfoot><dl id='zhml7'><fieldset id='zhml7'></fieldset></dl></div>

        <small id='zhml7'></small><noframes id='zhml7'>

      1. 设置 bean 时出现 Nullpointerexception

        时间:2023-09-25

        • <i id='vcoSB'><tr id='vcoSB'><dt id='vcoSB'><q id='vcoSB'><span id='vcoSB'><b id='vcoSB'><form id='vcoSB'><ins id='vcoSB'></ins><ul id='vcoSB'></ul><sub id='vcoSB'></sub></form><legend id='vcoSB'></legend><bdo id='vcoSB'><pre id='vcoSB'><center id='vcoSB'></center></pre></bdo></b><th id='vcoSB'></th></span></q></dt></tr></i><div id='vcoSB'><tfoot id='vcoSB'></tfoot><dl id='vcoSB'><fieldset id='vcoSB'></fieldset></dl></div>

              <legend id='vcoSB'><style id='vcoSB'><dir id='vcoSB'><q id='vcoSB'></q></dir></style></legend>
              <tfoot id='vcoSB'></tfoot>

                  <bdo id='vcoSB'></bdo><ul id='vcoSB'></ul>

                  <small id='vcoSB'></small><noframes id='vcoSB'>

                    <tbody id='vcoSB'></tbody>
                  本文介绍了设置 bean 时出现 Nullpointerexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  点击这样的超链接后,我有一个操作 URL

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

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

                  在我的 ActionClassexecute 方法中,我有以下代码

                  In my execute method of ActionClass I have the following code

                  public String execute() {
                    int urislno=Integer.parseInt(getServletRequest().getParameter("slno"));
                    System.out.println(urislno);
                    bean.setUslno(urislno);       
                  }
                  

                  我在执行 bean.setuslno(urislno) 时收到 NullPointerException.即使 urislno 被正确打印为 3.

                  I am getting NullPointerException when I am performing bean.setuslno(urislno). Even though urislno is printed properly as 3.

                  ProfileBean 类:

                  ProfileBean class:

                  public class ProfileBean {
                  
                    private int uslno;
                  
                    public int getUslno() {
                      return uslno;
                    }
                  
                    public void setUslno(int uslno) {
                      this.uslno = uslno;
                    }
                  }
                  

                  为什么会这样?

                  推荐答案

                  bean 未初始化.你应该在动作中以某种方式初始化它

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

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

                  然而,更好的方法是让容器为你做这件事.您只需要在 struts.xml

                  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"/>
                  

                  那么你就会有

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

                  并且您不需要解析参数请求,这已经由 params 拦截器完成,它是您的操作应该运行的 defaultStack 的一部分.您应该在您的操作中创建属性来保存参数值.

                  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;
                  }
                  

                  动作看起来像

                  public String execute() {
                  
                     if (slno != null) {
                       System.out.println(slno)
                       bean.setUslno(slno);
                     }
                  
                     ......
                     return SUCCESS;
                  }
                  

                  这篇关于设置 bean 时出现 Nullpointerexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何通过 Struts 2 中的代码动态设置语言环境 下一篇:双击提交按钮时如何避免两次插入两条相同的记录?

                  相关文章

                • <tfoot id='LSTt3'></tfoot>

                    <small id='LSTt3'></small><noframes id='LSTt3'>

                  1. <i id='LSTt3'><tr id='LSTt3'><dt id='LSTt3'><q id='LSTt3'><span id='LSTt3'><b id='LSTt3'><form id='LSTt3'><ins id='LSTt3'></ins><ul id='LSTt3'></ul><sub id='LSTt3'></sub></form><legend id='LSTt3'></legend><bdo id='LSTt3'><pre id='LSTt3'><center id='LSTt3'></center></pre></bdo></b><th id='LSTt3'></th></span></q></dt></tr></i><div id='LSTt3'><tfoot id='LSTt3'></tfoot><dl id='LSTt3'><fieldset id='LSTt3'></fieldset></dl></div>

                    <legend id='LSTt3'><style id='LSTt3'><dir id='LSTt3'><q id='LSTt3'></q></dir></style></legend>
                      <bdo id='LSTt3'></bdo><ul id='LSTt3'></ul>