ここにピクルスが少しあります。私はJSONをZipファイルから読み込んでいます。私はJSONの内容をVaadinのテーブルに書きたいと思います。Java - Vaadin - テーブルがいっぱいでない
ここに私の物を読んでテーブルを埋める機能があります、これはJavaです。
private void getJsonContent() {
try {
FileInputStream fis = new FileInputStream(backupFile);
ZipInputStream zin = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
byte[] buffer = new byte[1024];
while((entry = zin.getNextEntry()) != null) {
if(entry.getName().equalsIgnoreCase("content.json")) {
int n;
while((n = zin.read(buffer, 0, 1024)) > -1){
String JSON = new String(buffer, Charset.forName("UTF-8"));
JSONObject obj = new JSONObject(JSON);
logger.info(JSON);
// Assign "global" Values to Variables
this.createdAt = obj.getString("created_at");
this.version = obj.getString("version");
// Fill table if applicable
for(int i = 0; i < obj.getJSONArray("content").length(); i++) {
JSONObject sub = obj.getJSONArray("content").getJSONObject(i);
logger.info(sub);
infoTable.addItem(new Object[] {
sub.get("imported_identities").toString(),
sub.get("project_versions").toString(),
sub.get("last_import").toString(),
sub.get("client").toString(),
sub.get("project").toString()
}, i +1);
}
}
}
}
zin.close();
fis.close();
} catch (FileNotFoundException e) {
// Can't happen here
} catch (IOException e) {
logger.info("Can't read File.");
} catch (JSONException jse) {
logger.info("JSON Content could not be read: " + jse.getMessage());
}
}
あなたは、私は、関数呼び出しlogger.info(sub)
持って気づくでしょう - 私は別の有効なJSONオブジェクト(私が読んでいるファイルは、ネストされたものが含まれている)で何を得ることを確認するために
出力:
{"imported_identities":0,"project_versions":0,"last_import":null,"client":"Client1","project":"Project2"}
{"imported_identities":0,"project_versions":0,"last_import":null,"client":"Client2","project":"Project1"}
{"imported_identities":0,"project_versions":1,"last_import":"2016-09-14T09:28:24.520Z","client":"Client1","project":"Project1"}
すべての値が正しいことを確認しました(デフォルトではテーブルはnullで作成されています) - ここにテーブルプロパティがあります:
infoTable.addContainerProperty(impIds, String.class, null);
infoTable.addContainerProperty(projVe, String.class, null);
infoTable.addContainerProperty(lstImp, String.class, null);
infoTable.addContainerProperty(client, String.class, null);
infoTable.addContainerProperty(projct, String.class, null);
infoTable.setColumnCollapsingAllowed(true);
infoTable.setColumnCollapsed(impIds, true);
infoTable.setColumnCollapsed(projVe, true);
infoTable.setColumnCollapsed(lstImp, true);
最後に、テーブルに「refreshRowCache」が呼び出されました。誰もが問題を参照してください?エラーには何もテーブルがちょうど(infoTable.getItemIds().size()
のサイズは、右の呼び出し後に0で項目を追加しません、ありません
はEDIT:。
私が確認するために、以下のことを試してみました
infoTable.addItem(i + 1);
infoTable.getItem(i + 1).getItemProperty(impIds).setValue(sub.get("imported_identities").toString());
infoTable.getItem(i + 1).getItemProperty(projVe).setValue(sub.get("project_versions").toString());
。
これは行ってNullPointerExceptionが、スタックトレースは、しかし、私の知る限り見ることができるように私のクラスのいずれかが含まれていません原因
最初の手順として、行の各セルのメンバーを含むPOJOクラスを設定します。 JSONのデータを含むPOJOにJavaコンテナを正常に埋め込んだ後、コンテナ(BeanItemContainerなど)を使用してそのコンテナをテーブルにバインドします。 'table.setContainerDataSource(new BeanItemContainer <>(POJO.class、yourListOfPojos))' –
気にしないでください。長すぎた。私のテーブルは、それを埋める前に適切に初期化されていないため、予期しないnullpointersが発生します。残りのコードは動作します。 – Eskir