ognl.OgnlException:对于getProperty(null,“0”)错误,source为null

时间:2013-10-29 01:05:52

标签: java jsp struts2 iterator ognl

我可以显示值并编辑它们,但是当我提交表单时,它会遇到以下错误。

爪哇

public Class Baskets {
  private Basket basket;
  ...

  public String edit(){
    System.err.println("item>>>" + this.basket.getItems().get(0).getProduct().getPrice());
    ...
  }
  getter and setters
}

JSP

 <form name="edit" method="POST" action="edit">
      <table border="4">
        <thead><td>Product</td><td>Price 2</td><td>User</td><td>Time</td></thead>  
        <tbody>
           <s:iterator value="basket.items" var="item" status="cur">
             <tr>
                <s:textfield type="hidden" id="item[%{#cur.index}].id" name="item[%{#cur.index}].id" value="%{id}" theme="simple"/>
                  <td>

                      Price : <s:textfield id="item[%{#cur.index}].product.price" name="item[%{#cur.index}].product.price" value="%{prodcut.price}" theme="simple"/>
                      <br/>
                      Name: <s:label value="%{product.name}" theme="simple"/> 

                  </td>

                  <td>
                      <s:textfield name="item[%{#cur.index}].product.price2" 
                                   id="item[%{#cur.index}].product.price2" 
                                   value="%{product.price2}" theme="simple"/>
                  </td>

                  <td><s:label value="%{name}" theme="simple"/> </td>
                  <td><s:label value="%{time}" theme="simple"/> </td>

            </tr>
         </s:iterator>
       </tbody>
       </table>        
    <input id="edit" type="submit" name="action" value="Edit"/>
 </form>

控制台错误

   ognl.OgnlException: source is null for getProperty(null, "0")

浏览器错误

java.lang.NullPointerException

com.myproject.controller.Baskets.edit(Baskets.java:100)

第100行如下

 System.err.println("item>>>" + this.basket.getItems().get(0).getProduct().getPrice());

HTML

 <form name="edit" method="POST" action="edit">
    <table border="4">
           <thead><td>Product</td><td>Price 2</td><td>User</td><td>Time</td></thead>
                <tbody>
                  <tr>
                    <input type="hidden" name="item[0].id" value="16" id="item[0].id"/>
                    <td>Price : <input type="text" name="item[0].product.price"    
                                   value="55" id="item[0].product.price"/>
                        <br/>
                       Name: <label id="">Product 1</label> </td>

                   <td><input type="text" name="item[0].product.price2" value="1000" 
                                          id="item[0].product.price2"/></td>

                            <td><label id="">User1</label> </td>
                            <td><label id="">9:31:03 AM</label> </td>

                  </tr>

                <tr>  
                   <input type="hidden" name="item[1].id" value="17" id="item[1].id"/>
                   <td>Price : <input type="text" name="item[1].product.price" 
                                  value="60" id="item[1].product.price"/>
                    <br/>
                                Name: <label id="">Product 2</label> </td>

                   <td><input type="text" name="item[1].product.price2" value="1000" 
                             id="item[1].product.price2"/></td>

                            <td><label id="">User1</label> </td>
                            <td><label id="">11:25:55 AM</label> </td>

                </tr>

                </tbody>
            </table>
            <label id="balance"></label>        
            <input id="edit" type="submit" name="action" value="Edit"/>
        </form>

1 个答案:

答案 0 :(得分:1)

<s:textfield id="item[ {#cursor.index}].product.price" 
                               name="item[%{#cursor.index}].product.price" 
                               value="%{product.price}" /> 

似乎错过%basket.item,请更正

<s:textfield id="basket.item[%{#cursor.index}].product.price" 
                               name="basket.item[%{#cursor.index}].product.price" 
                               value="%{product.price}" />