Solved

java.lang.NullPointerException

Posted on 2004-10-04
5
281 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

705 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