Solved

java.lang.NullPointerException

Posted on 2004-10-04
5
275 Views
Last Modified: 2010-03-31
This is problem that I am facing currently.

I want to read a file only if the file name contains a certain string value.But, if the file name doesnt contain that string, it is throwing java.lang.NullPointerException.Please have a look at the code as I have pointed where the error occurs.Also, let me know if you have any further questions.Thanks for your help!

public static void unzipAndGetEntries(String zipfilename , Date fromDDt , Date toDDt, String profileName){
      
            
            
             System.out.println(" Zip File Name : " + zipfilename);
            
          try{
                 Date fromDDDt = fromDDt;
                 Date toDDDt = toDDt;
                 String transactionName = "";
                           String InbOrOut = rco.getDirection(); // inbound or outbound
                           InputStream f = null;
                                     ZipFile myzipfile = new ZipFile(zipfilename);
                  
                  for (int y = 0;y < wantedTransactionList.size();y++){
        transactionName = ((ProfileObject)wantedTransactionList.elementAt(y)).getName();              
             
             for (Enumeration e = myzipfile.entries(); e.hasMoreElements();){
                ZipEntry ze = ((ZipEntry)e.nextElement());
                String fileNm =  ze.getName();
      System.out.println(" File Name : " + fileNm);
               
                // for example file name abc.zip/outbound/processed/123456_52532.txt
               // it throws null pointer exception if it doesnt find "outbound" in the file name.
                if ((fileNm.indexOf(InbOrOut)!=-1) && (fileNm.indexOf("processed")!=-1))
      if (ze.getSize()>0){
                System.out.println(" File Name : " + fileNm);
                f = myzipfile.getInputStream(ze);
                        
                    readTheFileContent(f,fileNm,profileName,transactionName,fromDDDt,toDDDt);
                   
                }
               
                ze = null;
                fileNm = null;
               
               
              }
             
    }        
           fromDDDt = null;
           toDDDt = null;
           f.close();
           myzipfile.close();  
         
             
          } catch (IOException e){ System.out.println("Error while unzipping the files : " );
            e.printStackTrace();}        
              
          
      }  



0
Comment
Question by:g46905
  • 3
  • 2
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12222078
whats the stack trace?
0
 

Author Comment

by:g46905
ID: 12222146
Here it is. Please let me know if you want me to post the entire code.
java.lang.NullPointerException
      at ReportStats.unzipAndGetEntries(ReportStats.java:596)
      at ReportStats.getLogsFromArchives(ReportStats.java:531)
      at ReportStats.main(ReportStats.java(Compiled Code))

Thanks,
0
 
LVL 92

Expert Comment

by:objects
ID: 12222184
which is line 596
0
 

Author Comment

by:g46905
ID: 12222223
Here it is

           f.close(); // line 596

This is present in the code mentioned above.

Thanks,
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 12222237
remove that statement, and instead close after calling readTheFileContent:

                  f = myzipfile.getInputStream(ze);            
                  readTheFileContent(f,fileNm,profileName,transactionName,fromDDDt,toDDDt);
                  f.close();
                 
                }
               
                ze = null;
                fileNm = null;
               
               
              }
             
    }        
           fromDDDt = null;
           toDDDt = null;
           myzipfile.close();  
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now