How to Handle Jasper Report’s NoSuchMethodException
I was generating an ordinary report, when suddenly i met a weird error. My Exception showed a net.sf.jasperreports.engine.JRException
and a java.lang.NoSuchMethodException: Unknown property ''
when im using Jasper Report’s JRBeanCollectionDataSource
. I tought it was because my java bean is not Serializable, but changing my bean to Serializable still didnt fix my errors.
Here is my complete stacktrace
net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:123) at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getFieldValue(JRAbstractBeanDataSource.java:96) at net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.getFieldValue(JRBeanCollectionDataSource.java:100) at net.sf.jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset.java:818) at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:782) at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1433) at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:108) at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:908) at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:830) at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:85) at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:624) Caused by: java.lang.NoSuchMethodException: Unknown property '' at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1122) at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:686) at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:715)
Here is my source code snippet, take a look at line 12, that is where my exception happen.
@Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { List<Log> logs = logService.select(); FileInputStream fis = new FileInputStream("/ejournal.jasper"); BufferedInputStream bufferedInputStream = new BufferedInputStream(fis); Map<String, String> map = new HashMap<String, String>(); JRBeanCollectionDataSource jrbcds = new JRBeanCollectionDataSource(logs); JasperReport jasperReport = (JasperReport) JRLoader.loadObject(bufferedInputStream); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, jrbcds); }
Well actually it’s very easy to fix it, you should either
- Remove the empty field descriptions from the JRXML.
- Set the field descriptions to match the bean property names.
- Pass false as isUseFieldDescription when creating the bean data source, e.g. new JRBeanCollectionDataSource(data, false).
This is how i fixed it,
@Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { List<Log> logs = logService.select(); FileInputStream fis = new FileInputStream("/ejournal.jasper"); BufferedInputStream bufferedInputStream = new BufferedInputStream(fis); Map<String, String> map = new HashMap<String, String>(); JRBeanCollectionDataSource jrbcds = new JRBeanCollectionDataSource(logs,false); JasperReport jasperReport = (JasperReport) JRLoader.loadObject(bufferedInputStream); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, jrbcds); }
I hope it can help others, because i spend some ridicoulously amount of time looking for this workaround.
6 Comments
trabelsi
about 8 years agoyes !!!! i am very happy thx thx thx sir
Replyedwin
about 8 years agoHi trabelsi, glad it could help :)
Budi Oktaviyan
about 7 years agoThx bro. barusan nemu yang seperti ini errornya :))
Replyedwin
about 7 years agoya elah si pepe akwkakwk....
Divya
about 5 years agoThankyou so much..really helpfull.
ReplyRobert
about 4 years agoThank you very much Edwin , you really saved my day at work.
Reply