BIRT runtime simple example giving exception

dimaz
edited February 11, 2022 in Analytics #1
<p style="font-size:15px;color:rgb(36,39,41);font-family:Arial, 'Helvetica Neue', Helvetica, sans-serif;">I am attempting to create a report using BIRT runtime (4.5) and below is the code I used for creating a simple demo.</p>
<pre class="_prettyXprint">
public static void main(String[] args) {
try {
buildReport();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SemanticException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

// This function shows how to build a very simple BIRT report with a
// minimal set of content: a simple grid with an image and a label.

static void buildReport() throws IOException, SemanticException {

String birtRuntimePath = "C:\\birt-runtime-4_5_0\\ReportEngine\\";
// Create a session handle. This is used to manage all open designs.
// Your app need create the session only once.

// Configure the Engine and start the Platform
DesignConfig config = new DesignConfig();

// config.setProperty("BIRT_HOME", birtRuntimePath);
config.setBIRTHome(birtRuntimePath);
IDesignEngine engine = null;
try {

Platform.startup(config);
IDesignEngineFactory factoryDesignEngine = (IDesignEngineFactory) Platform
.createFactoryObject(IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY);
engine = factoryDesignEngine.createDesignEngine(config);

SessionHandle session = engine.newSessionHandle(ULocale.ENGLISH);

// Create a new report design.

ReportDesignHandle design = session.createDesign();

// The element factory creates instances of the various BIRT elements.

ElementFactory factory = design.getElementFactory();

// Create a simple master page that describes how the report will
// appear when printed.
//
// Note: The report will fail to load in the BIRT designer
// unless you create a master page.

DesignElementHandle element = factory.newSimpleMasterPage("Page Master"); //$NON-NLS-1$
design.getMasterPages().add(element);

// Create a grid and add it to the "body" slot of the report
// design.

GridHandle grid = factory.newGridItem(null, 2 /* cols */, 1 /* row */ );
design.getBody().add(grid);

// Note: Set the table width to 100% to prevent the label
// from appearing too narrow in the layout view.

grid.setWidth("100%"); //$NON-NLS-1$

// Get the first row.

RowHandle row = (RowHandle) grid.getRows().get(0);

// Create an image and add it to the first cell.

ImageHandle image = factory.newImage(null);
CellHandle cell = (CellHandle) row.getCells().get(0);
cell.getContent().add(image);
image.setURL("\"http://www.eclipse.org/birt/phoenix/tutorial/basic/multichip-4.jpg\"");

// Create a label and add it to the second cell.

LabelHandle label = factory.newLabel(null);
cell = (CellHandle) row.getCells().get(1);
cell.getContent().add(label);
label.setText("Hello, world!"); //$NON-NLS-1$

// Save the design and close it.

design.saveAs("C:/Test/sample.rptdesign"); //$NON-NLS-1$
design.close();
System.out.println("Finished");

// We're done!


} catch (Exception ex) {
ex.printStackTrace();
}
}
</pre>
<p><span style="color:rgb(36,39,41);font-family:Arial, 'Helvetica Neue', Helvetica, sans-serif;font-size:15px;">POM.xml</span></p>
<pre class="_prettyXprint">
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>


<dependency>
<groupId>org.eclipse.birt.runtime</groupId>
<artifactId>org.eclipse.birt.runtime</artifactId>
<version>4.5.0a</version>
</dependency>

<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>4.8</version>
</dependency>
</pre>
<p style="font-size:15px;color:rgb(36,39,41);font-family:Arial, 'Helvetica Neue', Helvetica, sans-serif;">When I run the main method in eclipse or maven, I usually have a NullPointerException on this line</p>
<pre class="_prettyXprint">
engine = factoryDesignEngine.createDesignEngine(config);
</pre>
<p style="font-size:15px;color:rgb(36,39,41);font-family:Arial, 'Helvetica Neue', Helvetica, sans-serif;">Run the program again in eclipse using debug mode and found that factoryDesignEngine variable is null.</p>
<pre class="_prettyXprint">
IDesignEngineFactory factoryDesignEngine = (IDesignEngineFactory) Platform.createFactoryObject(IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY);</pre>
<p style="font-size:15px;color:rgb(36,39,41);font-family:Arial, 'Helvetica Neue', Helvetica, sans-serif;">I understand that this line is supposed to initialize factoryDesignEngine but I don't understand why it says it's null. Maybe there is a setup problem or a configuration problem on my eclipse. I have already all the dependencies resolved in Maven but I still can't get it to work. The original code is found <a data-ipb='nomediaparse' href='http://www.eclipse.org/birt/resources/documentation/integrating/DeDemo.java'>here</a> and made a bit of modification, I've seen articles claiming that they were able to run the sample program just fine.</p>
<p style="font-size:15px;color:rgb(36,39,41);font-family:Arial, 'Helvetica Neue', Helvetica, sans-serif;">Any help is greatly appreciated.</p>