使用 JSF returns java.lang.NullPointerException 错误生成 PDF Jasper 报告
Generating PDF Jasper Report with JSF returns java.lang.NullPointerException error
我是 JasperReport 的新手。我正在使用 Eclipse Luna 和 JasperSoft Studio。 jasperreports-3.7.6.jar。我正在尝试从 JSF 页面生成一个 pdf 文件,这就是我得到的:
java.lang.NullPointerException
exception
javax.servlet.ServletException
root cause
java.lang.NullPointerException
这是 JSF 页面
<h:body>
<h:form>
<h:inputText value="#{gradeManagedBean.code}" />
<h:inputText value="#{gradeManagedBean.name}" />
<h:commandButton actionListener ="#{gradeManagedBean.save}" value="submit"/>
</h:form>
</h:body>
JSF ManagedBean :
package managedBean;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import ejb.Service;
import ejb.ServiceLocal;
import entity.Grade;
@ManagedBean
@SessionScoped
public class GradeManagedBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@EJB
private ServiceLocal service;
private int code;
private String name;
private Grade grade = new Grade();
public GradeManagedBean(){
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
public ServiceLocal getService() {
return service;
}
public void setService(Service service) {
this.service = service;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void save(ActionEvent actionEvent) throws JRException, IOException{
grade.setCode(code);
grade.setName(name);
service.saveGrade(grade);
String codeString = ""+ grade.getCode() ;
Map<String, Object> hm = new HashMap<String, Object>();
hm.put("CODE",codeString);
hm.put("NAME",grade.getName());
System.out.println("Test 1");
String fileName = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/Report3.jasper");
System.out.println("Test 2");
File jasper = new File(fileName);
JasperPrint print = JasperFillManager.fillReport(jasper.getPath(),hm, new JREmptyDataSource());
System.out.println("Test 3");
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
System.out.println("Test 4");
response.addHeader("Content-disposition", "attachment; filename=Report2.pdf");
System.out.println("Test 5");
ServletOutputStream stream = response.getOutputStream();
System.out.println("Test 6");
JasperExportManager.exportReportToPdfStream(print, stream);
System.out.println("Test 7");
stream.flush();
stream.close();
System.out.println("Test 8");
FacesContext.getCurrentInstance().responseComplete();
System.out.println("Test 9");
}
}
非常感谢您的帮助
我通过添加 itext-pdfa-5.5.5 jar 解决了这个问题。
需要此 jar 从 *.jasper 文件生成 pdf 文件。
我也将 jasperreports jar 更新到最新版本。
我是 JasperReport 的新手。我正在使用 Eclipse Luna 和 JasperSoft Studio。 jasperreports-3.7.6.jar。我正在尝试从 JSF 页面生成一个 pdf 文件,这就是我得到的:
java.lang.NullPointerException
exception
javax.servlet.ServletException
root cause
java.lang.NullPointerException
这是 JSF 页面
<h:body>
<h:form>
<h:inputText value="#{gradeManagedBean.code}" />
<h:inputText value="#{gradeManagedBean.name}" />
<h:commandButton actionListener ="#{gradeManagedBean.save}" value="submit"/>
</h:form>
</h:body>
JSF ManagedBean :
package managedBean;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import ejb.Service;
import ejb.ServiceLocal;
import entity.Grade;
@ManagedBean
@SessionScoped
public class GradeManagedBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@EJB
private ServiceLocal service;
private int code;
private String name;
private Grade grade = new Grade();
public GradeManagedBean(){
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
public ServiceLocal getService() {
return service;
}
public void setService(Service service) {
this.service = service;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void save(ActionEvent actionEvent) throws JRException, IOException{
grade.setCode(code);
grade.setName(name);
service.saveGrade(grade);
String codeString = ""+ grade.getCode() ;
Map<String, Object> hm = new HashMap<String, Object>();
hm.put("CODE",codeString);
hm.put("NAME",grade.getName());
System.out.println("Test 1");
String fileName = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/Report3.jasper");
System.out.println("Test 2");
File jasper = new File(fileName);
JasperPrint print = JasperFillManager.fillReport(jasper.getPath(),hm, new JREmptyDataSource());
System.out.println("Test 3");
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
System.out.println("Test 4");
response.addHeader("Content-disposition", "attachment; filename=Report2.pdf");
System.out.println("Test 5");
ServletOutputStream stream = response.getOutputStream();
System.out.println("Test 6");
JasperExportManager.exportReportToPdfStream(print, stream);
System.out.println("Test 7");
stream.flush();
stream.close();
System.out.println("Test 8");
FacesContext.getCurrentInstance().responseComplete();
System.out.println("Test 9");
}
}
非常感谢您的帮助
我通过添加 itext-pdfa-5.5.5 jar 解决了这个问题。
需要此 jar 从 *.jasper 文件生成 pdf 文件。
我也将 jasperreports jar 更新到最新版本。