Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ooxml模式、IntelliJ IDEA和NullPointerException_Java_Apache_Intellij Idea_Apache Poi_Openxml - Fatal编程技术网

Java ooxml模式、IntelliJ IDEA和NullPointerException

Java 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

我试图使用某些需要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;
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

A
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