Java 添加带空格的空白单元格时引发iText库异常

Java 添加带空格的空白单元格时引发iText库异常,java,itext,Java,Itext,我试图在我的pdf文件中添加一个空格(“”)的空白单元格。i、 e PdfPCell blankCell = ContentHandler.getNormalCell(" ", language,fontsize); 它抛出了这个错误 Message: java.lang.NullPointerException com.itextpdf.text.DocumentException: java.lang.NullPointerException at c

我试图在我的pdf文件中添加一个空格
(“”)
的空白单元格。i、 e

 PdfPCell blankCell = ContentHandler.getNormalCell(" ", language,fontsize);
它抛出了这个错误

Message: java.lang.NullPointerException
         com.itextpdf.text.DocumentException: java.lang.NullPointerException
         at com.itextpdf.text.pdf.PdfDocument.add(PdfDocument.java:727)
         at com.itextpdf.text.Document.add(Document.java:282)
然后我从牢房里腾出空间。。i、 e

PdfPCell blankCell = ContentHandler.getNormalCell("", language,fontsize);
我用这种方式解决了我的问题,但pdf格式很糟糕

有人能帮我吗?除了我正在做的事情之外,还有什么解决办法吗

编辑:

public static PdfPCell getNormalCell(String string, String language, float size) throws DocumentException, IOException {
    // TODO Auto-generated method stub
    Font f =  new Font();
    if(string!=null && !"".equals(string)){
        f = getFontForThisLanguage(language);
    }
    if(size==-1)  //Using a condition to make color RED as per need in view report
    {
    f.setColor(BaseColor.RED);
    }

    f.setSize(size);

    Chunk chunk = new Chunk(new String(string.getBytes(), "UTF-8"),f);

    PdfPCell pdfCell1 = new PdfPCell(new Phrase(string, f));
    pdfCell1.setHorizontalAlignment(Element.ALIGN_LEFT);

    return pdfCell1;

}

首先回答您的问题:我创建了一个名为的示例,我可以完美地将
添加到
PdfPCell
中,而不会导致
NullPointerException
。这是我的代码:

public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    PdfPTable table = new PdfPTable(2);
    table.addCell("Winansi");
    table.addCell(getNormalCell("Test", null, 12));
    table.addCell("Winansi");
    table.addCell(getNormalCell("Test", null, -12));
    table.addCell("Greek");
    table.addCell(getNormalCell("\u039d\u03cd\u03c6\u03b5\u03c2", "greek", 12));
    table.addCell("Czech");
    table.addCell(getNormalCell("\u010c,\u0106,\u0160,\u017d,\u0110", "czech", 12));
    table.addCell("Test");
    table.addCell(getNormalCell(" ", null, 12));
    table.addCell("Test");
    table.addCell(getNormalCell(" ", "greek", 12));
    table.addCell("Test");
    table.addCell(getNormalCell(" ", "czech", 12));
    document.add(table);
    document.close();
}

public static PdfPCell getNormalCell(String string, String language, float size)
        throws DocumentException, IOException {
    if(string != null && "".equals(string)){
        return new PdfPCell();
    }
    Font f  = getFontForThisLanguage(language);
    if(size < 0) {
        f.setColor(BaseColor.RED);
        size = -size;
    }
    f.setSize(size);
    PdfPCell cell = new PdfPCell(new Phrase(string, f));
    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
    return cell;
}
public static Font getFontForThisLanguage(String language) {
    if ("czech".equals(language)) {
        return FontFactory.getFont(FONT, "Cp1250", true);
    }
    if ("greek".equals(language)) {
        return FontFactory.getFont(FONT, "Cp1253", true);
    }
    return FontFactory.getFont(FONT, null, true);
}

请与我的方法版本进行比较。

我以前从未见过这个问题。我是iText的原始开发人员,以前从未见过
getNormalCell()
方法。如果你想知道这个问题的答案,你应该分享更多信息。您使用的是哪个版本的iText?不要惊讶,我们已经创建了自己的名为getNormalCell的方法,将用更多信息编辑我的帖子。我的iText版本是itextpdf-5.1.3,我已经更新了getNormalCell方法。(1)您使用的方法有一个
fontsize
参数;你分享的方法,没有。(2) 测试
string==null
,但不管返回什么,都会执行
string.getBytes()
,这可能会导致
NullPointerException
。(3) 您创建了一个
对象,但从未使用该
。我还需要说更多吗,或者你明白你应该扔掉你的代码重新开始吗?它仍然写得很糟糕。首先修复明显的错误,然后我们可以查看是否存在实际的iText问题。
public static PdfPCell getNormalCell(String string, String language, float size)
    throws DocumentException, IOException {
    // The next line will create an instance of Helvetica, 12pt.
    Font f =  new Font();
    // I don't understand this check. Why is it important to check string here?
    if(string!=null && !"".equals(string)){
        // are you sure you didn't want to check if language is not null?
        f = getFontForThisLanguage(language);
        // we have no idea what getFontForThisLanguage is doing
        // what if language is null, does it throw an exception?
    }
    // This is ridiculous. See the next comment to find out why
    if(size==-1)  //Using a condition to make color RED as per need in view report
    {
    f.setColor(BaseColor.RED);
    }
    // In some situations, you are setting the font size to -1, that doesn't make sense
    f.setSize(size);
    // This line doesn't make sense either.
    // (1.) string should already be in unicode.
    //      why are you getting the bytes and creating a UTF-8 string?
    // (2.) why are you creating this chunk object? you never use it
    Chunk chunk = new Chunk(new String(string.getBytes(), "UTF-8"),f);
    // See: you're creating a Phrase with string, you don't use chunk!
    PdfPCell pdfCell1 = new PdfPCell(new Phrase(string, f));
    pdfCell1.setHorizontalAlignment(Element.ALIGN_LEFT);
    return pdfCell1;
}