问题描述:

I am able to show values and edit them but when I submit the form it runs into following errors.

Java

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>

Error in console

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

Error on browser

java.lang.NullPointerException

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

Line 100 is as following

 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>

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

Seems to miss % and basket.item, please correct to

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