InputStream java.util.zip.ZipFile.getInputStream(ZipEntry arg 0) throws IOException

I am getting

java.lang.NullPointerException
      at java.util.zip.ZipFile.getInputStream(ZipFile.java:191)
      at LineCount.unzipAndGetEntries(LineCount.java:642)
      at LineCount.processFromAchivesFolder(LineCount.java:575)
      at LineCount.main(LineCount.java(Compiled Code))

This is happening when I am trying to open a particular file. Can you please tell me what I am supposed to check?

Thanks a lot
g46905Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
CEHJConnect With a Mentor Commented:
Check the integrity of the zip file by other means first
0
 
kiranhkCommented:
you need to tell what you r trying to do and when/where u r getting this exception. Better still you can post the code here
0
 
g46905Author Commented:
Here is the method where I am having this error.I commented the line where this exception is being thrown.Please let me know if I can help you with some more information.Please search for <<<< so that it will be easier for you to search for the line.Thanks.

public static void unzipAndGetEntries(String zipfilename,String sProfile, Date fromDD, Date toDate) {
   System.out.println(" Zip File Name : "  + zipfilename);
                            
    try {
       ZipFile myzipfile = new ZipFile(zipfilename);
       ZipEntry ze = null;
      InputStream iss = null;
                String sFileInZip = null;
       Vector vFileName = null;
       String sZipDirection = null;;
       for(Enumeration e = myzipfile.entries(); e.hasMoreElements();)
       {
          ze = (ZipEntry)e.nextElement();
          sFileInZip = ze.getName();
          if(sFileInZip.endsWith(".log"))
            {
              iss = myzipfile.getInputStream(ze);
              vFileName = processZipEntry(iss,fromDD, toDate);
              iss.close();
              iss = null;
              } // if
           ze = null;
           sFileInZip = null;
      }  // for
                  
   for(Enumeration e = myzipfile.entries(); e.hasMoreElements();)
  {
      ze = (ZipEntry)e.nextElement();
      sFileInZip = ze.getName();
      for (int vf = 0;vf<vFileName.size();vf++){
       String sFileName = vFileName.elementAt(vf).toString();
                                              
      if (sFileInZip.indexOf("outbound/processed")!=-1)
      sZipDirection = "outbound";
       if (sFileInZip.indexOf("inbound/processed")!=-1)
                 sZipDirection = "inbound";
                             
                             
                                                    
      if(sFileInZip.endsWith(sFileName) && (sFileInZip.indexOf("processed")!=-1)){
      System.out.println("sFileInZip : 1 " + sFileInZip );
      System.out.println("sFileInZip Size : " + sFileInZip.length() );
                iss = myzipfile.getInputStream(ze);// here is the Zip exception <<<<<----------------------------------------------
      
                BufferedReader brZipFile = new BufferedReader(new InputStreamReader(iss));
       updateLineCount(brZipFile,sProfile,sZipDirection);
      brZipFile.close();
      brZipFile = null;
       iss.close();
       iss = null;
        ze = null;
                              
       } // if
   } // for  
      sFileInZip = null;              
      } // for
                     
               myzipfile.close();
               myzipfile = null;
              String sFileName = null;              
             
      } catch(OutOfMemoryError ex) { System.out.println(" Memory Exception........ " + ex.toString());
   } // try/catch
    catch(IOException e) {System.out.println( " unzipAndGetEntries.....");
     e.printStackTrace(); } // try/catch
 }
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
kiranhkConnect With a Mentor Commented:
This is what i tested out. it is working fine over here. if u have the problem again maybe u need to post all the methods getting called from this method to check why it is throwing error

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/*
 * Created on Oct 27, 2004
 * @author ZJNRSHA (kiran.k.krishnamurthy@verizon.com)
 *
 */

/**
 * @author ZJNRSHA
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class TestZip {

    public static void main(String[] args) {
        TestZip tz = new TestZip();
        tz.unzipAndGetEntries("startup.jar","kiran",new Date(),new Date());
    }
   
    public void unzipAndGetEntries(String zipfilename,String sProfile, Date fromDD, Date toDate) {
        System.out.println(" Zip File Name : "  + zipfilename);
                             
         try {
           ZipFile myzipfile = new ZipFile(zipfilename);
           ZipEntry ze = null;
          InputStream iss = null;
                     String sFileInZip = null;
           Vector vFileName = null;
           String sZipDirection = null;;

           for(Enumeration e = myzipfile.entries(); e.hasMoreElements();)
           {
              ze = (ZipEntry)e.nextElement();
              sFileInZip = ze.getName();
              if(sFileInZip.endsWith(".log"))
                {
                  iss = myzipfile.getInputStream(ze);
             //     vFileName = processZipEntry(iss,fromDD, toDate);
                  iss.close();
                  iss = null;
                  } // if
               ze = null;
               sFileInZip = null;
          }  // for
 
        for(Enumeration e = myzipfile.entries(); e.hasMoreElements();)
       {
          ze = (ZipEntry)e.nextElement();
          iss =  myzipfile.getInputStream(ze);
          System.out.println("InputStream available size is " + iss.available());        
          /*sFileInZip = ze.getName();
          for (int vf = 0;vf<vFileName.size();vf++){
           String sFileName = vFileName.elementAt(vf).toString();
                                           
          if (sFileInZip.indexOf("outbound/processed")!=-1)
          sZipDirection = "outbound";
           if (sFileInZip.indexOf("inbound/processed")!=-1)
                      sZipDirection = "inbound";
                             
                             
                                                 
           if(sFileInZip.endsWith(sFileName) && (sFileInZip.indexOf("processed")!=-1)){
          System.out.println("sFileInZip : 1 " + sFileInZip );
          System.out.println("sFileInZip Size : " + sFileInZip.length() );
                     iss = myzipfile.getInputStream(ze);// here is the Zip exception <<<<<----------------------------------------------
         
                     BufferedReader brZipFile = new BufferedReader(new InputStreamReader(iss));
         //  updateLineCount(brZipFile,sProfile,sZipDirection);
          brZipFile.close();
          brZipFile = null;
           iss.close();
           iss = null;
            ze = null;
                             
            } // if
        } // for */
          iss.close();
          iss = null;
           ze = null;
         
          sFileInZip = null;            
          } // for
                       
                    myzipfile.close();
                    myzipfile = null;
                   String sFileName = null;            
               
           } catch(OutOfMemoryError ex) { System.out.println(" Memory Exception........ " + ex.toString());
        } // try/catch
         catch(IOException e) {System.out.println( " unzipAndGetEntries.....");
          e.printStackTrace(); } // try/catch
      }
}
0
 
CEHJCommented:

>>iss = myzipfile.getInputStream(ze);// here is the Zip exception <<<<<-

You've actually already made that assignment earlier:

>>
for(Enumeration e = myzipfile.entries(); e.hasMoreElements();)
      {
         ze = (ZipEntry)e.nextElement();
         iss =  myzipfile.getInputStream(ze);
>>
0
 
kiranhkCommented:
CEHJ,
that is my code which i posted after modifying. I have commented the earlier code which g46905 had posted as throwing exception.

Check out my comment
0
 
CEHJCommented:
Oh OK. I'll wait to see what g46905 says
0
 
g46905Author Commented:
kiranhk,
            Could you please explain me why you have commented the code below.Thanks

 /*sFileInZip = ze.getName();
          for (int vf = 0;vf<vFileName.size();vf++){
           String sFileName = vFileName.elementAt(vf).toString();
                                           
          if (sFileInZip.indexOf("outbound/processed")!=-1)
          sZipDirection = "outbound";
           if (sFileInZip.indexOf("inbound/processed")!=-1)
                      sZipDirection = "inbound";
                             
                             
                                                 
           if(sFileInZip.endsWith(sFileName) && (sFileInZip.indexOf("processed")!=-1)){
          System.out.println("sFileInZip : 1 " + sFileInZip );
          System.out.println("sFileInZip Size : " + sFileInZip.length() );
                     iss = myzipfile.getInputStream(ze);// here is the Zip exception <<<<<----------------------------------------------
         
                     BufferedReader brZipFile = new BufferedReader(new InputStreamReader(iss));
         //  updateLineCount(brZipFile,sProfile,sZipDirection);
          brZipFile.close();
          brZipFile = null;
           iss.close();
           iss = null;
            ze = null;
                             
            } // if
        } // for */
0
 
g46905Author Commented:
Could some one please help me with this issue where I am getting

java.lang.NullPointerException
     at java.util.zip.ZipFile.getInputStream(ZipFile.java:191)
     at LineCount.unzipAndGetEntries(LineCount.java:642)
     at LineCount.processFromAchivesFolder(LineCount.java:575)
     at LineCount.main(LineCount.java(Compiled Code))
0
 
CEHJCommented:
Is the exception occurring where you marked it in your code - i.e. is that line 191?
0
 
g46905Author Commented:
Yes sir. It happens when it tries to open a particular .zip folder. It is able to open the .zip file aand the access the files withing it.Could you please tell me if I am missing something?

Thanks a lot for your interest.
0
 
CEHJCommented:
>>It happens when it tries to open a particular .zip folder.

That folder is not being held open by anything else at that time is it?
0
 
g46905Author Commented:
No CEHJ.Could you please suggest me if I missed somthing?

Thanks,
0
 
g46905Author Commented:
Here is the method where the unzipAndGetEntries(String zipfilename,String sProfile, Date fromDD, Date toDate) is being called.

public static void processFromAchivesFolder(Vector proList, Date fromD, Date toDate)
        {
                                     
              try
              {
                    String proName = "";
                    File in_dir = new File(archive_path);
                    File filelist[] = in_dir.listFiles();
                    for(int x = 0; x < filelist.length; x++)
                    {
                          for(int y = 0; y < proList.size(); y++)
                          {
                                  String sWantedProfile = ((ProfileObject)proList.elementAt(y)).getName();
                                proName = ((ProfileObject)proList.elementAt(y)).getName().replace(' ', '_');
                                proName = proName.replace('-', '_');
                                proName = proName.replace('(', '_');
                                proName = proName.replace(')', '_');
                                
                                System.out.println("proName : " + proName);
                                System.out.println("filelist[x].getName() " + filelist[x].getName());
                                
                                if(filelist[x].getName().startsWith(proName))
                                      unzipAndGetEntries(archive_path + "/" + filelist[x].getName(),sWantedProfile,fromD, toDate);
                          }

                    }

                    proName = null;
                  
              }
              catch(Exception e) {
                    System.out.println(" processFromAchivesFolder..........");
                    e.printStackTrace(); } // end try/catch
} // end processFromAchivesFolder
0
 
g46905Author Commented:
Could you please tell me what exactly you meant by saying " Check the integrity by other means?.Please explain.

Thanks,
0
 
CEHJCommented:
Open with Winzip, Jar etc?
0
 
g46905Author Commented:
I have done all possible testing.I personally feel that I am doing something incorrect in my logic and is going out of boundry.Do we have any method where I can post the .zip file that I am trying to access so that you will be able to test it in your environment? .Please suggest me if there is an alternative.Thanks a lot

0
 
CEHJCommented:
I could take a look if you post it somewhere public (webspace etc)
0
 
g46905Author Commented:
I will see if I can provide a link to the webspace and let you know.Thanks for your help!

Thanks again
0
 
g46905Author Commented:
CEHJ,
        I was able to rectify the problem as I found that it was not exiting out of the loop correctly.Thanks for you time and help.
0
 
g46905Author Commented:
how do I split the points between you and me. Please suggest
0
 
CEHJCommented:
>>how do I split the points between you and me

I don't think you can. I think i might be able to lower them though. I'll see if i can get a mod over. Do nothing in the meantime ...
0
 
g46905Author Commented:
Sure.

Thanks
0
 
g46905Author Commented:
I feel that kiranhk  also deserves points for the time he invested so I have increased the Point Value.
0
 
CEHJCommented:
8-)
0
 
kiranhkCommented:
thanks  :)
0
All Courses

From novice to tech pro — start learning today.