0

テーブルの配列 (2 つのテーブル) を作成しようとしています。私のプログラムは、nullpointer 例外で最後の行で停止します。理由はありますか?

com.lowagie.text.pdf.PdfPTable[] table = new com.lowagie.text.pdf.PdfPTable[1];
// the cell object
com.lowagie.text.pdf.PdfPCell cell;
// header

cell = new PdfPCell(new Phrase(wdComponentAPI.getMessage("Ordernr")));
cell.setColspan(1);
cell.setBackgroundColor(Color.LIGHT_GRAY);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table[0].addCell(cell);
4

3 に答える 3

1

作成して配列しましたが、中には何もありません。配列フィールド "table[0]" に null が含まれています。

次のようなオブジェクトを追加します。

tables[0] = new PdfPTable();

または、正確に必要なものに応じて、PdfPTable() とは異なるコンストラクターを使用します

これが更新されたコードです

com.lowagie.text.pdf.PdfPTable[] table = new com.lowagie.text.pdf.PdfPTable[1];
tables[0] = new PdfPTable();
// the cell object
com.lowagie.text.pdf.PdfPCell cell;
// header

cell = new PdfPCell(new Phrase(wdComponentAPI.getMessage("Ordernr")));
cell.setColspan(1);
cell.setBackgroundColor(Color.LIGHT_GRAY);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table[0].addCell(cell);
于 2014-04-25T07:35:57.083 に答える
1

作成されたオブジェクトで配列が作成されない

PdfPTable [] tables = new PdfPTable[1];
tables[0].doStuff() // null pointer

配列内にオブジェクトを作成します

PdfPTable [] tables = new PdfPTable[1];
tables[0] = new PdfPTable();
tables[0].doStuff() // works good!
于 2014-04-25T07:36:47.070 に答える
1

com.lowagie.text.pdf.PdfPTable[] table = new com.lowagie.text.pdf.PdfPTable[1];

長さ 1 の配列を作成するだけで、table[0]まだnullこの時点です。オブジェクトを使用する前に、オブジェクトを作成して割り当てる必要があります

table[0] = new PdfPTable(1); // create a PDFTable with one column
于 2014-04-25T07:37:28.583 に答える