在jasperstudio的JasperFillManager.fillReport的JasperPrint对象中获取NullpointerException

时间:2018-01-31 09:17:44

标签: java eclipse-plugin jasper-reports rcp

我已经从eclipse市场安装了Jasper studio 6.4.0并使用jasperdesigner来设计报告。

报告的数据源是xmldatasource JRXmlDataSource dataSource = new JRXmlDataSource(xmlfile); 当我打电话的时候 JasperFillManager.fillReport(jasperReport,params, dataSource); 获取nullpointer异常(GenerateReport是我创建的类名)

这是我的异常堆栈:

java.lang.NullPointerException
    at com.jaspersoft.studio.debug.TraceGovernor.setup(TraceGovernor.java:107)
    at com.jaspersoft.studio.debug.TraceGovernor.initMaps(TraceGovernor.java:114)
    at com.jaspersoft.studio.debug.TraceGovernor.beforeReportInit(TraceGovernor.java:158)
    at net.sf.jasperreports.engine.fill.JRFillDatasetScriptlet.beforeReportInit(JRFillDatasetScriptlet.java:74)
    at net.sf.jasperreports.engine.JRAbstractScriptlet.callBeforeReportInit(JRAbstractScriptlet.java:176)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:236)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:115)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:582)
    at net.sf.jasperreports.engine.fill.BaseReportFiller.fill(BaseReportFiller.java:414)
    at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:121)
    at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:667)
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:983)
    at com.continental.benchmark.popup.actions.GenerateReport.fill(GenerateReport.java:186)
    at com.continental.benchmark.popup.actions.GenerateReport.run(GenerateReport.java:140)
    at org.eclipse.ui.internal.PluginAction.runWithEvent(PluginAction.java:247)
    at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:565)
    at org.eclipse.jface.action.ActionContributionItem.lambda$4(ActionContributionItem.java:397)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4418)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1079)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4236)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3824)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$4.run(PartRenderingEngine.java:1121)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1022)
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:150)
    at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:693)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:610)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:148)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:138)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:388)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:243)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:673)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:610)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1519)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1492)

任何人都可以建议我解决方案。

Dummy.jrxml:

   <?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.4.0.final using JasperReports Library version 6.4.1  -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Dummy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="5e964e3f-1705-4bda-bc9a-2b3372dfeb58">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Dummy Adapter "/>
    <queryString language="XPath">
        <![CDATA[/Dummy_Report/Author]]>
    </queryString>
    <field name="ID" class="java.lang.String">
        <fieldDescription><![CDATA[ID]]></fieldDescription>
    </field>
    <field name="Name" class="java.lang.String">
        <fieldDescription><![CDATA[Name]]></fieldDescription>
    </field>
    <field name="Version" class="java.lang.String">
        <fieldDescription><![CDATA[Version]]></fieldDescription>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="35" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="555" height="35" uuid="f24c035a-72d3-427b-95a3-29156eb2c0c2"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="Arial" size="20"/>
                </textElement>
                <text><![CDATA[Dummy Report]]></text>
            </staticText>
        </band>
    </title>
    <columnHeader>
        <band height="31">
            <staticText>
                <reportElement x="0" y="0" width="100" height="30" uuid="d0cbab44-c38a-487a-8f02-532b8066e929"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[ID]]></text>
            </staticText>
            <staticText>
                <reportElement x="100" y="0" width="300" height="30" uuid="b476b250-7316-4c72-b1ff-f669822070ea"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[Name]]></text>
            </staticText>
            <staticText>
                <reportElement x="400" y="0" width="155" height="30" uuid="af4215b5-7e4a-4bf9-ab8c-77fcff396be6"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[Version]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="211" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="100" height="30" uuid="a0bf6c59-7422-4340-8fd4-b95851bd7777"/>
                <textElement textAlignment="Center" verticalAlignment="Top"/>
                <textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="100" y="0" width="300" height="30" uuid="40a8a896-d1f6-48a1-9311-ae1264e91ecf"/>
                <textElement textAlignment="Center" verticalAlignment="Top"/>
                <textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="400" y="0" width="155" height="30" uuid="3d8577ea-970b-4d52-9628-da81b1229cb2"/>
                <textElement textAlignment="Center" verticalAlignment="Top"/>
                <textFieldExpression><![CDATA[$F{Version}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

**GenerateReport.java**

    import java.io.File;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;

import org.eclipse.core.resources.IFile;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.w3c.dom.Document;

import com.continental.benchmark.benchmark.BenchmarkPackage;
import com.continental.benchmark.benchmark.Template;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRMapArrayDataSource;
import net.sf.jasperreports.engine.data.JRXmlDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.query.JRXPathQueryExecuterFactory;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.engine.util.JRXmlUtils;

public class GenerateReport implements IObjectActionDelegate {

    private Shell shell;
    IFile file;
    URI uriFile;

    /**
     * Constructor for Action1.
     */
    public GenerateReport() {
        super();
    }

    /**
     * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
     */
    public void setActivePart(IAction action, IWorkbenchPart targetPart) {
        shell = targetPart.getSite().getShell();
    }
/**
     * @see IActionDelegate#run(IAction)
     */
    public void run(IAction action){File outDir = new File("D://Report");
               outDir.mkdirs();

               /*try {
                JasperExportManager.exportReportToPdfFile("C:/Users/uidk3332/Documents/Project/Benchmarking_Suite/src/main/com.continental.benchmark.reports/src/com/continental/benchmark/description/report/Benchmark_Description_Template.jrprint",
                           "D://Report//JasperOutput.pdf");

                   } catch (JRException e) {
                e.printStackTrace();
            }*/

               JRPdfExporter exporter = new JRPdfExporter();

                try {
                    exporter.setParameter(JRExporterParameter.JASPER_PRINT, fill());
                } catch (JRException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "D:/Report/Result.pdf");
                try {
                    exporter.exportReport();
                } catch (JRException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        /*} catch (JRException e) {
            e.printStackTrace();
        }*/
    }
public JasperPrint fill() throws JRException
    {
Map<String, Object> params = new HashMap<>();
        JRMapArrayDataSourceDemo obj = new  JRMapArrayDataSourceDemo();
        JRMapArrayDataSource dataSource = obj.arrData();
params.put(JRXPathQueryExecuterFactory.XML_DATE_PATTERN, "yyyy-MM-dd");
        params.put(JRXPathQueryExecuterFactory.XML_NUMBER_PATTERN, "#,##0.##");
        params.put(JRXPathQueryExecuterFactory.XML_LOCALE, Locale.ENGLISH);
        params.put(JRParameter.REPORT_LOCALE, Locale.US);

JasperPrint jasperPrintObj = JasperFillManager.fillReport(jasperReport,params, dataSource);

        //System.out.println("---------------------------"+jasperPrint);
        return jasperPrintObj;
}
}

JRMapArrayDataSourceDemo.java

public class JRMapArrayDataSourceDemo {

    public JRMapArrayDataSource arrData()
    {
Set<Employee> emp = new HashSet<>();
        emp.add(new Employee("ghj", "95"));
        emp.add(new Employee("abc", "958"));
        emp.add(new Employee("def", "955"));
        Map<String, String> employee = new HashedMap();
        for(Employee ob: emp)
        {
            employee.put(ob.getName(), ob.getId());
        }
            Object[] data = {employee};

            JRMapArrayDataSource dataSource = new JRMapArrayDataSource(data);

            return dataSource;

    }
}

class Employee
{
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    private String id;
    Employee(String name, String id)
    {
        this.name = name;
        this.id = id;
    }
}

Dummy.xml

<Dummy_Report>
    <Author>
        <ID>95</ID>
        <Name>fgh</Name>

    </Author>
    <Author>
        <ID>985</ID>
        <Name>abc</Name>

    </Author>
    <Author>
        <ID>958</ID>
        <Name>def</Name>

    </Author>
</Dummy_Report>

谢谢

0 个答案:

没有答案