0

年[]など、ユーザーから入力の配列を取得し、コントローラーで値を処理します。

これが私のコードです:

コントローラー:
コントローラー コードは、フォーム データをバインドしようとします。私の場合、html フォームから年の値の配列を取得します。

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 

モデル:

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.

この実行時例外が発生します。

 [InvalidPropertyException: Invalid property 'years[1]' of bean class [models.FinStatementInput]: Invalid array index in property path 'years[5]'; nested exception is java.lang.**ArrayIndexOutOfBoundsException**]

コードのどこにも配列のサイズを明示的に指定していません。年の空の配列を宣言し、ユーザーフォームから一連の年をバインドしようとしました。たとえば、1 から 10 までのすべての年の配列要素にアクセスしたいのですが、ArryOutOfBound Exception につながる間違いがどこにあるのかわかりません。どんな助けでも大歓迎です。

4

1 に答える 1

0

間違いはモデルクラスの配列宣言にあります。以下の行のように変更するとうまくいきます。配列は宣言されていますが、インスタンス化されていません。#ばかげた間違い。

public int[] years = new int[10];
于 2014-09-18T21:44:18.463 に答える