Java ooxml模式、IntelliJ IDEA和NullPointerException
我试图使用某些需要ooxml模式jar的函数,即使在通过Maven导入poi ooxml库和ooxml模式库之后,我仍然在第13行得到一个NullPointerException。我正在使用IntelliJ IDEA 2017Java ooxml模式、IntelliJ IDEA和NullPointerException,java,apache,intellij-idea,apache-poi,openxml,Java,Apache,Intellij Idea,Apache Poi,Openxml,我试图使用某些需要ooxml模式jar的函数,即使在通过Maven导入poi ooxml库和ooxml模式库之后,我仍然在第13行得到一个NullPointerException。我正在使用IntelliJ IDEA 2017 import java.io.*; import java.math.BigInteger; import org.apache.poi.util.Units; import org.apache.poi.wp.usermodel.HeaderFooterType; im
import java.io.*;
import java.math.BigInteger;
import org.apache.poi.util.Units;
import org.apache.poi.wp.usermodel.HeaderFooterType;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
public class ASM {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
FileOutputStream out = new FileOutputStream(new File("AASM.docx"));
CTSectPr sectPr = document.getDocument().getBody().getSectPr();
CTPageSz pageSz = sectPr.getPgSz();
double pageWidth = pageSz.getW().doubleValue();
CTPageMar pageMar = sectPr.getPgMar();
double pageMarginLeft = pageMar.getLeft().doubleValue();
double pageMarginRight = pageMar.getRight().doubleValue();
double effectivePageWidth = pageWidth - pageMarginLeft - pageMarginRight;
//Header
XWPFHeader header = document.createHeader(HeaderFooterType.DEFAULT);
XWPFTable headerTable = header.createTable(1, 3);
CTTblWidth width = headerTable.getCTTbl().addNewTblPr().addNewTblW();
width.setType(STTblWidth.DXA);
width.setW(new BigInteger(effectivePageWidth + ""));
XWPFTableRow headerTableRowOne = headerTable.getRow(0);
//Cell 0
XWPFTableCell companyCell = headerTableRowOne.getCell(0);
XWPFParagraph companyParagraph = companyCell.addParagraph();
XWPFRun companyRun = companyParagraph.createRun();
InputStream companyImageInputStream = new BufferedInputStream(new FileInputStream("20opy.png"));
companyRun.addPicture(companyImageInputStream, Document.PICTURE_TYPE_PNG, "20opy.png", Units.toEMU(125), Units.toEMU(19));
//Main Document
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("Hello world");
run.addPicture(companyImageInputStream, Document.PICTURE_TYPE_PNG, "20opy.png", Units.toEMU(125), Units.toEMU(19));
document.write(out);
out.close();
System.out.println("Finished");
}
}
NullPointerException
不能由缺少库引起。如果一个对象指向NULL
,但代码试图以某种方式使用该对象,则会发生NPE
在您的例子中,如果sectPr.getPgSz()
抛出NPE
,那么sectPr
是null
,因此null.getPgSz()
抛出NPE
为什么sectPr
为null
?这是因为document.getDocument().getBody().getSectPr()
返回了null
。这是因为使用XWPFDocument document=new XWPFDocument()创建了一个新的XWPFDocument
代码>未设置任何节属性。当涉及到部分和/或页面设置时,它依赖于wordprocessing应用程序的默认值
很明显,您始终需要检查文档中是否已有CTSectPr
。只有在它们已经存在的情况下,你才能使用它们。否则需要使用addnewspecpr
创建它们
获取页面设置的目的似乎是将表的宽度设置为effectivePageWidth
。但是,由于新创建的XWPFDocument
没有设置任何节属性,因此需要首先设置它们,而不是尝试获取不存在的部分
请参阅我使用的代码示例CTSectPr
:ANullPointerException
不能由缺少库引起。如果一个对象指向NULL
,但代码试图以某种方式使用该对象,则会发生NPE
在您的例子中,如果sectPr.getPgSz()
抛出NPE
,那么sectPr
是null
,因此null.getPgSz()
抛出NPE
为什么sectPr
为null
?这是因为document.getDocument().getBody().getSectPr()
返回了null
。这是因为使用XWPFDocument document=new XWPFDocument()创建了一个新的XWPFDocument
代码>未设置任何节属性。当涉及到部分和/或页面设置时,它依赖于wordprocessing应用程序的默认值
很明显,您始终需要检查文档中是否已有CTSectPr
。只有在它们已经存在的情况下,你才能使用它们。否则需要使用addnewspecpr
创建它们
获取页面设置的目的似乎是将表的宽度设置为effectivePageWidth
。但是,由于新创建的XWPFDocument
没有设置任何节属性,因此需要首先设置它们,而不是尝试获取不存在的部分
请参阅我使用的代码示例CTSectPr
: