Null pointer exceptions

Discussion in 'Java' started by Alan, Dec 25, 2007.

  1. Alan

    Alan Guest

    I keep getting NullPointerExceptions in the code below. I have
    also provided the execution output below. It seems when I add a check
    on an error (such as I did at Line 56), I just get the exception at a
    different spot.

    I am new to Java. I think this has something to do with
    conversions from an Array (of files from listfiles method of File
    class) to a List, but I cannot pinpoint the problem.

    Is there something obviously wrong?

    My intent was to create a general utility method that returns a
    list of files in the designated directory and its subdirectories.

    Thanks, Alan


    ----jGRASP exec: java jat.util.file.AllFiles



    .... Entering method listAllFiles ...
    dir = My Java Code
    java.lang.NullPointerException
    at jat.util.file.AllFiles.listAllFiles(AllFiles.java:56)
    at jat.util.file.AllFiles.listAllFiles(AllFiles.java:73)
    FilenameFilter selected . . .
    filelist contents:
    null

    Directory found
    Children: (size = 48)
    URLget.java


    .... Entering method listAllFiles ...
    dir = URLget.java
    FilenameFilter selected . . .
    at jat.util.file.AllFiles.main(AllFiles.java:22)
    java.lang.NullPointerException
    at jat.util.file.AllFiles.listAllFiles(AllFiles.java:73)
    at jat.util.file.AllFiles.main(AllFiles.java:22)

    ----jGRASP: operation complete.

    package jat.util.file;

    import java.io.*;
    import java.io.FilenameFilter;
    import java.util.*;

    public class AllFiles
    {
    /** The main method is used for testing other methods
    of the AllFiles class.
    */
    public static void main(String[] args)
    {
    File directory = new File(System.getProperty("user.dir"));
    if (args.length > 0)
    {
    File tempFile = new File(args[0]);
    if (tempFile.isDirectory()) { directory = tempFile; }
    }
    try
    {
    List<File> filelist = listAllFiles(directory, "java");
    for (Iterator it = filelist.iterator(); it.hasNext();)
    {
    File file = (File) it.next();
    System.out.println(file.getName());
    }
    }
    catch (Exception e)
    { }
    }

    /** The listAllFiles method provides a list of all files
    found in the user-specified directory and its
    subdirectories.

    @param dir the directory at which the file list
    should start
    @param extension a string indicating the file
    extension that should be used to
    filter the
    results
    @return File[] a list of File objects
    */
    public static List<File> listAllFiles(File dir, String extension)
    throws Exception
    {
    System.out.println("\n\n... Entering method listAllFiles ...");
    try
    {
    List<File> contents = null;
    List<File> filelist = null;
    System.out.println("dir = " + dir.getName());
    FilenameFilter select = new FileListFilter(dir.getName(),
    extension);
    System.out.println("FilenameFilter selected . . .");
    File[] filearray = dir.listFiles(select);
    if (filearray.length > 0)
    { filelist = Arrays.asList(filearray); }
    System.out.println("filelist contents:\n"
    + filelist + "\n");
    if (filelist != null)
    {
    System.out.println("filelist is NOT empty");
    contents = filelist;
    }

    if (dir.isDirectory())
    {
    System.out.println("Directory found");
    String[] children = dir.list();
    System.out.println("Children: (size = " + children.length
    + ")");
    for (int i = 0; i < children.length; i++)
    {
    System.out.println(children);
    contents.addAll(listAllFiles(new File(dir,
    children), extension));
    }
    }
    return contents;
    }
    catch ( SecurityException e )
    {
    e.printStackTrace();
    return null;
    }
    catch ( RuntimeException e )
    {
    e.printStackTrace();
    return null;
    }
    }
    }

    /** The FileListFilter class implements the
    java.io.FilenameFilter interface.
    */
    class FileListFilter implements FilenameFilter
    {
    private String name, extension;

    public FileListFilter(String name, String extension)
    {
    this.name = name;
    this.extension = extension;
    }

    /** The accept method determines whether or not a File
    meets the filename filter criteria.

    @param directory a <code>File</code> object that indicates
    the directory
    @param filename a String that contains the filename
    of the file
    @return boolean indicates whether or not the file
    meets the filtering criteria
    */
    public boolean accept(File directory, String filename)
    {
    boolean fileOK = true;
    if (name != null)
    { fileOK &= filename.startsWith(name); }

    if (extension != null)
    { fileOK &= filename.endsWith('.' + extension); }

    return fileOK;
    }
    }
     
    Alan, Dec 25, 2007
    #1
    1. Advertising

  2. Alan

    d0ngd0ng Guest

    public static List<File> listAllFiles(File dir, String extension)

    You have to check if the parameter dir is a directior with
    dir.isDirectory.

    Because the invoking of the method listFiles on a no directory File
    object will return null.


    On Dec 26, 12:30 am, Alan <> wrote:
    >    I keep getting NullPointerExceptions in the code below.  I have
    > also provided the execution output below.  It seems when I add a check
    > on an error (such as I did at Line 56), I just get the exception at a
    > different spot.
    >
    >       I am new to Java.  I think this has something to do with
    > conversions from an Array (of files from listfiles method of File
    > class) to a List, but I cannot pinpoint the problem.
    >
    >       Is there something obviously wrong?
    >
    >       My intent was to create a general utility method that returns a
    > list of files in the designated directory and its subdirectories.
    >
    >                                     Thanks, Alan
    >
    >  ----jGRASP exec: java jat.util.file.AllFiles
    >
    > ... Entering method listAllFiles ...
    > dir = My Java Code
    > java.lang.NullPointerException
    >         at jat.util.file.AllFiles.listAllFiles(AllFiles.java:56)
    >         at jat.util.file.AllFiles.listAllFiles(AllFiles.java:73)
    > FilenameFilter selected . . .
    > filelist contents:
    > null
    >
    > Directory found
    > Children: (size = 48)
    > URLget.java
    >
    > ... Entering method listAllFiles ...
    > dir = URLget.java
    > FilenameFilter selected . . .
    >         at jat.util.file.AllFiles.main(AllFiles.java:22)
    > java.lang.NullPointerException
    >         at jat.util.file.AllFiles.listAllFiles(AllFiles.java:73)
    >         at jat.util.file.AllFiles.main(AllFiles.java:22)
    >
    >  ----jGRASP: operation complete.
    >
    > package jat.util.file;
    >
    > import java.io.*;
    > import java.io.FilenameFilter;
    > import java.util.*;
    >
    > public class AllFiles
    > {
    > /**     The main method is used for testing other methods
    >         of the AllFiles class.
    > */
    >    public static void main(String[] args)
    >    {
    >       File directory = new File(System.getProperty("user.dir"));
    >       if (args.length > 0)
    >       {
    >          File tempFile = new File(args[0]);
    >          if (tempFile.isDirectory()) { directory = tempFile; }
    >       }
    >       try
    >          {
    >             List<File> filelist = listAllFiles(directory, "java");
    >             for (Iterator it = filelist.iterator(); it.hasNext();)
    >             {
    >         File file = (File) it.next();
    >         System.out.println(file.getName());
    >             }
    >          }
    >       catch (Exception  e)
    >       { }
    >
    > }
    >
    > /**     The listAllFiles method provides a list of all files
    >         found in the user-specified directory and its
    >         subdirectories.
    >
    >         @param  dir   the directory at which the file list
    >                        should start
    >         @param  extension       a string indicating the file
    >                                 extension that should be used to
    > filter the
    >                                 results
    >         @return File[]          a list of File objects
    > */
    > public static List<File> listAllFiles(File dir, String extension)
    > throws Exception
    > {
    >    System.out.println("\n\n... Entering method listAllFiles ...");
    >    try
    >    {
    >       List<File> contents = null;
    >       List<File> filelist = null;
    >       System.out.println("dir = " + dir.getName());
    >       FilenameFilter select = new FileListFilter(dir.getName(),
    >                                                         extension);
    >       System.out.println("FilenameFilter selected . . .");
    >       File[] filearray = dir.listFiles(select);
    >       if (filearray.length > 0)
    >        { filelist = Arrays.asList(filearray); }
    >       System.out.println("filelist contents:\n"
    >                                        + filelist + "\n");
    >       if (filelist != null)
    >       {
    >          System.out.println("filelist is NOT empty");
    >          contents = filelist;
    >       }
    >
    >       if (dir.isDirectory())
    >       {
    >         System.out.println("Directory found");
    >         String[] children = dir.list();
    >         System.out.println("Children: (size = "                                       + children.length
    > + ")");
    >         for (int i = 0; i < children.length; i++)
    >         {
    >              System.out.println(children);
    >              contents.addAll(listAllFiles(new File(dir,
    >                              children), extension));
    >         }
    >    }
    >    return contents;}
    >
    > catch ( SecurityException  e )
    > {
    >     e.printStackTrace();
    >     return null;}
    >
    > catch ( RuntimeException  e )
    > {
    >     e.printStackTrace();
    >     return null;
    >
    > }
    > }
    > }
    >
    > /**     The FileListFilter class implements the
    >                  java.io.FilenameFilter interface.
    > */
    > class FileListFilter implements FilenameFilter
    > {
    >      private String name, extension;
    >
    >      public FileListFilter(String name, String extension)
    >      {
    >         this.name = name;
    >         this.extension = extension;
    >      }
    >
    > /**   The accept method determines whether or not a File
    >        meets the filename filter criteria.
    >
    >       @param  directory  a <code>File</code> object that indicates
    >                                    the directory
    >       @param   filename   a String that contains the filename
    >                      of the file
    >       @return   boolean    indicates whether or not the file
    >                    meets the filtering criteria
    > */
    >    public boolean accept(File directory, String filename)
    >    {
    >       boolean fileOK = true;
    >       if (name != null)
    >       { fileOK &= filename.startsWith(name); }
    >
    >       if (extension != null)
    >       { fileOK &= filename.endsWith('.' + extension); }
    >
    >       return fileOK;
    >    }
    >
    > }
     
    d0ngd0ng, Dec 25, 2007
    #2
    1. Advertising

  3. Alan

    Kira Yamato Guest

    On 2007-12-25 11:30:21 -0500, Alan <> said:

    > I keep getting NullPointerExceptions in the code below.


    NullPointerException is certainly java's version of C's dangling
    pointer problem.

    > [...]


    --

    -kira
     
    Kira Yamato, Dec 25, 2007
    #3
  4. Alan

    Lew Guest

    Kira Yamato wrote:
    > On 2007-12-25 11:30:21 -0500, Alan <> said:
    >
    >> I keep getting NullPointerExceptions in the code below.

    >
    > NullPointerException is certainly java's version of C's dangling pointer
    > problem.


    Crude but effective analogy. Note that the scope, causes and impact of the
    two are not equivalent. Analysis and remediation also differ markedly for the
    two types of error.

    --
    Lew
     
    Lew, Dec 25, 2007
    #4
  5. Alan

    Roedy Green Guest

    Roedy Green, Dec 28, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    27,597
    Mike Schilling
    Mar 29, 2006
  2. Tennessee James Leeuwenburg

    null pointer exceptions

    Tennessee James Leeuwenburg, Jul 18, 2003, in forum: Python
    Replies:
    1
    Views:
    357
    Erik Max Francis
    Jul 18, 2003
  3. aa
    Replies:
    4
    Views:
    370
    Patricia Shanahan
    Jul 24, 2006
  4. aneuryzma
    Replies:
    3
    Views:
    786
    Jim Langston
    Jun 16, 2008
  5. Christopher
    Replies:
    4
    Views:
    469
    Ruben Safir
    Jul 9, 2011
Loading...

Share This Page