You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by John Turnbull <jt...@uow.edu.au> on 2011/12/06 00:53:17 UTC

NPE setting font family for a XWPFRun

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

RE: NPE setting font family for a XWPFRun

Posted by John Turnbull <jt...@uow.edu.au>.
OK, I'll paste the code in here instead:

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();
		}
	}
}

-----Original Message-----
From: John Turnbull [mailto:jturnbul@uow.edu.au] 
Sent: Tuesday, 6 December 2011 1:47 PM
To: POI Users List
Subject: RE: NPE setting font family for a XWPFRun

Hmm, the test case seemed to not make it on to the list.  I'll try again now.

-----Original Message-----
From: John Turnbull [mailto:jturnbul@uow.edu.au] 
Sent: Tuesday, 6 December 2011 1:44 PM
To: POI Users List
Subject: RE: NPE setting font family for a XWPFRun

Hi Nick,

I have attached a simple test case and a stripped-down version of the document in question which always crashes as previously described.  Please let me know if it crashes for you as well.

-jct

P.S. I hope this list takes attachments!

-----Original Message-----
From: Nick Burch [mailto:nick.burch@alfresco.com]
Sent: Tuesday, 6 December 2011 12:24 PM
To: POI Users List
Subject: Re: NPE setting font family for a XWPFRun

On Tue, 6 Dec 2011, John Turnbull wrote:
> 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)

I can't see anything obviously wrong with that line that would cause this. 
Are you able to create a simple unit test that shows the problem?

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org For additional commands, e-mail: user-help@poi.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


RE: NPE setting font family for a XWPFRun

Posted by John Turnbull <jt...@uow.edu.au>.
Hmm, the test case seemed to not make it on to the list.  I'll try again now.

-----Original Message-----
From: John Turnbull [mailto:jturnbul@uow.edu.au] 
Sent: Tuesday, 6 December 2011 1:44 PM
To: POI Users List
Subject: RE: NPE setting font family for a XWPFRun

Hi Nick,

I have attached a simple test case and a stripped-down version of the document in question which always crashes as previously described.  Please let me know if it crashes for you as well.

-jct

P.S. I hope this list takes attachments!

-----Original Message-----
From: Nick Burch [mailto:nick.burch@alfresco.com]
Sent: Tuesday, 6 December 2011 12:24 PM
To: POI Users List
Subject: Re: NPE setting font family for a XWPFRun

On Tue, 6 Dec 2011, John Turnbull wrote:
> 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)

I can't see anything obviously wrong with that line that would cause this. 
Are you able to create a simple unit test that shows the problem?

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org For additional commands, e-mail: user-help@poi.apache.org



RE: NPE setting font family for a XWPFRun

Posted by Nick Burch <ni...@alfresco.com>.
On Tue, 6 Dec 2011, John Turnbull wrote:
> I'll gladly create a new Bugzilla entry if you can confirm that it is a 
> bug i.e. it crashes for you too and I am not doing anything obviously 
> silly.

You shouldn't be getting a NPE, so it does look like a bug

> P.S. What's the Bugzilla address?

https://issues.apache.org/bugzilla/buglist.cgi?product=POI

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


RE: NPE setting font family for a XWPFRun

Posted by John Turnbull <jt...@uow.edu.au>.
I'll gladly create a new Bugzilla entry if you can confirm that it is a bug i.e. it crashes for you too and I am not doing anything obviously silly.

Thanks,

-jct

P.S. What's the Bugzilla address?

-----Original Message-----
From: Nick Burch [mailto:nick.burch@alfresco.com] 
Sent: Tuesday, 6 December 2011 1:52 PM
To: POI Users List
Subject: RE: NPE setting font family for a XWPFRun

On Tue, 6 Dec 2011, John Turnbull wrote:
> I have attached a simple test case and a stripped-down version of the 
> document in question which always crashes as previously described. 
> Please let me know if it crashes for you as well.

Any chance you could create a new bugzilla entry, and attach it there?

> P.S. I hope this list takes attachments!

It isn't a big fan, bugzilla is much much safer!

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


RE: NPE setting font family for a XWPFRun

Posted by Nick Burch <ni...@alfresco.com>.
On Tue, 6 Dec 2011, John Turnbull wrote:
> I have attached a simple test case and a stripped-down version of the 
> document in question which always crashes as previously described. 
> Please let me know if it crashes for you as well.

Any chance you could create a new bugzilla entry, and attach it there?

> P.S. I hope this list takes attachments!

It isn't a big fan, bugzilla is much much safer!

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


RE: NPE setting font family for a XWPFRun

Posted by John Turnbull <jt...@uow.edu.au>.
Hi Nick,

I have attached a simple test case and a stripped-down version of the document in question which always crashes as previously described.  Please let me know if it crashes for you as well.

-jct

P.S. I hope this list takes attachments!

-----Original Message-----
From: Nick Burch [mailto:nick.burch@alfresco.com] 
Sent: Tuesday, 6 December 2011 12:24 PM
To: POI Users List
Subject: Re: NPE setting font family for a XWPFRun

On Tue, 6 Dec 2011, John Turnbull wrote:
> 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)

I can't see anything obviously wrong with that line that would cause this. 
Are you able to create a simple unit test that shows the problem?

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: NPE setting font family for a XWPFRun

Posted by Nick Burch <ni...@alfresco.com>.
On Tue, 6 Dec 2011, John Turnbull wrote:
> 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)

I can't see anything obviously wrong with that line that would cause this. 
Are you able to create a simple unit test that shows the problem?

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org