• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

java.lang.NullPointerException at java.io.ObjectInputStream...

 
Ranch Hand
Posts: 127
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a problem in my java class when I execute the JasperPrint command and the error is in my InputStream. Because I can´t find de solution to solve this problem, I send this question for you. Thanks.

Class 1:

package ITC_Prontuario_Package;

import java.awt.BorderLayout;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Map;
import javax.swing.JFrame;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.swing.JRViewer;
/**
*
* @author Cezar Apulchro
*/
public class Report_Utils_A01
{
   public static void openReport(
               String titulo,
               InputStream inputStream,
               Map parametros,
               Connection conexao) throws JRException
   {
       JasperPrint print = JasperFillManager.fillReport(
               inputStream, parametros, conexao);
       
       viewReportFrame( titulo, print );
   }
   
   public static void openReport(
               String titulo,
               InputStream inputStream,
               Map parametros,
               JRDataSource dataSource) throws JRException
   {
       JasperPrint print = JasperFillManager.fillReport(
               inputStream, parametros, dataSource);
       
       viewReportFrame( titulo, print );
   }
   
   private static void viewReportFrame( String titulo, JasperPrint print )
   {
       JRViewer viewer = new JRViewer(print);
       
       JFrame frameRelatorio = new JFrame(titulo);
       
       frameRelatorio.add(viewer, BorderLayout.CENTER);
       
       frameRelatorio.setSize(500, 500);
       
       frameRelatorio.setExtendedState(JFrame.MAXIMIZED_BOTH);
       
       frameRelatorio.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
       
       frameRelatorio.setVisible(true);
   }
}
Main class:

package ITC_Prontuario_Package;

import java.io.InputStream;
import java.io.FileInputStream;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import ITC_Prontuario_Package.PacientesDAO_A01;
import ITC_Prontuario_Package.Report_Utils_A01;
import ITC_Prontuario_Package.ConectarBanco;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
/**
*
* @author Cezar Apulchro
*/
public class Report_Main_A01
{
   private static final String inputS =
       /*"C:/Users/Administrador/JaspersoftWorkspace/ITC_Systems_Reports/Relatorio_Clientes_A01.jasper";*/
       /*"C:/Program Files (x86)/ITC_Systems/build/classes/ITC_Prontuario_Package/Relatorio_Clientes_A01.jrxml";*/
               "ITC_Relatorios_Package/Relatorio_Clientes_A01.jasper";
   
   public void abreRelatorio() throws JRException
   {
       PacientesDAO_A01 pcdao = new PacientesDAO_A01();
       /*InputStream inputStream = this.getClass().getResourceAsStream(inputS);*/
       /*InputStream inputStream;*/
               /*"/Relatorio_Clientes_A01.jasper");*/
               /*"ITC_Prontuario_Package/Relatorio_Clientes_A01.jasper");*/
               /*"C:/Program Files (x86)/ITC_Systems/build/classes/ITC_Prontuario_Package/Relatorio_Clientes_A01.jasper");*/
       
       Map parametros = new HashMap();
       
       try
       {
           InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(
           "C:/Users/Administrador/JaspersoftWorkspace/ITC_Systems_Reports/Relatorio_Clientes_A01.jasper");
           System.out.println("Carregando o InputStream..." + inputStream);
       
           Report_Utils_A01.openReport("Relacao_Clientes_A01", inputStream, parametros,
                   ConectarBanco.getConnection());
       }
         catch (JRException ex)
           {
               ex.printStackTrace();
           }
   }
   
   public static void main(String[] args) throws JRException
   {
       new Report_Main_A01().abreRelatorio();
       
   }
}


StackTrace:

Exception in thread "main" java.lang.NullPointerException
at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2653)
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2669)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:3146)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:858)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:354)
at net.sf.jasperreports.engine.util.ContextClassLoaderObjectInputStream.<init>(ContextClassLoaderObjectInputStream.java:56)
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:277)
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:262)
at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:415)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:838)
at ITC_Prontuario_Package.Report_Utils_A01.openReport(Report_Utils_A01.java:30)
at ITC_Prontuario_Package.Report_Main_A01.abreRelatorio(Report_Main_A01.java:48)
at ITC_Prontuario_Package.Report_Main_A01.main(Report_Main_A01.java:59)
 
Marshal
Posts: 64667
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Unfortunately you didn't quite get the code tags right: the [code=java] part goes before each class and [/code] after it. Your code would have looked like this:-
Main class:

Unfortunately the line numbers don't match those in your stack trace. Please show us which lines are involved in the exception.
 
Cezar Apulchro
Ranch Hand
Posts: 127
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your attention. When I run Report_Main_A01 the error occurs in line 43.
 
Saloon Keeper
Posts: 2617
329
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cezar Apulchro wrote:When I run Report_Main_A01 the error occurs in line 43.

How/where does ConectarBanco get initialized?
 
Cezar Apulchro
Ranch Hand
Posts: 127
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont´t know, in my stacktrace the nullpointer exception is referencing ObjectInputStream, in line 41, the return of command println is null. The ConectarBanco.getConnection is used in my project by other class and work ok.
 
Ron McLeod
Saloon Keeper
Posts: 2617
329
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cezar Apulchro wrote:... in my stacktrace the nullpointer exception is referencing ObjectInputStream, in line 41, the return of command println is null.

Since your report file is located in the file system and not in your Java application, you should be getting the input stream using something like FileInputStream.
 
Cezar Apulchro
Ranch Hand
Posts: 127
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the lines 29-33 are attempts to operate the line 39 and consequently the line 43 which is my JasperPrint line 25 of Report_Utils_A01. I will test with FileInputStream.
 
Cezar Apulchro
Ranch Hand
Posts: 127
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Morning Ron,
I change my class to use FileInputStream as your sugestion, now my class find the input jasper and generate a blanck viewReportFrame and new errors:

 
Cezar Apulchro
Ranch Hand
Posts: 127
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ron,
I solved this problem now the JRViewer is showing data of my DB.

How do I close this contact as resolved?

Thanks and best regards.

Cezar.
 
Campbell Ritchie
Marshal
Posts: 64667
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done sorting it out Please briefly tell us the exact solution, and click the button below the last post in the discussion.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!