Solved

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

Posted on 2004-10-27
1,241 Views
Last Modified: 2009-12-16
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
0
Question by:g46905
    26 Comments
     
    LVL 8

    Expert Comment

    by:kiranhk
    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
     

    Author Comment

    by:g46905
    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
     
    LVL 8

    Assisted Solution

    by:kiranhk
    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
     
    LVL 86

    Expert Comment

    by:CEHJ

    >>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
     
    LVL 8

    Expert Comment

    by:kiranhk
    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
     
    LVL 86

    Expert Comment

    by:CEHJ
    Oh OK. I'll wait to see what g46905 says
    0
     

    Author Comment

    by:g46905
    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
     

    Author Comment

    by:g46905
    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
     
    LVL 86

    Expert Comment

    by:CEHJ
    Is the exception occurring where you marked it in your code - i.e. is that line 191?
    0
     

    Author Comment

    by:g46905
    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
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>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
     

    Author Comment

    by:g46905
    No CEHJ.Could you please suggest me if I missed somthing?

    Thanks,
    0
     

    Author Comment

    by:g46905
    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
     
    LVL 86

    Accepted Solution

    by:
    Check the integrity of the zip file by other means first
    0
     

    Author Comment

    by:g46905
    Could you please tell me what exactly you meant by saying " Check the integrity by other means?.Please explain.

    Thanks,
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    Open with Winzip, Jar etc?
    0
     

    Author Comment

    by:g46905
    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
     
    LVL 86

    Expert Comment

    by:CEHJ
    I could take a look if you post it somewhere public (webspace etc)
    0
     

    Author Comment

    by:g46905
    I will see if I can provide a link to the webspace and let you know.Thanks for your help!

    Thanks again
    0
     

    Author Comment

    by:g46905
    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
     

    Author Comment

    by:g46905
    how do I split the points between you and me. Please suggest
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>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
     

    Author Comment

    by:g46905
    Sure.

    Thanks
    0
     

    Author Comment

    by:g46905
    I feel that kiranhk  also deserves points for the time he invested so I have increased the Point Value.
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    8-)
    0
     
    LVL 8

    Expert Comment

    by:kiranhk
    thanks  :)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Anonabox PRO Tor & VPN Router

    PRO is the most advanced way to fortify your privacy and online anonymity by layering the Tor network with VPN services. Use both together or separately, and without needing to download software onto your devices.

    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    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…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    884 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now