[英]Why do I get error (ClassCastException) when trying to retrive bean from my data adapter?
我使用JasperSoft Studio創建了一個報告。
我指的是這篇有用的文章Jaspersoft Studio: Alex K 如何在數據適配器中使用Java Bean集合
我想檢索訂單清單。 Order類的定義如下:
public class Order {
private double price;
private int quantity;
private Product product;
// Getters & Setters
}
如果我想檢索價格或數量,則報告生成成功。 但是,一旦我檢索到產品,就會顯示:
細節:
net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: $F{product}.getName()
at com.jaspersoft.studio.editor.preview.view.control.ReportControler.fillReport(ReportControler.java:530)
at com.jaspersoft.studio.editor.preview.view.control.ReportControler.access$20(ReportControler.java:505)
...
Caused by: java.lang.ClassCastException: ru.alex.Product cannot be cast to ru.alex.Product
at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:277)
... 14 more
文件sample.jrxml是:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1 -->
<!-- 2017-05-18T13:29:14 -->
<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="sample" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ade9e357-e2d0-42bb-ae0d-000b69f4e2e9">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="JavaBeanCollection - orders"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="product" class="ru.alex.Product">
<fieldDescription><![CDATA[product]]></fieldDescription>
</field>
<field name="quantity" class="java.lang.Integer">
<fieldDescription><![CDATA[quantity]]></fieldDescription>
</field>
<field name="price" class="java.lang.Double">
<fieldDescription><![CDATA[price]]></fieldDescription>
</field>
<title>
<band height="80" splitType="Stretch">
<staticText>
<reportElement x="70" y="49" width="100" height="30" uuid="a19f5b7c-21ed-4c00-a224-af5cf7ef27ec"/>
<text><![CDATA[price]]></text>
</staticText>
<staticText>
<reportElement x="170" y="49" width="100" height="30" uuid="772c4807-25f7-4e7a-8a10-eba5232b92c7"/>
<text><![CDATA[quantity]]></text>
</staticText>
<staticText>
<reportElement x="270" y="49" width="140" height="30" uuid="613da9ef-6a5a-45b2-8c8f-c3cd450e66ed"/>
<text><![CDATA[product]]></text>
</staticText>
</band>
</title>
<detail>
<band height="130" splitType="Stretch">
<textField>
<reportElement x="70" y="0" width="100" height="30" uuid="a594224b-c015-4dab-b52b-6e317e76cea3"/>
<textFieldExpression><![CDATA[$F{price}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="170" y="0" width="100" height="30" uuid="b60503ca-f6bc-48dc-ad01-178d9befd805"/>
<textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="270" y="0" width="140" height="30" uuid="480bfb2f-2831-4700-8adc-f818bbbf6592"/>
<textFieldExpression><![CDATA[$F{product}.getName()]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
JRBeanCollection是以下內容:
public class MyImplementation implements JRDataSource {
// (...)
public static Collection<Order> getOrders() {
List<Order> orders = new ArrayList<Order>();
orders.add(new Order(1, "aa", new BigDecimal("1111.11"), 2, new Product("apples")));
orders.add(new Order(2, "bb", new BigDecimal("2222.22"), 10, new Product("oranges")));
return orders;
}
// (...)
}
你能告訴我例外的原因是什么?
它看起來像Jaspersoft Studio ( JSS )的錯誤。
我認為獲取ClassCastException的原因(請查看stacktrace的這一部分: Caused by: java.lang.ClassCastException: ru.alex.Product cannot be cast to ru.alex.Product
原因Caused by: java.lang.ClassCastException: ru.alex.Product cannot be cast to ru.alex.Product
)正在使用兩個jar文件:
是的,它是具有相同類的同一jar文件(物理上)。 看起來在類路徑中有多個jar的問題。
了解問題的根源后,我們可以很輕松地解決此問題。
在我們的bean類中,我們應該只保留一個jar-在JSS構建路徑中:
這意味着我們應該從數據適配器的屬性中刪除該jar 。 像這樣:
在此之后,最好重新啟動JSS 。
對於這個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="Report with Bean" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="JavaBeanCollection - orders"/>
<field name="product" class="ru.alex.Product">
<fieldDescription><![CDATA[product]]></fieldDescription>
</field>
<field name="quantity" class="java.lang.Integer">
<fieldDescription><![CDATA[quantity]]></fieldDescription>
</field>
<field name="price" class="java.lang.Double">
<fieldDescription><![CDATA[price]]></fieldDescription>
</field>
<detail>
<band height="30" splitType="Stretch">
<textField>
<reportElement x="10" y="0" width="100" height="30"/>
<textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="110" y="0" width="100" height="30"/>
<textFieldExpression><![CDATA[$F{price}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="210" y="0" width="100" height="30"/>
<textFieldExpression><![CDATA[$F{product}.getName()]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
我們正在JSS上成功獲取報告:
在Java項目中,一切工作正常(沒有任何黑魔法),因為在類路徑下,我們的bean類只有一個jar 。
如果有人在尋找解決方案,我的設置會稍有不同,因此解決方案也會稍有不同(雖然是相同的根本問題)。
我的設置包括-
我的主要項目包括報告中需要的所有類/子類。
帶有Bean工廠的單獨項目用於為報表加載對象。
指向#2中的工廠方法的數據適配器(無jar文件)。
彼此的構建路徑中包含了項目1和2。
我能夠通過將工廠類/方法從項目2移到項目1並完全刪除項目2來解決此問題。 我使用的是eclipse插件,但是直到重新啟動eclipse才起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.