Solved

java.lang.NullPointerException

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

809 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