I am new to Jasper Reports and i am trying to generate a bar chart with data coming from my data source but am getting stuck at a place. I am getting exception which i am not able to resolve. Cant figure out where am i going wrong . Can someone pls help.
Exception in thread "main" java.lang.NullPointerException at net.sf.jasperreports.charts.fill.JRFillCategoryDataset.customIncrement(JRFillCategoryDataset.java:136) at net.sf.jasperreports.engine.fill.JRFillElementDataset.increment(JRFillElementDataset.java:134)
Not able to generate a bar chart
in Products
Posted
Hi ,
I am new to Jasper Reports and i am trying to generate a bar chart with data coming from my data source but am getting stuck at a place. I am getting exception which i am not able to resolve. Cant figure out where am i going wrong . Can someone pls help.
Exception in thread "main" java.lang.NullPointerException at net.sf.jasperreports.charts.fill.JRFillCategoryDataset.customIncrement(JRFillCategoryDataset.java:136) at net.sf.jasperreports.engine.fill.JRFillElementDataset.increment(JRFillElementDataset.java:134)
Heres the java file.
public class JasperReportsIntro
{
public static void main(String[] args)
{
JasperReport jasperReport;
JasperPrint jasperPrint;
try
{
jasperReport = JasperCompileManager.compileReport("/home/arpana/work/bccms/src/com/bc/reports/rep/jasperreports_demo.jrxml");
String columnNames[]={"FIELD1","VALUE"};
ArrayList arrayList = new ArrayList<ArrayList<Double>>();
List<Double> dataArray = new ArrayList<Double>();
dataArray.add(0.0);
dataArray.add(1.0);
arrayList.add(dataArray);
List<Double> dataArray1 = new ArrayList<Double>();
dataArray1.add(2.0);
dataArray1.add(3.0);
arrayList.add(dataArray1);
jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), new JRTableModelDataSource(new DataSource(columnNames, arrayList)));
JasperExportManager.exportReportToPdfFile(jasperPrint, "/home/arpana/bar_report.pdf");
System.out.println("Successfully exported to PDF");
}
catch (JRException e)
{
e.printStackTrace();
}
}
}
Heres the jrxml file :
<?xml version="1.0"?>
<!DOCTYPE jasperReport
PUBLIC "-//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="Simple_Report">
<parameter name="VALUE_HEADER_P" isForPrompting="false" class="java.lang.String"/>
<field name="FIELD1" class="java.lang.Double">
</field>
<field name="VALUE" class="java.lang.Double">
</field>
<variable name="SumFirstGroup" class="java.lang.Double">
<variableExpression><![CDATA[$F{VALUE}]]></variableExpression>
</variable>
<group name="FirstGroup" isStartNewColumn="false" isReprintHeaderOnEachPage="false" minHeightToStartNewPage="25">
<groupExpression><![CDATA[$F{FIELD1}]]></groupExpression>
</group>
<detail>
<band height="120">
<staticText>
<reportElement x="80" y="0" width="200" height="20"/>
<text><![CDATA[Hello World!]]></text>
</staticText>
<barChart>
<chart evaluationTime="Report">
<reportElement positionType="Float" x="0" y="35" width="540" height="75"/>
</chart>
<categoryDataset>
<dataset incrementType="Group" incrementGroup="FirstGroup"/>
<categorySeries>
<seriesExpression>$P{VALUE_HEADER_P}</seriesExpression>
<categoryExpression>$F{FIELD1}</categoryExpression>
<valueExpression>new Double($F{VALUE}.doubleValue() / 2d)</valueExpression>
</categorySeries>
</categoryDataset>
<barPlot isShowTickMarks="true" isShowTickLabels="true">
<plot/>
</barPlot>
</barChart>
</band>
</detail>
</jasperReport>
And finally the data source file.
public class DataSource extends AbstractTableModel
{
private String[] columnNames = null;;
private String[] columnDisplayNames = null;
/**
* ArrayList of ArrayLists
*/
private ArrayList<ArrayList> data = null;
public DataSource(String[] columns, ArrayList records)
{
columnNames = columns;
data = records;
}
public void setColumnDisplayNames(String[] displayNames)
{
columnDisplayNames = displayNames;
}
public String[] getColumnDisplayNames()
{
return columnDisplayNames;
}
public int getColumnCount()
{
return this.columnNames.length;
}
public String getColumnName(int columnIndex)
{
return this.columnNames[columnIndex];
}
public int getRowCount()
{
return this.data.size();
}
public Object getValueAt(int rowIndex, int columnIndex)
{
ArrayList record = (ArrayList) data.get(rowIndex);
return record.get(columnIndex);
}
public ArrayList getData()
{
return data;
}
}