Getting null pointer exception on fillReport

0
Hi all,

Would be very much appeciative if someone could point me in the right direction

try {
if (conn != null) {
File f = new File(reportFile);
InputStream is = new FileInputStream(f);

JasperReport jasperReport = JasperCompileManager.compileReport(
is);

jasperPrint = JasperFillManager.fillReport(jasperReport, params, conn);

}
} catch (JRException ejr) {
ejr.printStackTrace();
}catch (Exception __ex) {
__ex.printStackTrace();
}

it gives me the following exception :
java.lang.NullPointerException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at net.sf.jasperreports.engine.util.JRClassLoader.loadClassForRealName(JRClassLoader.java:161)
at net.sf.jasperreports.engine.util.JRClassLoader.loadClassForName(JRClassLoader.java:119)
at net.sf.jasperreports.engine.util.JRSingletonCache.createInstance(JRSingletonCache.java:88)
at net.sf.jasperreports.engine.util.JRSingletonCache.getCachedInstance(JRSingletonCache.java:78)
at net.sf.jasperreports.engine.util.JRTextMeasurerUtil.getTextMeasurerFactory(JRTextMeasurerUtil.java:118)
at net.sf.jasperreports.engine.util.JRTextMeasurerUtil.createTextMeasurer(JRTextMeasurerUtil.java:103)
at net.sf.jasperreports.engine.util.JRTextMeasurerUtil.createTextMeasurer(JRTextMeasurerUtil.java:90)

Thanks in advance for any help

Fraser Williams
fwilliams's picture
Joined: Dec 6 2006 - 10:28pm
Last seen: 6 years 3 months ago

12 Answers:

0
As well I should note that the error is being thrown at the JasperFillManager.fillReport part of the code.

This is mind boggling as this has always worked for me in the past only when I upgraded iReport has this started to occure.

Thanks again - Fraser
fwilliams's picture
Joined: Dec 6 2006 - 10:28pm
Last seen: 6 years 3 months ago
0
I can only imagine (at the moment) a single scenario in which this might occur, namely when one has several versions of the JasperReports jar on the classpath.

Is this the case? If no, how can this error be reproduced?

Regards,
Lucian
lucianc's picture
7146
Joined: Jul 17 2006 - 1:10am
Last seen: 4 days 5 hours ago
0
Thanks for your response. My classpath has no other versions of the jasperreports jars.

I have a directory where I have put the iReport program. I have put a small application in the same directory which offers a menu system for the user to view a report. The user double clicks the report and the report is supposed to show in the JRViewer however I am getting the error as described above. IReport works like a charm from the same folder so it puzzles me as to why my program doesn't. Hmmmm!

I code with Netbeans the jasperreports jars are referenced in the libraries and no other classpath information is specified.

Fraser
fwilliams's picture
Joined: Dec 6 2006 - 10:28pm
Last seen: 6 years 3 months ago
0
Is there a simple way to reproduce this? I'm afraid I'm out of clues..

You could also try debugging the application (that's why JasperReports is open source, after all) in order to see what exactly happens.

Regards,
Lucian
lucianc's picture
7146
Joined: Jul 17 2006 - 1:10am
Last seen: 4 days 5 hours ago
0
Hi all!

I get exactly the same problem with only one version of the JasperReports jar on the classpath... (the 2.0.5)
For information, is use iReport 2.0.2 and it works fine but not my application...

Did anybody find a solution ?

Thanks in advance !
JB
jbcostis's picture
Joined: Nov 5 2007 - 6:38pm
Last seen: 12 years 2 weeks ago
0
Hi,

For information, I solved this problem modifying the default.jasperreports.properties file as follow :

Replace
Code:
<br />
#Default text measurer<br />
net.sf.jasperreports.text.measurer.factory=default<br />
net.sf.jasperreports.text.measurer.factory.default=net.sf.jasperreports.engine.fill.TextMeasurerFactory</td></tr></tbody></table><br />
<br />
by<br />
<table align="center" border="0" cellpadding="3" cellspacing="1" width="90%"><tbody><tr><td><b>Code:</b></td></tr><tr><td><pre><br />
#Text measurer (overridden because of a NullPointerException in JRClassLoader...)<br />
net.sf.jasperreports.text.measurer.factory=net.sf.jasperreports.engine.fill.TextMeasurerFactory</td></tr></tbody></table><br />
<br />
I hope this will help...<br />
JB
jbcostis's picture
Joined: Nov 5 2007 - 6:38pm
Last seen: 12 years 2 weeks ago
0
I got the same problem as this when moving from Jasper 2.0.1 to 3.0.0, and I tracked it down. To repeat, this is the stack trace:

Code:
<br />
java.lang.NullPointerException<br />
at java.lang.Class.forName0(Native Method)<br />
at java.lang.Class.forName(Unknown Source)<br />
at net.sf.jasperreports.engine.util.JRClassLoader.loadClassForRealName(JRClassLoader.java:161)<br />
at net.sf.jasperreports.engine.util.JRClassLoader.loadClassForName(JRClassLoader.java:119)<br />
at  net.sf.jasperreports.engine.util.JRSingletonCache.createInstance(JRSingletonCache.java:88)<br />
at net.sf.jasperreports.engine.util.JRSingletonCache.getCachedInstance(JRSingletonCache.java:78)<br />
at net.sf.jasperreports.engine.util.JRTextMeasurerUtil.getTextMeasurerFactory(JRTextMeasurerUtil.java:118)<br />
at net.sf.jasperreports.engine.util.JRTextMeasurerUtil.createTextMeasurer(JRTextMeasurerUtil.java:103)<br />
at net.sf.jasperreports.engine.util.JRTextMeasurerUtil.createTextMeasurer(JRTextMeasurerUtil.java:90)<br />
at net.sf.jasperreports.engine.fill.JRFillTextElement.createTextMeasurer(JRFillTextElement.java:115)<br />
at net.sf.jasperreports.engine.fill.JRFillTextElement.ensureTextMeasurer(JRFillTextElement.java:122)<br />
at net.sf.jasperreports.engine.fill.JRFillTextElement.chopTextElement(JRFillTextElement.java:972)<br />
at net.sf.jasperreports.engine.fill.JRFillStaticText.prepare(JRFillStaticText.java:193)<br />
at net.sf.jasperreports.engine.fill.JRFillElementContainer.prepareElements(JRFillElementContainer.java:344)<br />
at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:346)<br />
at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:305)<br />
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillTitle(JRVerticalFiller.java:313)<br />
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:247)<br />
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:113)<br />
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:879)<br />
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:801)<br />
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:89)<br />
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:601)<br />
at net.sf.jasperreports.engine.JasperFillManager.fillReportToFile(JasperFillManager.java:492)<br />
at net.sf.jasperreports.engine.JasperFillManager.fillReportToFile(JasperFillManager.java:448)<br />
</td></tr></tbody></table><br />
<br />
I am not fluent with the Jasper source enough to propose a fix, but I now where the problem occurs.  In net.sf.jasperreports.engine.util.JRTextMeasurerUtil line 86 there is this function:<br />
<table align="center" border="0" cellpadding="3" cellspacing="1" width="90%"><tbody><tr><td><b>Code:</b></td></tr><tr><td><pre><br />
public static JRTextMeasurer createTextMeasurer(JRCommonText text)<br />
{<br />
   JRPropertiesHolder propertiesHolder =<br />
	text instanceof JRPropertiesHolder    ? (JRPropertiesHolder) text : null;<br />
   return createTextMeasurer(text,    <br />
      propertiesHolder);<br />
}<br />
</td></tr></tbody></table><br />
As you can see, if the input is not an instance of JRPropertiesHolder then the function ends with a call to createTextMeasurer(text, null).<br />
<br />
createTextMeasurer calls other functions to load a class based upon its second input parameter, and naturally this throws a NullPointerException.
EsauCairn's picture
Joined: Jun 19 2008 - 6:23am
Last seen: 11 years 5 months ago
0
EsauCairn wrote:

I am not fluent with the Jasper source enough to propose a fix, but I now where the problem occurs. In net.sf.jasperreports.engine.util.JRTextMeasurerUtil line 86 there is this function:
Code:
<br />
public static JRTextMeasurer createTextMeasurer(JRCommonText text)<br />
{<br />
   JRPropertiesHolder propertiesHolder =<br />
	text instanceof JRPropertiesHolder    ? (JRPropertiesHolder) text : null;<br />
   return createTextMeasurer(text,    <br />
      propertiesHolder);<br />
}<br />
</td></tr></tbody></table><br />
As you can see, if the input is not an instance of JRPropertiesHolder then the function ends with a call to createTextMeasurer(text, null).<br />
<br />
createTextMeasurer calls other functions to load a class based upon its second input parameter, and naturally this throws a NullPointerException.</td></tr></table><br />
<br />
createTextMeasurer() should work fine with a null propertiesHolder.  If you trace the code further, you'll see that the propertiesHolder is used in calls to [url=http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/util/JRProperties.html#getProperties(net.sf.jasperreports.engine.JRPropertiesHolder,%20java.lang.String)]JRProperties.getProperties(JRPropertiesHolder, String).  If propertiesHolder is null, the default JR properties would be used to determine the text measurer class name.<br />
<br />
If you post a test case to reproduce this problem, we would investigate it and find what causes it.<br />
<br />
Regards,<br />
Lucian
lucianc's picture
7146
Joined: Jul 17 2006 - 1:10am
Last seen: 4 days 5 hours ago
0
lucianc,
You were correct and I was wrong. I apologize for posting unhelpful data.

All I can see is that if you replace
net.sf.jasperreports.text.measurer.factory=default
net.sf.jasperreports.text.measurer.factory.default=net.sf.jasperreports.engine.fill.TextMeasurerFactory

with
net.sf.jasperreports.text.measurer.factory=net.sf.jasperreports.engine.fill.TextMeasurerFactory
The null pointer exception goes away.

The problem baffles me. To reproduce the problem, I simply need to run:
Code:
<br />
String path = getPath(); <br />
String fileName = getReportFileName();<br />
Map parameters = new HashMap(); <br />
populate(parameters); <br />
<br />
JasperCompileManager.compileReportToFile(path + filename + ".jrxml"«»);<br />
JasperFillManager.fillReportToFile(path+filename+".jasper", parameters, new JREmptyDataSource());	<br />
</td></tr></tbody></table><br />
<br />
It runs without errors with the change to the default.jasperreports.properties configuration file above, and gives the noted nullpointerexception otherwise.<br />
<br />
I'll try to take another stab at this over the weekend, see if I can't lock it down.  Thanks for your help.
EsauCairn's picture
Joined: Jun 19 2008 - 6:23am
Last seen: 11 years 5 months ago
0
This baffles me as well. That's why I want to understand what happens.

In any case, this occurred several times, so it's a real problem. Please log it as a bug [url=http://jasperforge.org/sf/tracker/do/listArtifacts/projects.jasperreport....

In the worst case, if we don't understand why this happens, we could still apply the fix suggested here. We don't usually fix things without understanding where the problem is, but since the described fix would not make any harm, we could consider applying it.

Regards,
Lucian
lucianc's picture
7146
Joined: Jul 17 2006 - 1:10am
Last seen: 4 days 5 hours ago
0
I would prefer to wait to file the report until I can pinpoint the problem and possibly recommend a solution.

I took the jar file out of my project. Instead, I included the source code from Jasper 3.0.0 into my project, built it, and ran it. The bug did not appear. This is, naturally, puzzling. When the bug appears in the .jar, the stack trace line numbers match the source code line numbers exactly, so I consider the possibility that the official .jar has separate code from the source tree to be very small.

Next I am going to use the .jar and replace the affected class files with ones compiled from source one at a time to pinpoint the problem.
EsauCairn's picture
Joined: Jun 19 2008 - 6:23am
Last seen: 11 years 5 months ago
0

 hi,

 

I have tried to edit the default.jasperreports.prop in jasper.jar, but still got the NullPointer exceptions, such as:

java.lang.NullPointerException

at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:89)

at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:628)

at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:544)

 

What I am trying to do is: user jasper 3.5.3 with spring 3.0.2 on glassfish 2.1. 

 

The next line is causing the trouble:

jasperPrint = JasperFillManager.fillReport(myFile.getPath(),parameters, jasperDs);

 

All of these parameters are not null, and has valid and  good values in it.

 

Any ideas Guys?

 

thx

pogacsa's picture
Joined: Jun 29 2011 - 10:17pm
Last seen: 4 years 5 months ago
Feedback
randomness