java.lang.NullPointerException 在 com.aspose.words.ëQF.ëZ
java.lang.NullPointerException at com.aspose.words.ëQF.ëZ
使用aspose-words-16.2.0-jdk16.jar
在 aspose word 文档保存
上出现以下异常
Word 文档 Link :
https://1drv.ms/w/s!AjRHZK-1fb7PjzH14y9bTaFO9jjK
在下面创建 class SampleApplicationTest
以保存文档:
Java代码:
public class AsposeTableTest {
public static void main(String[] args) {
try {
Document doc=new Document("D:/MultipleRequestResponse.docx");
createTableToReplace(doc,2,0);
doc.save("D:/SavedDoC.docx");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void createTableToReplace(Document sourceDocument, int noofCount)
throws Exception {
try{
Document doc = sourceDocument;
DocumentBuilder builder = new DocumentBuilder(doc);
Section section = doc.getFirstSection();
// Quick typed access to the Body child node of the Section.
Body body = section.getBody();
// Quick typed access to all Table child nodes contained in the
// Body.
TableCollection tables = body.getTables();
System.out.println(tables.getCount());
for (Table table : tables.toArray()) {
// Quick typed access to the first row of the table.
if (table.getFirstRow() != null) {
if (table.getFirstRow().toString(SaveFormat.TEXT).trim()
.equalsIgnoreCase("~Multiple~")) {
if (table.getNextSibling() != null) {
builder.moveTo(table.getNextSibling());
} else {
builder.moveToDocumentEnd();
}
for (int i = 0; i < noofCount; i++) {
Table table1 = builder.startTable();
for (int j = 0; j < table.getRows().getCount(); j++) {
Row row = (Row) table.getRows().get(j)
.deepClone(false);
table1.appendChild(row);
if (!"~Multiple~".equalsIgnoreCase(table.getRows()
.get(j).toString(SaveFormat.TEXT).trim())) {
for (int k = 0; k < table.getRows().get(j)
.getCells().getCount(); k++) {
Cell newcell = (Cell) table.getRows()
.get(j).getCells().get(k)
.deepClone(false);
row.getCells().add(newcell);
Paragraph para = new Paragraph(doc);
Run run = new Run(doc, table.getRows()
.get(j).getCells().get(k)
.toString(SaveFormat.TEXT).trim());
para.appendChild(run);
newcell.appendChild(para);
}
}
}
}
}
}
if (table.getFirstRow() != null) {
if (table.getFirstRow().toString(SaveFormat.TEXT).trim()
.equalsIgnoreCase("~Multiple~")) {
table.removeAllChildren();
}
}
}
}
catch(Exception e){
throw e;
}
}
}
异常堆栈跟踪:
java.lang.NullPointerException
at com.aspose.words.zzX.zzZ(Unknown Source)
at com.aspose.words.zzX.zzZ(Unknown Source)
at com.aspose.words.zzX.zzX(Unknown Source)
at com.aspose.words.zzZ1M.zzw(Unknown Source)
at com.aspose.words.zzZ1M.visitTableStart(Unknown Source)
at com.aspose.words.zzBB.visitTableStart(Unknown Source)
at com.aspose.words.Table.zzZ(Unknown Source)
at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
at com.aspose.words.Table.accept(Unknown Source)
at com.aspose.words.CompositeNode.acceptChildren(Unknown Source)
at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
at com.aspose.words.Body.accept(Unknown Source)
at com.aspose.words.CompositeNode.acceptChildren(Unknown Source)
at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
at com.aspose.words.Section.accept(Unknown Source)
at com.aspose.words.CompositeNode.acceptChildren(Unknown Source)
at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
at com.aspose.words.Document.accept(Unknown Source)
at com.aspose.words.zzBB.zzY(Unknown Source)
at com.aspose.words.Document.zzZ(Unknown Source)
at com.aspose.words.Document.save(Unknown Source)
at com.aspose.words.Document.save(Unknown Source)
at AsposeTableTest.main(AsposeTableTest.java:22)
注意:路径不为空,文档也不为空。
以上异常的原因是什么?
请帮我解决这个问题。
请使用以下修改方法解决此问题。
public static void createTableToReplace(Document sourceDocument, int noofCount)
throws Exception {
try{
Document doc = sourceDocument;
DocumentBuilder builder = new DocumentBuilder(doc);
Section section = doc.getFirstSection();
// Quick typed access to the Body child node of the Section.
Body body = section.getBody();
// Quick typed access to all Table child nodes contained in the
// Body.
TableCollection tables = body.getTables();
System.out.println(tables.getCount());
for (Table table : tables.toArray()) {
// Quick typed access to the first row of the table.
if (table.getFirstRow() != null) {
if (table.getFirstRow().toString(SaveFormat.TEXT).trim()
.equalsIgnoreCase("~Multiple~")) {
for (int i = 0; i < noofCount; i++) {
Table cloneTable = (Table)table.deepClone(true);
//Remove the first row of table if you want.
cloneTable.getFirstRow().remove();
table.getParentNode().insertAfter(cloneTable, table);
//table.getParentNode().insertAfter(new Paragraph((doc)), table);
}
}
}
if (table.getFirstRow() != null) {
if (table.getFirstRow().toString(SaveFormat.TEXT).trim()
.equalsIgnoreCase("~Multiple~")) {
for(Row row : table.getRows())
{
if(row.isFirstRow())
continue;
else
row.remove();
}
//table.remove();
}
}
}
}
catch(Exception e){
throw e;
}
}
我在 Aspose 工作,担任开发人员推广员。
使用aspose-words-16.2.0-jdk16.jar
在 aspose word 文档保存
上出现以下异常Word 文档 Link : https://1drv.ms/w/s!AjRHZK-1fb7PjzH14y9bTaFO9jjK
在下面创建 class SampleApplicationTest
以保存文档:
Java代码:
public class AsposeTableTest {
public static void main(String[] args) {
try {
Document doc=new Document("D:/MultipleRequestResponse.docx");
createTableToReplace(doc,2,0);
doc.save("D:/SavedDoC.docx");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void createTableToReplace(Document sourceDocument, int noofCount)
throws Exception {
try{
Document doc = sourceDocument;
DocumentBuilder builder = new DocumentBuilder(doc);
Section section = doc.getFirstSection();
// Quick typed access to the Body child node of the Section.
Body body = section.getBody();
// Quick typed access to all Table child nodes contained in the
// Body.
TableCollection tables = body.getTables();
System.out.println(tables.getCount());
for (Table table : tables.toArray()) {
// Quick typed access to the first row of the table.
if (table.getFirstRow() != null) {
if (table.getFirstRow().toString(SaveFormat.TEXT).trim()
.equalsIgnoreCase("~Multiple~")) {
if (table.getNextSibling() != null) {
builder.moveTo(table.getNextSibling());
} else {
builder.moveToDocumentEnd();
}
for (int i = 0; i < noofCount; i++) {
Table table1 = builder.startTable();
for (int j = 0; j < table.getRows().getCount(); j++) {
Row row = (Row) table.getRows().get(j)
.deepClone(false);
table1.appendChild(row);
if (!"~Multiple~".equalsIgnoreCase(table.getRows()
.get(j).toString(SaveFormat.TEXT).trim())) {
for (int k = 0; k < table.getRows().get(j)
.getCells().getCount(); k++) {
Cell newcell = (Cell) table.getRows()
.get(j).getCells().get(k)
.deepClone(false);
row.getCells().add(newcell);
Paragraph para = new Paragraph(doc);
Run run = new Run(doc, table.getRows()
.get(j).getCells().get(k)
.toString(SaveFormat.TEXT).trim());
para.appendChild(run);
newcell.appendChild(para);
}
}
}
}
}
}
if (table.getFirstRow() != null) {
if (table.getFirstRow().toString(SaveFormat.TEXT).trim()
.equalsIgnoreCase("~Multiple~")) {
table.removeAllChildren();
}
}
}
}
catch(Exception e){
throw e;
}
}
}
异常堆栈跟踪:
java.lang.NullPointerException
at com.aspose.words.zzX.zzZ(Unknown Source)
at com.aspose.words.zzX.zzZ(Unknown Source)
at com.aspose.words.zzX.zzX(Unknown Source)
at com.aspose.words.zzZ1M.zzw(Unknown Source)
at com.aspose.words.zzZ1M.visitTableStart(Unknown Source)
at com.aspose.words.zzBB.visitTableStart(Unknown Source)
at com.aspose.words.Table.zzZ(Unknown Source)
at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
at com.aspose.words.Table.accept(Unknown Source)
at com.aspose.words.CompositeNode.acceptChildren(Unknown Source)
at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
at com.aspose.words.Body.accept(Unknown Source)
at com.aspose.words.CompositeNode.acceptChildren(Unknown Source)
at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
at com.aspose.words.Section.accept(Unknown Source)
at com.aspose.words.CompositeNode.acceptChildren(Unknown Source)
at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
at com.aspose.words.Document.accept(Unknown Source)
at com.aspose.words.zzBB.zzY(Unknown Source)
at com.aspose.words.Document.zzZ(Unknown Source)
at com.aspose.words.Document.save(Unknown Source)
at com.aspose.words.Document.save(Unknown Source)
at AsposeTableTest.main(AsposeTableTest.java:22)
注意:路径不为空,文档也不为空。
以上异常的原因是什么?
请帮我解决这个问题。
请使用以下修改方法解决此问题。
public static void createTableToReplace(Document sourceDocument, int noofCount)
throws Exception {
try{
Document doc = sourceDocument;
DocumentBuilder builder = new DocumentBuilder(doc);
Section section = doc.getFirstSection();
// Quick typed access to the Body child node of the Section.
Body body = section.getBody();
// Quick typed access to all Table child nodes contained in the
// Body.
TableCollection tables = body.getTables();
System.out.println(tables.getCount());
for (Table table : tables.toArray()) {
// Quick typed access to the first row of the table.
if (table.getFirstRow() != null) {
if (table.getFirstRow().toString(SaveFormat.TEXT).trim()
.equalsIgnoreCase("~Multiple~")) {
for (int i = 0; i < noofCount; i++) {
Table cloneTable = (Table)table.deepClone(true);
//Remove the first row of table if you want.
cloneTable.getFirstRow().remove();
table.getParentNode().insertAfter(cloneTable, table);
//table.getParentNode().insertAfter(new Paragraph((doc)), table);
}
}
}
if (table.getFirstRow() != null) {
if (table.getFirstRow().toString(SaveFormat.TEXT).trim()
.equalsIgnoreCase("~Multiple~")) {
for(Row row : table.getRows())
{
if(row.isFirstRow())
continue;
else
row.remove();
}
//table.remove();
}
}
}
}
catch(Exception e){
throw e;
}
}
我在 Aspose 工作,担任开发人员推广员。