I have a get some array of inputs from user, say years[], and process the values in my controller.
Here is my code:
Controller:
The controller code tries to bind the form data. In my case, get the array of years value from the html form:
Form<FinStatementInput> ipForm = Form.form(FinStatementInput.class).bindFromRequest();//Array out of bound exception is thrown at this line.
if (ipForm.hasErrors()) {
return badRequest("<h1>error</h1>").as("text/html");
} else {
FinStatementInput ip = ipForm.get();
System.out.println("first year input(to test)>>"+ ip.years[0]);
return ok();
}
FinStatementInput
Model:
public int[] years;//array declaration
.
.
FinStatmentInput (int[] years) {
this.years = years; // in the constructor
}
HTML:
<form id="Start Here" name="Start Here" style="display: none;"
action="@routes.Application.calculate()" method="post">
<table class="table table-condensed">
<tr>
<td id="tdInput" >
<div class="input-group">
<span class="input-group-addon">Year 1</span> <input name="years[0]" id="GreenInput"
pattern="[0-9.]+" type="text" class="form-control" required="required">
</div></td>
<td id="tdInput" >
<div class="input-group">
<span class="input-group-addon">Year 2</span> <input name="years[1]" id="GreenInput"
pattern="[0-9.]+" type="text" class="form-control" required="required">
</div></td>
<td id="tdInput" >
<div class="input-group">
<span class="input-group-addon">Year 3</span> <input name="years[2]" id="GreenInput"
pattern="[0-9.]+" type="text" class="form-control" required="required">
</div></td>
<td id="tdInput" >
<div class="input-group">` ... like this input fields for all years needed ,say 10 years.
I get this run time exception:
[InvalidPropertyException: Invalid property 'years[1]' of bean class [models.FinStatementInput]: Invalid array index in property path 'years[5]'; nested exception is java.lang.**ArrayIndexOutOfBoundsException**]
I have not specified the size of array anywhere explicitly in the code; Just declared empty array of years and trying to bind bunch of years from user form. I want to access all the array element of years, say, from 1 to 10. But I don't know where is the mistake that leads to ArryOutOfBound Exception. Any help is appreciated.