Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Null pointer exceptions

Reply
Thread Tools

Null pointer exceptions

 
 
Alan
Guest
Posts: n/a
 
      12-25-2007
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 = 4
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[i]);
contents.addAll(listAllFiles(new File(dir,
children[i]), 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;
}
}






 
Reply With Quote
 
 
 
 
d0ngd0ng
Guest
Posts: n/a
 
      12-25-2007
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 <(E-Mail Removed)> 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 = 4
> 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[i]);
> * * * * * * *contents.addAll(listAllFiles(new File(dir,
> * * * * * * * * * * * * * * *children[i]), 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;
> * *}
>
> }


 
Reply With Quote
 
 
 
 
Kira Yamato
Guest
Posts: n/a
 
      12-25-2007
On 2007-12-25 11:30:21 -0500, Alan <(E-Mail Removed)> said:

> I keep getting NullPointerExceptions in the code below.


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

> [...]


--

-kira

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      12-25-2007
Kira Yamato wrote:
> On 2007-12-25 11:30:21 -0500, Alan <(E-Mail Removed)> 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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      12-28-2007
On Tue, 25 Dec 2007 08:30:21 -0800 (PST), Alan
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>NullPointerExceptions

for generic help tracking these down see:

http://mindprod.com/jgloss/runerrorm...INTEREXCEPTION
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to pointer intialize to NULL but still point to NULL Christopher C++ 4 07-09-2011 12:35 AM
Null pointer (NULL array pointer is passed) aneuryzma C++ 3 06-16-2008 05:48 AM
beginner question about null pointer exceptions aa Java 4 07-24-2006 09:22 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
null pointer exceptions Tennessee James Leeuwenburg Python 1 07-18-2003 05:06 AM



Advertisments