https://issues.apache.org/bugzilla/show_bug.cgi?id=52288

             Bug #: 52288
           Summary: Trying to set font family on XWPFRun causes NPE
           Product: POI
           Version: 3.8-dev
          Platform: PC
        OS/Version: Windows XP
            Status: NEW
          Severity: blocker
          Priority: P2
         Component: XWPF
        AssignedTo: [email protected]
        ReportedBy: [email protected]
    Classification: Unclassified


Created attachment 28029
  --> https://issues.apache.org/bugzilla/attachment.cgi?id=28029
Test document that causes the crash

I have just started using POI and I am trying to set the font family for a
XWPFRun using XWPFRun#setFontFamily() but it always crashes with a
NullPointerException.  I have tried this with POI versions 3.7 and 3.8 Beta 4
with the same results.

The font exists and the run's text is not null so why would this fail?  I
cannot locate the source in question but the actual exception is:

java.lang.NullPointerException
                at
org.apache.poi.xwpf.usermodel.XWPFRun.setFontFamily(XWPFRun.java:465)

Does anyone know how I can get around this?  Calling XWPFRun#getFontFamily()
immediately before trying to set it returns null but I wouldn't have thought
that would matter.

Thanks,

-jct

Here is the code for a test case to use with the attached document:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class RunTest {

    private static final String INPUT_FILE_NAME = "C:\\input.docx";
    private static final String OUTPUT_FILE_NAME = "C:\\output.docx";

    public static void main(final String[] args) {
        try {
            final XWPFDocument doc = new XWPFDocument(new
FileInputStream(INPUT_FILE_NAME));
            final Iterator<XWPFParagraph> paragraphs =
doc.getParagraphsIterator();
            while (paragraphs.hasNext()) {
                final XWPFParagraph paragraph = paragraphs.next();
                for (final XWPFRun run : paragraph.getRuns()) {
                    if (run != null) {
                        final String text = run.getText(0);
                        if (text != null) {
                            run.setFontFamily("Times New Roman");
                        }
                    }
                }
            }
            doc.write(new FileOutputStream(OUTPUT_FILE_NAME));
        } catch (final Exception e) {
            e.printStackTrace();
        }
    }
}

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to