NullPointerException while creating multicolumn table

I’ve read this topic and translate it on Java.
But I’ve got the nullPointerException in row

builder.moveTo(hCell.getFirstParagraph());

Here is the code with this line:

DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToBookmark("timelineTable");
Table myTable = (Table) builder.getCurrentNode().getAncestor(NodeType.TABLE);
for (int i = 0; i <3; i++)
{
    Cell hCell = (Cell) myTable.getFirstRow().getLastCell().deepClone(true);
    builder.moveTo(hCell.getFirstParagraph());
    builder.write("" + i);
}

I attached the template to this topic.
Why is the NPE happens? And what should I do?

Hi Alexey,
Thanks for your request. The problem occurs because you did not insert the cloned node into the document. That is why you cannot move DocumentBuidler cursor to this node. You should modify your code as shown below:

DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToBookmark("timelineTable");
Table myTable = (Table) builder.getCurrentNode().getAncestor(NodeType.TABLE);
for (int i = 0; i <3; i++)
{
    Cell hCell = (Cell) myTable.getFirstRow().getLastCell().deepClone(true);
    myTable.getFirstRow().appendChild(hCell);
    builder.moveTo(hCell.getFirstParagraph());
    builder.write("" + i);
}

If node is not in the document, you cannot mode DocumentBuidler cursor to this node.
Best regards,

Ok.
This lline I missed.
But now I have another error.
Here is my code

DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToBookmark("timelineTable");
Table myTable = (Table) builder.getCurrentNode().getAncestor(NodeType.TABLE);
for (int i = 0; i <3; i++)
{
    Cell hCell = (Cell) myTable.getLastRow().getFirstCell().deepClone(true);
    myTable.getFirstRow().appendChild(hCell);
    builder.moveTo(hCell.getFirstParagraph());
    builder.write("" + i);
}
for (int i = 0; i <3; i++)
{
    Cell bCell = (Cell) myTable.getRows().get(1).getLastCell().deepClone(true);
    // Insert cell into the second row
    myTable.getRows().get(1).appendChild(bCell);
    // Move document builder cursor to the cell
    builder.moveTo(bCell.getFirstParagraph());

    // If it is first column we should insert tableStart mergefield
    if (i == 0)
        builder.insertField(String.format("MERGEFIELD \"TableStart:{0}\"", "timelinePoints"), "");

    // Insert mergefield
    builder.insertField(String.format("MERGEFIELD \"{0}\"", "field" + i), "");

    // If column is last we should insert TableEnd
    if (i == 2)
        builder.insertField(String.format("MERGEFIELD \"TableEnd:{0}\"", "timelinePoints"), "");
}

In result document columns have various width. Where is an error?

Hi Alexey,
Thanks for your request. The problem occurs because preferred width of the table is specified. So the table cannot be wider than the preferred width even if there are more columns. To fix the problem you should just reset preferred width of the table in your template.
One more problem in your code, since you are using Java, the following syntax is not correct for formatting strings:

String.format("MERGEFIELD \"TableStart:{0}\"", "timelinePoints")

This is .Net syntax, in Java you should use:

String.format("MERGEFIELD \"TableStart:%s\"", "timelinePoints")

Best regards,

alexey.noskov:
Hi Alexey,
Thanks for your request. The problem occurs because preferred width of the table is specified. So the table cannot be wider than the preferred width even if there are more columns. To fix the problem you should just reset preferred width of the table in your template.
Best regards,

How can I reset prefered width of the table?

Hi
Thanks for your inquiry. Open your template in MS Word and reset this option in table properties. Please see the attached screenshot.
Also, if the problem will still occur, please attach your template document here.
Best regards,

Thanks.

Different width of column is explained that in first row I’ve cloned the first cell and for second row I’ve cloned last cell. And they have different width.

Hi Alexey,
It is perfect that you managed to fond the reason of the issue. Please let us know if you need more assistance, we will be glad to help you.
Best regards,