net.sf.jasperreports.engine.JRRuntimeException中的异常UncaughtExceptionHandler

时间:2016-01-05 07:42:12

标签: java maven netbeans jasper-reports

我有一个Netbeans Maven Java项目,我使用Jasper报告。虽然在Netbeans中一切都运行良好,但是当我运行带有依赖项的jar文件程序时,我会尝试从数据库获取数据报告。当我按下按钮返回报告中两个日期之间的数据时,程序抛出错误 - 线程中的异常" AWT-EventQueue-0"例外:从线程" AWT-EventQueue-0" 中的UncaughtExceptionHandler抛出net.sf.jasperreports.engine.JRRuntimeException。当我尝试从列表中获取数据(showMessageDialog)时,我从DB获得的数据一切正常。我使用shade插件创建我的jar文件。我添加了POM文件代码。我只看到" net.sf.jasper2docx",但没有其他jasper依赖。也许问题是POM中的依赖声明?在项目依赖项中,我有jasper2docx-apache-poi,jasper2docx-api和jasperreports-6.1.0 jars。

课程代码

    import beans.Spent;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.List;
    import javax.swing.JOptionPane;
    import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
    import net.sf.dynamicreports.report.builder.DynamicReports;
    import net.sf.dynamicreports.report.builder.column.Columns;
    import net.sf.dynamicreports.report.builder.column.TextColumnBuilder;
    import net.sf.dynamicreports.report.builder.style.StyleBuilder;
    import net.sf.dynamicreports.report.constant.HorizontalTextAlignment;
    import net.sf.dynamicreports.report.constant.VerticalTextAlignment;
    import net.sf.dynamicreports.report.definition.datatype.DRIDataType;
    import org.hibernate.Query;
    import org.hibernate.Session;

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            searchDate();
        }        
    private void searchDate() {
            if ((jDateChooser1.getDate() == null) || (jDateChooser2.getDate() == null)) {
                JOptionPane.showMessageDialog(rootPane, "Error text");
            } else {
                Date dataFrom = jDateChooser1.getDate();
                Date dataTo = jDateChooser2.getDate();

                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                String dataF = format.format(dataFrom);
                String dataT = format.format(dataTo);

                Date dateNow = new Date();
                String dataDabar = format.format(dateNow);

                Session session;
                session = HibernateUtil.getSessionFactory().openSession();

                try {
                    String hql2 = "FROM Spent E WHERE E.date BETWEEN '" + dataF + "' AND '" + dataT + "' ORDER BY E.date";
                    Query query = session.createQuery(hql2);
                    List<Spent> spentList= query.list();

                    StyleBuilder boldStyle = DynamicReports.stl.style().bold();
                    StyleBuilder centerStyle = DynamicReports.stl.style().setTextAlignment(HorizontalTextAlignment.CENTER,
                            VerticalTextAlignment.MIDDLE);
                    StyleBuilder leftStyle = DynamicReports.stl.style().setTextAlignment(HorizontalTextAlignment.LEFT, VerticalTextAlignment.JUSTIFIED);

                    JasperReportBuilder report = DynamicReports.report();
                    TextColumnBuilder<String> colName = Columns.column("Title", "name",
                            DynamicReports.type.stringType());
                    TextColumnBuilder<Date> colDate = Columns.column("Date", "date", (DRIDataType) DynamicReports.type.dateType());
                    TextColumnBuilder<Double> colPrice = Columns.column("Price", "price",
                            DynamicReports.type.doubleType()).setPattern("#,##0.00");
                    TextColumnBuilder<String> colCat = Columns.column("Category", "cat",
                            DynamicReports.type.stringType());
                    TextColumnBuilder<String> colComments = Columns.column("Comments", "comm",
                            DynamicReports.type.stringType());
                    report.setDataSource(spentList)
                            .columns(colDate.setWidth(30).setStyle(centerStyle), colName.setWidth(70), colPrice.setWidth(20), colCat.setWidth(35), colComments)
                            .show(false);
} catch (Exception ex) {
                    JOptionPane.showMessageDialog(rootPane, ex.getLocalizedMessage(), "Klaida", JOptionPane.PLAIN_MESSAGE);
                } finally {
                    session.close();
                }
            }

的pom.xml

<modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>Finansa</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.dynamicreports</groupId>
            <artifactId>dynamicreports-core</artifactId>
            <version>4.0.1</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>net.sf.jasper2docx</groupId>
            <artifactId>jasper2docx-apache-poi</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>com.toedter</groupId>
            <artifactId>jcalendar</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>maven</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>1.8.1</version>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>main.Login</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>create-my-bundle</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.2</version>
                <executions>
                    <!-- Run shade goal on package phase -->
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <!-- add Main-Class to manifest file -->
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>main.Login</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

的printStackTrace

Exception in thread "AWT-EventQueue-0" Exception in thread "AWT-EventQueue-0"
Exception: net.sf.jasperreports.engine.JRRuntimeException thrown from the Uncaug
htExceptionHandler in thread "AWT-EventQueue-0"
java.lang.NullPointerException
        at net.sf.dynamicreports.jasper.base.JasperChartCustomizer.customize(Jas
perChartCustomizer.java:42)
        at net.sf.jasperreports.engine.fill.JRFillChart.evaluateChart(JRFillChar
t.java:861)
        at net.sf.jasperreports.engine.fill.JRFillChart.evaluateRenderer(JRFillC
hart.java:819)
        at net.sf.jasperreports.engine.fill.JRFillChart.evaluate(JRFillChart.jav
a:808)
        at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFi
llElementContainer.java:258)
        at net.sf.jasperreports.engine.fill.JRFillFrame.evaluate(JRFillFrame.jav
a:163)
        at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFi
llElementContainer.java:258)
        at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:
454)
        at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillSummaryNoLastFo
oterSamePage(JRVerticalFiller.java:1071)
        at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillSummary(JRVerti
calFiller.java:1032)
        at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportEnd(JRVer
ticalFiller.java:293)
        at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVertic
alFiller.java:129)
        at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:
551)
        at net.sf.jasperreports.engine.fill.BaseReportFiller.fill(BaseReportFill
er.java:411)
        at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:122)
        at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.
java:667)
        at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillMa
nager.java:983)
        at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.toJasperPrin
t(JasperReportBuilder.java:312)
        at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.show(JasperR
eportBuilder.java:336)
        at main.SData.searchDate(SData.java:203)
        at main.SData.jButton1ActionPerformed(SData.java:102)
        at main.SData.access$000(SData.java:29)
        at main.SData$1.actionPerformed(SData.java:60)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
ce)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$500(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionP
rivilege(Unknown Source)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionP
rivilege(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionP
rivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

3 个答案:

答案 0 :(得分:0)

2.4.2升级后,尝试清洁然后包装。

我不确定Netbeans是否同时调用它的构建。也许从命令行尝试'mvn clean package',看看你生成的超级jar是否正常。在目标文件夹

答案 1 :(得分:0)

OP发现的解决方案:

  

必须添加Jasper报告插件依赖项。如果没有它,Jasper报告将无法100%工作。

<dependency>
 <groupId>com.alexnederlof</groupId>
 <artifactId>jasperreports-plugin</artifactId>
 <version>2.0</version>
</dependency>

答案 2 :(得分:0)

我遇到了类似的问题,这都是因为我的字体jar和相同的jasper项目在同一路径中有一个公用文件。然后,一个文件将覆盖另一个文件,并进入JRERuntime异常。

我所做的是合并文件,我使用了shadowJar,并且这样工作。您应该为Maven寻找类似的东西

shadowJar {
    mergeServiceFiles()
    mergeServiceFiles('jasperreports_extension.properties')
}