Solved

java.lang.NullPointerException

Posted on 2004-10-04
5
277 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JUnit 4 @Before and @BeforeClass differences 3 59
ForLoop Example 3 48
how to see all occupied ports on windows 10 laptop 15 61
How to convert from xls to xlsx using java 7 36
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

808 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