1

我在 struts2 中创建了一个应用程序,并使用 jasper 进行报告。但是我在浏览器中遇到了一些异常,如下所示。

谁能告诉我如何解决这个问题?

我所做的代码如下:

报告1.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<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="null" language="groovy" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="d4884678-fb84-4dba-a9f5-8d90964cd9b6">
    <style name="Title" forecolor="#FFFFFF" fontName="Times New Roman" fontSize="50" isBold="false" pdfFontName="Times-Bold"/>
    <style name="SubTitle" forecolor="#CCCCCC" fontName="Times New Roman" fontSize="18" isBold="false" pdfFontName="Times-Roman"/>
    <style name="Column header" forecolor="#666666" fontName="Times New Roman" fontSize="14" isBold="true" pdfFontName="Times-Roman"/>
    <style name="Detail" mode="Transparent" fontName="Times New Roman" pdfFontName="Times-Roman"/>
    <style name="Row" mode="Transparent" fontName="Times New Roman" pdfFontName="Times-Roman">
        <conditionalStyle>
            <conditionExpression><![CDATA[$V{REPORT_COUNT}%2 == 0]]></conditionExpression>
            <style mode="Opaque" backcolor="#F0EFEF"/>
        </conditionalStyle>
    </style>
    <queryString language="SQL">
        <![CDATA[SELECT
     login.`id` AS login_id,
     login.`name` AS login_name,
     login.`pass` AS login_pass
FROM
     `login` login]]>
    </queryString>
    <field name="login_id" class="java.lang.Integer"/>
    <field name="login_name" class="java.lang.String"/>
    <field name="login_pass" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="102" splitType="Stretch">
            <image>
                <reportElement uuid="dcbcd4a5-4dd4-4bd6-b29b-e5ad87b214c7" x="725" y="0" width="77" height="102"/>
                <imageExpression><![CDATA["cherry.jpg"]]></imageExpression>
            </image>
            <frame>
                <reportElement uuid="f92c03b7-8765-4013-9118-9daa8a9d5718" mode="Opaque" x="0" y="0" width="721" height="66" backcolor="#000000"/>
                <staticText>
                    <reportElement uuid="f17f45bb-09e4-4c23-a40a-8c63b01a3866" style="Title" x="0" y="0" width="370" height="66"/>
                    <box topPadding="4" leftPadding="4" bottomPadding="4" rightPadding="4"/>
                    <textElement verticalAlignment="Bottom">
                        <font isBold="false"/>
                    </textElement>
                    <text><![CDATA[Cherry Title]]></text>
                </staticText>
                <staticText>
                    <reportElement uuid="6f8f92fe-182e-42ff-ab37-154a07540218" style="SubTitle" x="370" y="37" width="316" height="29"/>
                    <textElement textAlignment="Right">
                        <font size="22" isBold="false"/>
                    </textElement>
                    <text><![CDATA[Cherry SubTitle]]></text>
                </staticText>
            </frame>
            <frame>
                <reportElement uuid="75ed2a4a-20d4-4a9b-9ec6-84228e21204d" mode="Opaque" x="0" y="70" width="721" height="32" forecolor="#000000" backcolor="#CC0000"/>
                <textField pattern="EEEEE dd MMMMM yyyy">
                    <reportElement uuid="5664ed89-b46d-4ff6-8d8a-c9bcfe819097" x="553" y="12" width="144" height="20" forecolor="#FFFFFF"/>
                    <textElement textAlignment="Right">
                        <font size="12"/>
                    </textElement>
                    <textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
                </textField>
            </frame>
        </band>
    </title>
    <pageHeader>
        <band splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="18" splitType="Stretch">
            <staticText>
                <reportElement uuid="62448e30-8579-4e8d-a460-078762b80f02" style="Column header" x="0" y="0" width="265" height="18" forecolor="#000000"/>
                <textElement>
                    <font isBold="false"/>
                </textElement>
                <text><![CDATA[login_id]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="5b3d753c-0b30-4735-9fb6-65539c1b35cf" style="Column header" x="265" y="0" width="265" height="18" forecolor="#000000"/>
                <textElement>
                    <font isBold="false"/>
                </textElement>
                <text><![CDATA[login_name]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="220c90dc-095d-4e70-ba22-60cf2488e184" style="Column header" x="530" y="0" width="265" height="18" forecolor="#000000"/>
                <textElement>
                    <font isBold="false"/>
                </textElement>
                <text><![CDATA[login_pass]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="18" splitType="Stretch">
            <frame>
                <reportElement uuid="8c04906d-c548-420f-a050-46507f5da0dc" style="Row" mode="Opaque" x="0" y="0" width="797" height="18"/>
                <textField isStretchWithOverflow="true">
                    <reportElement uuid="4e69e264-6905-4a70-a6e6-da0b5fd8b7ea" style="Detail" positionType="Float" x="0" y="0" width="265" height="18"/>
                    <textElement>
                        <font size="14"/>
                    </textElement>
                    <textFieldExpression><![CDATA[$F{login_id}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true">
                    <reportElement uuid="e96a557f-770b-4eeb-b7b0-485c86952fb7" style="Detail" positionType="Float" x="265" y="0" width="265" height="18"/>
                    <textElement>
                        <font size="14"/>
                    </textElement>
                    <textFieldExpression><![CDATA[$F{login_name}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true">
                    <reportElement uuid="246e8199-e7ae-43f6-935a-1695418b7ef0" style="Detail" positionType="Float" x="530" y="0" width="265" height="18"/>
                    <textElement>
                        <font size="14"/>
                    </textElement>
                    <textFieldExpression><![CDATA[$F{login_pass}]]></textFieldExpression>
                </textField>
            </frame>
        </band>
    </detail>
    <columnFooter>
        <band height="7" splitType="Stretch">
            <line>
                <reportElement uuid="3497c8b2-88b1-4d5e-898c-634159a172f5" positionType="FixRelativeToBottom" x="0" y="3" width="555" height="1"/>
                <graphicElement>
                    <pen lineWidth="0.5" lineColor="#999999"/>
                </graphicElement>
            </line>
        </band>
    </columnFooter>
    <pageFooter>
        <band height="25" splitType="Stretch">
            <frame>
                <reportElement uuid="9d9f5a83-dbbd-46b4-b053-c8a0e8387705" mode="Opaque" x="0" y="0" width="802" height="25" forecolor="#D0B48E" backcolor="#000000"/>
                <textField evaluationTime="Report">
                    <reportElement uuid="0360093b-11f7-46ff-92da-34c7459baa7c" style="Column header" x="757" y="3" width="40" height="20" forecolor="#FFFFFF"/>
                    <textElement verticalAlignment="Middle">
                        <font size="10" isBold="false"/>
                    </textElement>
                    <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement uuid="e6b56da8-90e0-495d-bacc-d04606b3257a" style="Column header" x="677" y="3" width="80" height="20" forecolor="#FFFFFF"/>
                    <textElement textAlignment="Right" verticalAlignment="Middle">
                        <font size="10" isBold="false"/>
                    </textElement>
                    <textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
                </textField>
                <textField pattern="EEEEE dd MMMMM yyyy">
                    <reportElement uuid="611e2099-8b7e-455d-a8c8-37a4ae4b3e0e" style="Column header" x="2" y="3" width="197" height="20" forecolor="#FFFFFF"/>
                    <textElement verticalAlignment="Middle">
                        <font size="10" isBold="false"/>
                    </textElement>
                    <textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
                </textField>
            </frame>
        </band>
    </pageFooter>
    <summary>
        <band splitType="Stretch"/>
    </summary>
</jasperReport>

JasperAction.java(struts2动作类)

package action;

import com.lowagie.text.Anchor;
import java.util.ArrayList;
import java.util.List;



import model.Person;
import model.Phone;

import com.opensymphony.xwork2.ActionSupport;

public class JasperAction extends ActionSupport {

    private static final long serialVersionUID = 1L;

    public String execute() throws Exception {
        return SUCCESS;  
    }

public static ArrayList<Person> getBeanCollection() {
        ArrayList as=new ArrayList();
                as.add("val");
        return as;
    }

}

struts.xml

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

 <package name="default" extends="jasperreports-default,struts-default">

    <action name="myJasperTest" class="action.JasperAction">

    <result name="success" type="jasper">
            <param name="location">/jasper/report1.jasper</param>
            <param name="dataSource">beanCollection</param>
            <param name="format">PDF</param>
        </result>

        <result name="error" type="jasper">/Error.jsp</result>

    </action>

</package>


</struts>

索引.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Struts2Jasper</title>
</head>
<body>
    <h4>Header</h4>     
    <ul>
      <li><a href="<s:url action='myJasperTest'/>">Click</a></li>   
    </ul> 
</body>
</html>

浏览器中显示的异常

类型 Exception report

信息

描述 The server encountered an internal error () that prevented it from fulfilling this request.

例外

javax.servlet.ServletException: java.lang.NullPointerException
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)

root cause

java.lang.NullPointerException
    net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:89)
    net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:601)
    org.apache.struts2.views.jasperreports.JasperReportsResult.doExecute(JasperReportsResult.java:240)
    org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:178)
    com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:348)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:253)
    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:221)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:150)
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:48)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:123)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:167)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:105)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:83)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:207)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:74)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:127)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:107)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:206)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:115)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:143)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:121)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:170)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:123)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:504)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.22 logs.
4

2 回答 2

0

异常的根本原因说:

java.lang.NullPointerException

net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:89)

探索JRFiller 4.0.1 API。它需要参数 Map。

在此行中唯一可能导致 aNullPointerException的情况是填充符为空的情况。此时不检查参数是否为空。

所以,检查你的数据源 <param name="dataSource">beanCollection</param>

附带说明一下,您没有提到您使用的是哪个 JasperReports 版本。总是提到 API 版本。这是一个很好的做法。

于 2013-01-30T07:10:02.503 回答
0

您的

<param name="dataSource">beanCollection</param> 

不可能是对的。

你应该返回一个ArrayListof Persons,但是你在声明和实例化它时没有指定typeand...你正在向String它添加一个,而不是一个Person!!

public static ArrayList<Person> getBeanCollection() {
    ArrayList as=new ArrayList();
            as.add("val"); // <- what ?!
    return as;
}

将其更改为:

public static ArrayList<Person> getBeanCollection() {        
    Person person = new Person();
    person.setName("val"); //setName, or set something...

    ArrayList<Person> as=new ArrayList()<Person>;
    as.add(person);
    return as;
}
于 2013-01-30T11:26:31.680 回答