使用JSF生成PDF Jasper Report会返回java.lang.NullPointerException错误

时间:2015-04-02 14:27:30

标签: jsf jasper-reports

我是JasperReport的新手。我正在使用Eclipse Luna和JasperSoft Studio。 JasperReports的-3.7.6.jar。我正在试图从JSF页面生成一个pdf文件,这就是我得到的:

  

显示java.lang.NullPointerException

     

例外

     

javax.servlet.ServletException

     

根本原因

     

显示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");

}

    }

非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我通过添加itext-pdfa-5.5.5 jar解决了这个问题。

需要此jar来从* .jasper文件生成pdf文件。

我还将jasperreports jar更新为最新版本。