I use Jaspersoft plugin in Eclipse. I have a field in .jrxml file that prints distinct Artists featured in an Album. I did it using Java Stream API. The type of that field is java.util.List. But I can't compile the file, because the method getArtists() is undefinted for the type of Object, yet the type of variable t is Track.
Console:
Started the compilation of the resource albumreport.jrxml net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file: 1. The method getArtists() is undefined for the type Object value = ((java.util.List)field_tracks.getValue()).stream().flatMap(t -> t.getArtists().stream()).map(a -> a.getName()).distinct().reduce("", (n1, n2) -> n1 + ", " + n2); //$JR_EXPR_ID=11$ <--------> 2. The method getName() is undefined for the type Object value = ((java.util.List)field_tracks.getValue()).stream().flatMap(t -> t.getArtists().stream()).map(a -> a.getName()).distinct().reduce("", (n1, n2) -> n1 + ", " + n2); //$JR_EXPR_ID=11$ <-----> 3. The method getArtists() is undefined for the type Object value = ((java.util.List)field_tracks.getOldValue()).stream().flatMap(t -> t.getArtists().stream()).map(a -> a.getName()).distinct().reduce("", (n1, n2) -> n1 + ", " + n2); //$JR_EXPR_ID=11$ <--------> 4. The method getName() is undefined for the type Object value = ((java.util.List)field_tracks.getOldValue()).stream().flatMap(t -> t.getArtists().stream()).map(a -> a.getName()).distinct().reduce("", (n1, n2) -> n1 + ", " + n2); //$JR_EXPR_ID=11$ <-----> 5. The method getArtists() is undefined for the type Object value = ((java.util.List)field_tracks.getValue()).stream().flatMap(t -> t.getArtists().stream()).map(a -> a.getName()).distinct().reduce("", (n1, n2) -> n1 + ", " + n2); //$JR_EXPR_ID=11$ <--------> 6. The method getName() is undefined for the type Object value = ((java.util.List)field_tracks.getValue()).stream().flatMap(t -> t.getArtists().stream()).map(a -> a.getName()).distinct().reduce("", (n1, n2) -> n1 + ", " + n2); //$JR_EXPR_ID=11$ <-----> 6 errors . at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:229) at net.sf.jasperreports.eclipse.builder.JasperReportCompiler.compileReport(JasperReportCompiler.java:289) at net.sf.jasperreports.eclipse.builder.JasperReportCompiler.compileReport(JasperReportCompiler.java:141) at net.sf.jasperreports.eclipse.builder.JasperReportsBuilder.compileJRXML(JasperReportsBuilder.java:212) at com.jaspersoft.studio.editor.action.CompileAction.actionCompile(CompileAction.java:159) at com.jaspersoft.studio.editor.action.CompileAction$1.run(CompileAction.java:101) at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63) Unable to create the binary file, check the compilation errors
Album.java:
@Entity @Table(name="album") @NamedQuery(name="Album.findAll", query="SELECT a FROM Album a") public class Album implements Serializable { private static final long serialVersionUID = 1L; // ... //bi-directional many-to-one association to Track @OneToMany(mappedBy="album") private List<Track> tracks; // ... public List<Track> getTracks() { return this.tracks; } }
Track.java:
@Entity @Table(name="track") @NamedQuery(name="Track.findAll", query="SELECT t FROM Track t") public class Track implements Serializable { private static final long serialVersionUID = 1L; // ... //bi-directional many-to-many association to Artist @ManyToMany @JoinTable( name="producestracks" , joinColumns={ @JoinColumn(name="trackid") } , inverseJoinColumns={ @JoinColumn(name="artistid") } ) private List<Artist> artists; // ... public List<Artist> getArtists() { return this.artists; } public void setArtists(List<Artist> artists) { this.artists = artists; } }
AlbumController.java
@RequestMapping(value = "/createJasperReport", method = RequestMethod.GET) public void createJasperReport(HttpServletResponse response) throws Exception { response.setContentType("text/html"); JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(alr.findAll()); InputStream inputStream = this.getClass().getResourceAsStream("/jasperreports/albumreport.jrxml"); JasperReport jasperReport = JasperCompileManager.compileReport(inputStream); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSource); inputStream.close(); response.setContentType("application/x-download"); response.addHeader("Content-disposition", "attachment; filename=albumreport.pdf"); OutputStream outputStream = response.getOutputStream(); JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream); }
Attachments:
1 Answer:
Posted on February 4, 2022 at 9:38am
The type of the field is java.util.List, but the compiler cannot know that it's a list of Track objects.
What you can do is to cast $F{tracks} to List<Track> (note the you'll have to fully qualify the Track class name), which should allow the compiler to infer Track as the type of the t variable.
Regards,
Lucian
I tried to solve it by casting, but now Track cannot be resolved to a type.
For some reason, Jaspersoft plugin doesn't recognize lambda operators as valid.
It's been a week and nobody did reply.