Solved

java.lang.NullPointerException

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jsp insert to database example 2 63
ejb entity bean example issue 2 27
junit as external jar or library 7 39
How to log java errors in tomcat 8 24
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

733 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