Created attachment 28029 [details] 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(); } } }
I've just tried adding your sample file and testcase to head. Good and bad news - it passes already without any changes needed... The test was added in r1210768. I'd suggest you try a recent svn nightly build (or wait a week or two for beta 5), and it should be fixed there
Thanks. So it doesn't crash in recent builds but does it actually work as well? I read somewhere else that setting the font size before setting the font family avoids the NPE but I noticed that it doesn't actually change the font family in the run. Do you know if this works now that the NPE has been fixed in SVN? I have not been able to try it yet.