Wednesday, June 1, 2011

BIRT report conditional watermark: My fight with the NullPointerException

Hello All,

The first thing I ever did with BIRT was to develop a report from a fairly simple data set. And, there was a very simple requirement of displaying a TEST watermark if the report was a draft.

To accommodate this change, there was a simple change in the designer where, I had to insert one string type report parameter bgImage which would have the url of the TEST image file for draft report and nothing for actual report. Also, in the onPrepare method of any palette inside the master page, the following script had to be inserted:
this.getParent().getStyle().backgroundImage = params["bgImage"];

This worked fine for every draft report and naturally I was very happy to have figured it out so soon. But, when I finally tried generating an actual report, BIRT failed with a NullPointerException. Since the report was running all right before this change, the primary suspect was the inserted script, and as everybody would have done, I included a check in the script as:
if(params["bgImage"] != null || params["bgImage"] != "")
this.getParent().getStyle().backgroundImage = params["bgImage"];

Somehow even this didn't seemed to work out. Frustrated and irritated, I had only one place to look back to, the BIRT exchange forum. Got some valuable suggestions from experts, but somehow I was unable to come out of this. Then, I thought of giving this report parameter(bgImage) a default value like none or junk and then try out. To play safe, the check was still used otherwise I suspected that BIRT might throw FileNotFound or something similar. Hence, my new script looked like:
if(params["bgImage"] != null && params["bgImage"] != "junk" || params["bgImage"] != "none")
this.getParent().getStyle().backgroundImage = params["bgImage"];

And voila!!! The report ran successfully for both draft and actual reports with expected output and watermarks where needed.

Since, this thing was working, I never bothered to modify the script. But just out of curiosity, one fine day I removed the check, now my script looked like the initial one:
this.getParent().getStyle().backgroundImage = params["bgImage"];
The only change that has happened till now was, that I gave the parameter a default junk value.

To my surprise, the report ran successfully for draft as well as actual. Watermark was appropriately applied. Till date I'm unable to draw a conclusion as why the NullPointerException, but I have maintained it as a thumb rule to always specify default value for nullable url parameters.

The report is running fine till date :) :)

And I finally won the fight with the all times champion NullPointerException

Follow this link http://www.birt-exchange.org/org/forum/index.php/topic/19303-displaying-watermark-image-conditionally-birt-2-5/page__pid__63748__st__0 for more information on this topic.

Disclaimer: This is 100% personal opinion. If it contradicts in any means, the writer requests to be informed.