0

I have a Netbeans Maven Java project in which I use Jasper reports. While in Netbeans everything works good, but when I run my jar with dependencies file program works until i try to get report with data from database. When I press button to return data in report between two dates program throws error - Exception in thread "AWT-EventQueue-0" Exception: net.sf.jasperreports.engine.JRRuntimeException thrown from the UncaughtExceptionHandler in thread "AWT-EventQueue-0". When I try to get data from list (showMessageDialog), that I got from DB everything works OK. I create my jar file with shade plugin. I have added POM file code. I see only "net.sf.jasper2docx", but no other jasper dependencies. Maybe problem is with dependencies declaration in POM? In project dependencies I have jasper2docx-apache-poi, jasper2docx-api and jasperreports-6.1.0 jars.

CLASS CODE

    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)
Jongware
  • 22,200
  • 8
  • 54
  • 100
Dainius Java
  • 125
  • 1
  • 4
  • 14
  • Can you upgrade shade plugin to the latest version e.g 2.4.2 and retry? – javapapo Jan 05 '16 at 07:48
  • I upgraded (rewrote my pom in my example too), but still the same problem. But... when I press my button once again, the report showed up and no empty messagedialog appeared. Thank god its working but why it can not start from the first try? PS Thank you @javapapo – Dainius Java Jan 05 '16 at 08:00
  • Strange thing. I've posted not full code, just to make simple report without chart, footers, headers and so on. If I make a full report app shows same error and empty showmessagedialog (as it used to be). if I remove that code and leave only I posted above, program works. With bugs but works. I believe that something wrong with jasper dependency – Dainius Java Jan 05 '16 at 08:31
  • Can you post some more detail about the stacktrace, use ex.printStackTrace() in your try catch. (and catch Throwable) so that all errors are displayed. – Petter Friberg Jan 05 '16 at 09:45
  • Printstacktrace aded. It shows nullpoint to JasperChartCustomizer.customize, which is in dependencies and dynamicreports-core is declared in POM – Dainius Java Jan 05 '16 at 11:14
  • I have tried to manage report components like .setColumnTitle, .summary(chart) and it seems that they are not added to JAR file in build process. Properties like .setLabe, .setLabel for .subtotalsAtSummary are not shown and if report contains chart it doesn't even shows up and throws error. As I mentioned I believe that somehow jasperreports dependency is not added on build process. Is there any way to insert dependecy with "brutal" force? – Dainius Java Jan 07 '16 at 06:17
  • Please don't add "[Solved]" or similar to your title, and please don't edit the answer into your question. Instead, please [accept the answer](//stackoverflow.com/help/accepted-answer) that helped you the most. (If you found the answer yourself, you can even [self answer](//stackoverflow.com/help/self-answer) your question.) If you have enough [reputation](//stackoverflow.com/help/whats-reputation), you can also [upvote](//stackoverflow.com/privileges/vote-up) any answers that helped you. – Scott Weldon Jun 24 '16 at 21:27

3 Answers3

2

I had similar problems and it was all because my font jar and the same jasper project had a common file in the same path. Then one file would overwrite the other and go into JRERuntime Exception.

What I did was merge the files, I used shadowJar and it worked that way. You should look for something like that for maven

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

After the 2.4.2 upgrade,try cleaning and then packaging.

I am not sure if Netbeans is invoking it's build at the same time. Maybe try a ' mvn clean package' from the command line and see if your generated uber jar is ok. in the target folder

javapapo
  • 1,342
  • 14
  • 26
  • where I should write mvn clean package? In windows commander? If yes I don't know where, because if I write this in C:Windows/system 32 it doesn't works – Dainius Java Jan 05 '16 at 08:35
  • I guess you are invoking maven from the IDE, and you are using the internal Maven installation. Try to do this from your IDE. There must be some Maven tab in your IDE, with targets and goals to run. See [here](http://stackoverflow.com/questions/9458928/netbeans-7-1-invoking-actions-other-than-build-and-clean-build) for example – javapapo Jan 05 '16 at 08:36
0

Solution found by OP:

There must be added Jasper reports plugin dependency. Without it Jasper reports will not work at 100%.

<dependency>
 <groupId>com.alexnederlof</groupId>
 <artifactId>jasperreports-plugin</artifactId>
 <version>2.0</version>
</dependency>
Jongware
  • 22,200
  • 8
  • 54
  • 100