wicketエラーの解決方法【その1】[Java][wicket][PropertyModel]

org.apache.wicket.WicketRuntimeException: No get method defined for class: class example.Example expression: Example

今回はこれです。今回はとか書きましたけど初めてエラーの解決方法書きます。
これはPropertyModelでの指定が間違ってるときに起きるエラーです。

先にPropertyModelって何それおいしいの?という人のために解説しときます。

Example.java

public class Example {

	String exampleString;
	int exampleint;

	public String getExampleString() {
		return exampleString;
	}

	public void setExampleString(String exampleString) {
		this.exampleString = exampleString;
	}

	public int getExampleint() {
		return exampleint;
	}

	public void setExampleint(int exampleint) {
		this.exampleint = exampleint;
	}

}

こんなファイルを定義したとします。このStringにフォームから入力された値を突っ込みたいという場合にPropertyModelを利用すると超便利です。超便利。二回いいました。

Modelの場合

Model<String> model = new Model<String>();
TextField<String> text = new TextField<String>("test", model);

こんな感じに書いてFormコンポーネントに追加してonSubmitに

Form<Void> form = new Form<Void>("form"){
			@Override
			protected void onSubmit() {
				super.onSubmit();
				String str = model.getObject();
				example.setExampleString(str);
			}
		};

"モデルに入れて→モデルから出して→入れる"という面倒なことをやってます。メンドクサイ!!

PropertyModelの場合

TextField<String> text = new TextField<String>("test", new PropertyModel<String>(example, "exampleString"));

これでおしまいです。簡単!モデルから取り出さなくてもexampleの中のexampleStringにはフォームに入力された文字は入ります。

new PropertyModel<変数名の型>(オブジェクト,"オブジェクトの中の変数名");

こんな感じです。今回で言えば、Exampleのexampleインスタンスを第一引数に、第二引数はExampleの中のexampleStringという変数を指定しています。ジェネリックスはexampleStringの型がStringなのでStringとします。

でエラーの解決です。
"オブジェクトの中の変数名"を誤って指定していると今回のエラーがでます。
例えば

TextField<String> text = new TextField<String>("test", new PropertyModel<String>(example, "Examplestring"));

と書くと変数名とExampleクラスに定義してあるexampleStringと一致しないのでエラーになるんですね。このエラーで二時間ぐらい悩みました!!!!