1
votes

I'm new with JasperReport. I'm using Eclipse Luna and JasperSoft Studio. jasperreports-3.7.6.jar. I'm triying to generate a pdf file from a JSF page and this is what i'm getting this :

java.lang.NullPointerException

exception

javax.servlet.ServletException

root cause

java.lang.NullPointerException

this is the JSF page

<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>

The 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");

}

    }

Your help will be very appreciated

1
What do those sout statements trace (how is the save() method traversed through)? And I do not think you need a verbose session scoped managed for generating Jasper reports. - Tiny
What exactly is evaluating to null? - kolossus
the sout statement are just to verify the problem whereit accures. it is called when the commandButton is clicked. - Devlopas
the itextPdfa jar was missing. I added it and it's working now. - Devlopas

1 Answers

0
votes

I resolved this issu by adding the itext-pdfa-5.5.5 jar.

this jar is needed to generate pdf files from *.jasper files.

I also updated the jasperreports jar to the latest version.