Jasper Reports:JRBeanCollectionDataSource无法解析为某种类型 [英] Jasper Reports: JRBeanCollectionDataSource cannot be resolved to a type

查看:261
本文介绍了Jasper Reports:JRBeanCollectionDataSource无法解析为某种类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在iReports中构建一个图表,当我在Eclipse中编译时,我收到以下错误:

  net.sf .jasperreports.engine.JRException:编译报表表达式类文件时遇到错误:
1. net.sf.jasperreports.engine.JRBeanCollectionDataSource无法解析为类型
value = new net.sf.jasperreports .engine.JRBeanCollectionDataSource(((java.lang.String中)field_chartData46xAxis.getValue())); // $ JR_EXPR_ID = 11 $
< ------------------------------------- --------------->
2. net.sf.jasperreports.engine.JRBeanCollectionDataSource无法解析为类型
value = new net.sf.jasperreports.engine.JRBeanCollectionDataSource(((java.lang.String)field_chartData46xAxis.getOldValue( ))); // $ JR_EXPR_ID = 11 $
< ------------------------------------- --------------->
3. net.sf.jasperreports.engine.JRBeanCollectionDataSource无法解析为类型
value = new net.sf.jasperreports.engine.JRBeanCollectionDataSource(((java.lang.String)field_chartData46xAxis.getValue( ))); // $ JR_EXPR_ID = 11 $
< ------------------------------------- ---------------> 3个错误

at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:204)
at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:240)
at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:173)
at net .sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:448)
at org.reportprotojava.protosheet.Program.main(Program.java:122)

我将 ProtoReport ArrayList (我测试时只有一个)传递给jasper编译器。 ProtoReport 类包含一个 ChartData 类,它又有两个类型为Double的 ArrayList ,一个用于X轴,另一个用于Y- axis。

ProtoReport ChartData 类和主程序的定义如下(略有删节):



ProtoReport 类:

 包org.reportprotojava.protosheet; 

import java.util.ArrayList;

公共类ProtoReport {


private String outputFileName;
私有字符串标题;
private String logoLocation;
private String paragraphText;
private ArrayList< String>资料表;
private String picLocation;
private int [] [] graphData; // TODO决定如何存储图表数据
private ChartData chartData;
private String path;




//默认构造函数
public ProtoReport(){

//初始化对象字段
outputFileName =PrototypeReport;
title =原型报告;
paragraphText =默认文字;

tableData = new ArrayList< String>();
chartData = new ChartData();

//设置工作目录的路径
path = System.getProperty(user.dir);

//默认为假设报告位置
//(即与.jrxml和.jasper文件相同的文件夹)
logoLocation = path +\\reports\\\ \\logo.jpg;
picLocation = path +\\reports\\pic.jpg;

}

ChartData 类:

  package org.reportprotojava.protosheet; 

import java.util.ArrayList;

公共类ChartData {

private ArrayList< Double> x轴;
private ArrayList< Double> Y轴;
/ **
* @param xAxis
* @param yAxis
* /

//默认构造函数
public ChartData(){
xAxis = new ArrayList< Double>();
yAxis = new ArrayList< Double>();
}



//构造函数
public ChartData(ArrayList< Double> xAxis,ArrayList< Double> yAxis){
super( );
this.xAxis = xAxis;
this.yAxis = yAxis;
}

主程序

 公共课程{

/ **
* @param args
*
*程序运行我们的ProtoReport类及其支持类
*最后我们将从
*先前定义的.jrxml文件生成一个.pdf
* /

//为图表生成一些随机数据
public static ArrayList< Double> randomData(int size){
ArrayList< Double> arrayList = new ArrayList< Double>();
double randNumber;

for(int i = 0; i< size; i ++){
randNumber = Math.random();
arrayList.add(randNumber);
}

返回arrayList;
}

public static void main(String [] args){

ArrayList< ProtoReport> listOfReports = new ArrayList< ProtoReport>();

ProtoReport protoReport1 = new ProtoReport();
ProtoReport protoReport2 = new ProtoReport();

//简单字段和文本
protoReport1.setTitle(Example< br /> Fact Sheet);

protoReport1.setLogoLocation(protoReport1.getPath()+\\reports\\logo.gif);

ChartData chartData = new ChartData();
chartData.setYAxis(randomData(20));
for(Double i =(double)0; i< chartData.getYAxis()。size(); i ++){
chartData.getXAxis()。add(i);
}

protoReport1.setChartData(chartData);
String jrxmlLocation = protoReport1.getPath()
+\\reports \\ReportPrototype.jrxml;
String outputFileName = protoReport1.getPath()
+\\reports \\generated\\+ protoReport1.getOutputFileName()+。pdf;

listOfReports.add(protoReport1);

//并将ArrayList包装在JRBeanCollectionDataSource中
JRBeanCollectionDataSource beanBurritoWrap = new JRBeanCollectionDataSource(listOfReports);

//构建jasper报告
JasperReport jasperReport;
JasperPrint jasperPrint;
HashMap< String,Object> hashMap = new HashMap<>();
boolean reportCreated;

try {

jasperReport = JasperCompileManager.compileReport(jrxmlLocation);
jasperPrint = JasperFillManager.fillReport(jasperReport,hashMap,beanBurritoWrap);
JasperExportManager.exportReportToPdfFile(jasperPrint,outputFileName);
reportCreated = true;
}
catch(JRException e){
e.printStackTrace();
reportCreated = false;
}

}

我用Google搜索了问题提出这个 this ,并阅读关于jasper sourceforge的数据源部分但这些都没有帮助解决问题,我已经确定我已经使用了

  new net.sf.jasperreports.engine.JRBeanCollectionDataSource($ F {chartData.xAxis})


(当我更改字段名称时更新它)并且字段在iReport属性中设置为列表类型。



这是我的 .jrxml

 <?xml version =1.0encoding =UTF-8?> 
< jasperReport xmlns =http://jasperreports.sourceforge.net/jasperreportsxmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http: //jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsdname =ReportPrototype.jrxmlpageWidth =595pageHeight =842columnWidth =495leftMargin = 57rightMargin =43topMargin =43bottomMargin =43uuid =10825c57-f953-4166-bf03-8ecabe8a8f47>
< property name =ireport.zoomvalue =0.75/>
< property name =ireport.xvalue =0/>
< property name =ireport.yvalue =232/>
< subDataset name =ChartDatauuid =fc9ec0af-3e1a-40a7-8eb4-9ad30a266dee>
< field name =chartData.xAxisclass =java.lang.String/>
< / subDataset>
< queryString language =SQL>
<![CDATA []]>
< / queryString>
< field name =titleclass =java.lang.String/>
< field name =logoLocationclass =java.lang.String/>
< field name =picLocationclass =java.lang.String/>
< field name =chartData.xAxisclass =java.lang.String/>
< detail>
< band height =740splitType =Stretch>
< textField isStretchWithOverflow =truepattern =>
< reportElement uuid =519c6bb5-72f9-4c25-8e91-47865ae0c9dfmode =Opaquex =39y =75width =378height =45forecolor =#000099 />
< textElement textAlignment =CenterverticalAlignment =Middlemarkup =html>
< font size =26/>
< / textElement>
< textFieldExpression><![CDATA [$ F {title}]]>< / textFieldExpression>
< / textField>
< image onErrorType =Icon>
< reportElement uuid =3759a707-32a4-49ef-a9c6-b0ad7136f738x =216y =264width =279height =246/>
< imageExpression><![CDATA [$ F {picLocation}]]>< / imageExpression>
< / image>
< image onErrorType =Icon>
< reportElement uuid =f989f871-32ea-4f13-ae3f-3f487cde76ddx =295y =0width =200height =42/>
< imageExpression><![CDATA [$ F {logoLocation}]]>< / imageExpression>
< / image>
< xyLineChart>
< chart>
< reportElement uuid =ae87fc13-b92e-4a2a-b218-d395343f6028x =0y =537width =495height =203/>
< chartTitle />
< chartSubtitle />
< chartLegend />
< / chart>
< xyDataset>
< dataset>
< datasetRun subDataset =ChartDatauuid =de7fb84d-17ea-4e5e-82bf-2015e72e4982>
< dataSourceExpression><![CDATA [new net.sf.jasperreports.engine.JRBeanCollectionDataSource($ F {chartData.xAxis})]]>< / dataSourceExpression>
< / datasetRun>
< / dataset>
< / xyDataset>
< linePlot>
< plot />
< / linePlot>
< / xyLineChart>
< / band>
< / detail>
< pageFooter>
< band height =16>
< break>
< reportElement uuid =0d30dea4-a6af-4e41-b7be-c288f3188dbfx =0y =11width =100height =1/>
< / break>
< / band>
< / pageFooter>



在iReports中我试过了:

在字段字段下创建和命名字段 ProtoReport.ChartData.xAxis ProtoReport.ChartData.yAxis ChartData 来源下,我添加了

- 将字段重命名为 ChartData.xAxis ChartData.yAxis

- 将字段重命名为 chartData.xAxis chartData.yAxis

- 只需使用字段下的字段

- 只需使用ChartData下的字段 - 字段



所有给我一个错误。我有什么想法我做错了吗?



其他问题:

- 现在我的图表可能只生成xAxis数据点。如何将一个 ArrayList 的内容用于X轴,另一个用于Y轴?即 ChartData 对象中的 xAxis yAxis 字段。

- randomData()我在之前声明的方法在我声明 静态 之前不会运行,为什么会这样?



编辑

更清楚地说明我的问题:如何命名我的字段,设置数据源以及配置数据集以解决此问题?

我遵循评论中列出的教程(抱歉非超链接;作为新用户我已经使用了所有超链接),并根据我的需要对其进行了更改,但他的数据结构比我更简单,我想学习如何在jasper报告中处理更复杂的对象和数据集。

解决方案

在.jrxml中,图表的数据源定义为:

 <![CDATA [new net.sf.jasperreports .engine.JRBeanCollectionDataSource($ F {chartData.xAxis})]]> 

当您选择使用数据源表达式在详细信息窗格中的连接/数据源exp 下,iReports的默认表达式为:

  new net.sf.jasperreports.engine.JREmptyDataSource(1)

但是这个表达式错了,因为 .data 丢失了。它应该是:

  new net.sf.jasperreports.engine.data.JREmptyDataSource(1)

然后我将 JREmptyDataSource(1)更改为 JRBeanCollectionDataSource($ F {chartData}) 当然



这修复了编译时错误,然后我不得不重新组织并重新定义我的列表和POJO,以便我可以轻松访问数据点对 ChartData 对象的列表。基本上这意味着我没有一个包含两个双精度列表的ChartData对象(一个用于X轴,一个用于Y轴),我现在有一个ChartData对象列表,每个对象只有一个XY点。这似乎现在运作良好。


I'm building a chart in iReports and when I compile in Eclipse I get the following error:

net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file:
1. net.sf.jasperreports.engine.JRBeanCollectionDataSource cannot be resolved to a type
            value = new     net.sf.jasperreports.engine.JRBeanCollectionDataSource(((java.lang.String)field_chartData46xAxis.getValue())); //$JR_EXPR_ID=11$
                        <---------------------------------------------------->
2. net.sf.jasperreports.engine.JRBeanCollectionDataSource cannot be resolved to a type
            value = new net.sf.jasperreports.engine.JRBeanCollectionDataSource(((java.lang.String)field_chartData46xAxis.getOldValue())); //$JR_EXPR_ID=11$
                        <---------------------------------------------------->
3. net.sf.jasperreports.engine.JRBeanCollectionDataSource cannot be resolved to a type
            value = new net.sf.jasperreports.engine.JRBeanCollectionDataSource(((java.lang.String)field_chartData46xAxis.getValue())); //$JR_EXPR_ID=11$
                        <---------------------------------------------------->3 errors

at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:204)
at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:240)
at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:173)
at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:448)
at org.reportprotojava.protosheet.Program.main(Program.java:122)

I pass an ArrayList of ProtoReport (only one right now while I test) to the jasper compiler. The ProtoReport class contains a ChartData class which in turns has two ArrayList of type Double, one for the X-axis and one for the Y-axis.
The definitions of the ProtoReport, ChartData classes, and main program are as follows(slightly abridged):

ProtoReport Class:

package org.reportprotojava.protosheet;

import java.util.ArrayList;

public class ProtoReport {


private String outputFileName;
private String title;
private String logoLocation;
private String paragraphText;
private ArrayList<String> tableData;
private String picLocation;
private int[][] graphData;  //TODO decide how to store chart data
private ChartData chartData;
private String path;




//default constructor
public ProtoReport() {

    // Initialize object fields
    outputFileName = "PrototypeReport";
    title = "Prototype Report";
    paragraphText = "Default text";

    tableData = new ArrayList<String>();
    chartData = new ChartData();

    //set path to working directory
    path = System.getProperty("user.dir");

    //default to assumed report location 
    //(ie same folder as .jrxml and .jasper files)
    logoLocation = path + "\\reports\\logo.jpg";
    picLocation = path + "\\reports\\pic.jpg";

}

ChartData Class:

package org.reportprotojava.protosheet;

import java.util.ArrayList;

public class ChartData {

private ArrayList<Double> xAxis;
private ArrayList<Double> yAxis;
/**
 * @param xAxis
 * @param yAxis
 */

//default constructor
public ChartData(){
    xAxis = new ArrayList<Double>();
    yAxis = new ArrayList<Double>();
}



//constructor
public ChartData(ArrayList<Double> xAxis, ArrayList<Double> yAxis) {
    super();
    this.xAxis = xAxis;
    this.yAxis = yAxis;
}

Main Program

public class Program {

/**
 * @param args
 * 
 *Program runs our ProtoReport class and its supporting classes 
 * In the end we will have generated a .pdf from the 
 * previously defined .jrxml file
 */

//Generate some random data for the chart
public static ArrayList<Double> randomData(int size) {
    ArrayList<Double> arrayList = new ArrayList<Double>();
    double randNumber;

    for (int i = 0; i < size; i++) {
        randNumber = Math.random();
        arrayList.add(randNumber);
    }

    return arrayList;
}

public static void main(String[] args) {

    ArrayList<ProtoReport> listOfReports = new ArrayList<ProtoReport>();

    ProtoReport protoReport1 = new ProtoReport();
    ProtoReport protoReport2 = new ProtoReport();

 //Simple Fields and text
    protoReport1.setTitle("Example<br/>Fact Sheet");

    protoReport1.setLogoLocation(protoReport1.getPath() + "\\reports\\logo.gif");

 ChartData chartData = new ChartData();
    chartData.setYAxis(randomData(20));
    for (Double i = (double) 0; i < chartData.getYAxis().size(); i++) {
        chartData.getXAxis().add(i);
    }

        protoReport1.setChartData(chartData);  
 String jrxmlLocation = protoReport1.getPath() 
                    + "\\reports\\ReportPrototype.jrxml";
    String outputFileName = protoReport1.getPath() 
            + "\\reports\\generated\\" + protoReport1.getOutputFileName() + ".pdf";

    listOfReports.add(protoReport1);

//and wrap the ArrayList in a JRBeanCollectionDataSource
    JRBeanCollectionDataSource beanBurritoWrap = new JRBeanCollectionDataSource(listOfReports);

    //build the jasper report
    JasperReport jasperReport;
    JasperPrint jasperPrint;
    HashMap<String, Object> hashMap = new HashMap<>();
    boolean reportCreated;

    try {

        jasperReport = JasperCompileManager.compileReport(jrxmlLocation);
        jasperPrint = JasperFillManager.fillReport(jasperReport, hashMap, beanBurritoWrap);
        JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);
        reportCreated=true;
    }
    catch (JRException e) {
          e.printStackTrace();
          reportCreated=false;
    }

}

I've googled the problem and come up with this and this, and read the datasource section on jasper sourceforge but none of these have helped solve the problem, and I've made sure that I've used

 new net.sf.jasperreports.engine.JRBeanCollectionDataSource($F{chartData.xAxis})

in my chart data source expression(and updated it when I change field names) and the fields are set to the List type in the iReport properties.

Here is my .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="ReportPrototype.jrxml" pageWidth="595" pageHeight="842" columnWidth="495" leftMargin="57" rightMargin="43" topMargin="43" bottomMargin="43" uuid="10825c57-f953-4166-bf03-8ecabe8a8f47">
<property name="ireport.zoom" value="0.75"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="232"/>
<subDataset name="ChartData" uuid="fc9ec0af-3e1a-40a7-8eb4-9ad30a266dee">
    <field name="chartData.xAxis" class="java.lang.String"/>
</subDataset>
<queryString language="SQL">
    <![CDATA[]]>
</queryString>
<field name="title" class="java.lang.String"/>
<field name="logoLocation" class="java.lang.String"/>
<field name="picLocation" class="java.lang.String"/>
<field name="chartData.xAxis" class="java.lang.String"/>
<detail>
    <band height="740" splitType="Stretch">
        <textField isStretchWithOverflow="true" pattern="">
            <reportElement uuid="519c6bb5-72f9-4c25-8e91-47865ae0c9df" mode="Opaque" x="39" y="75" width="378" height="45" forecolor="#000099"/>
            <textElement textAlignment="Center" verticalAlignment="Middle" markup="html">
                <font size="26"/>
            </textElement>
            <textFieldExpression><![CDATA[$F{title}]]></textFieldExpression>
        </textField>
        <image onErrorType="Icon">
            <reportElement uuid="3759a707-32a4-49ef-a9c6-b0ad7136f738" x="216" y="264" width="279" height="246"/>
            <imageExpression><![CDATA[$F{picLocation}]]></imageExpression>
        </image>
        <image onErrorType="Icon">
            <reportElement uuid="f989f871-32ea-4f13-ae3f-3f487cde76dd" x="295" y="0" width="200" height="42"/>
            <imageExpression><![CDATA[$F{logoLocation}]]></imageExpression>
        </image>
        <xyLineChart>
            <chart>
                <reportElement uuid="ae87fc13-b92e-4a2a-b218-d395343f6028" x="0" y="537" width="495" height="203"/>
                <chartTitle/>
                <chartSubtitle/>
                <chartLegend/>
            </chart>
            <xyDataset>
                <dataset>
                    <datasetRun subDataset="ChartData" uuid="de7fb84d-17ea-4e5e-82bf-2015e72e4982">
                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JRBeanCollectionDataSource($F{chartData.xAxis})]]></dataSourceExpression>
                    </datasetRun>
                </dataset>
            </xyDataset>
            <linePlot>
                <plot/>
            </linePlot>
        </xyLineChart>
    </band>
</detail>
<pageFooter>
    <band height="16">
        <break>
            <reportElement uuid="0d30dea4-a6af-4e41-b7be-c288f3188dbf" x="0" y="11" width="100" height="1"/>
        </break>
    </band>
</pageFooter>

In iReports I've tried:
-Creating and naming fields ProtoReport.ChartData.xAxis and ProtoReport.ChartData.yAxis under Fields and the fields under the ChartData source I added
-Renaming the fields to ChartData.xAxis and ChartData.yAxis
-Renaming the fields to chartData.xAxis and chartData.yAxis
-Just using the fields under Field
-Just using the fields under ChartData - Fields

All give me an error. Any ideas what I'm doing wrong?

Additional questions:
-As it stands my chart will likely only produce the xAxis data points. How can I use the contents of one ArrayList for the X-axis and the other for the Y-Axis? ie my xAxis and yAxis fields in ChartData object.
-The randomData() method I declared before main would not run until I declared it static, why is that?

Edit
To state my question more clearly: How do I name my fields, setup my datasources, and configure my datasets in order to solve this problem?
I am following the tutorial listed in the comments (sorry for the non-hyper link; as a new user I've used all my hyperlinks already), and making changes to it for my needs, but his data structure is more simple than mine and I'd like to learn how to deal with more complex objects and datasets in jasper reports.

解决方案

In the .jrxml the data source for the chart is defined as:

<![CDATA[new net.sf.jasperreports.engine.JRBeanCollectionDataSource($F{chartData.xAxis})]]>

This come from the iReports 4.7.0 autofill when you select Use datasource expression under Connection/Datasource exp in the details pane, the default expression from iReports is:

 new net.sf.jasperreports.engine.JREmptyDataSource(1)

But this expression is wrong because .data is missing. It should read:

 new net.sf.jasperreports.engine.data.JREmptyDataSource(1)

and then I changed the JREmptyDataSource(1) to JRBeanCollectionDataSource($F{chartData}) of course

This fixes the compile time error and then I had to reorganize and redefine my Lists and POJO's so that I could easily access the data point pairs in my List of ChartData objects. Essentially this meant that instead of a single ChartData object with two lists of doubles (one for X-Axis and one for Y-Axis), I now have a list of ChartData objects, each with just one XY point. This seems to work well for now.

这篇关于Jasper Reports:JRBeanCollectionDataSource无法解析为某种类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆