Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Strange for loop behavior Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strange for loop behavior" Watch "Strange for loop behavior" New topic
Author

Strange for loop behavior

Marcus Laubli
Ranch Hand

Joined: Dec 24, 2004
Posts: 116
I've been playing with this recursive for loop on and off over the last few days.

It seems like it makes it through an entire hard drive, listing each file and directory, then stumbles two feet (no, three inches) before the finish line and crashes with a java.lang.NullPointerException.

I can't see what I'm doing wrong. Could it be that hidden files cannot be accessed without using some kind of special flag or something?

Help!

Marcus



Marcus L´┐Żubli, SCJP 1.4, CLP 5.0, SCWCD 1.4 (preparing)
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Can you please post the exact stack trace given with the NullPointerException message? This will give us the line number where the error occurs. Also, if you can indicate which line of code the line number referrs to, that would be helpful. I'm not about to count lines manually, however I'm more than willing to help if you'll provide this information.

Keep Coding!

Layne


Java API Documentation
The Java Tutorial
Marcus Laubli
Ranch Hand

Joined: Dec 24, 2004
Posts: 116
Layne,

Thanks for trying. I should have been more specific.

The line that the stacktrace is pointing at is the following:


for (int i=0; i<children.length; i++) {

It's inside the visitAllDirsAndFiles method.

The strange thing is that the code makes it all of the way through the hard drive, then bombs in the rood directory, after the last entry.

Here's the stack trace...

Exception in thread "main" java.lang.NullPointerException
at BackupClient.DSIDirectory.visitAllDirsAndFiles(DSIDirectory.java:42)
at BackupClient.DSIDirectory.visitAllDirsAndFiles(DSIDirectory.java:52)
at BackupClient.DSIDirectory.<init>(DSIDirectory.java:71)
at BackupClient.BackupClient.main(BackupClient.java:14)
Java Result: 1
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
The only thing in that line of code that can cause a NullPointerException is "children.length". This means that children is null, so file.list() is returning null for some reason. So my next question is what does the variable file refer to just before the NPE is thrown?

On a side note, I tried running this program myself and didn't have any problems. I had to change the values of cDir and dDir because I don't have a Windows system with Java available. Perhaps this makes a difference for some reason.

Anyway, take a look at the value of file to start tracking down the problem. I'll be more than happy to keep helping, at least until I have to leave for class in half an hour or so. After that it may be a while before I reply again.

Layne
Marcus Laubli
Ranch Hand

Joined: Dec 24, 2004
Posts: 116
Layne,

When I comment out the following line:



the program runs. umm. It obviously only processes the root directory, however, it doesn't fail.

Any Idea why? Do you think we may have a problem passing children[i]; the last time? It's about all I can think of!

Marcus
[ January 13, 2005: Message edited by: Marcus Laubli ]
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

I just ran this test and it also errored. I commented out the printlns for each file and then changed the code as follows:


And I got the following results:


So I dropped down to DOS and paid it a visit:

So it seems that file.list() will return null if you do not have access to the file.

The JavaDoc reads:

An array of strings naming the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.


So, it looks like an I/O error is occurring, but instead of throwing an exception, the method swallows the exception and returns null.
[ January 14, 2005: Message edited by: Joel McNary ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
With Joel's comments, it looks like you need to check whether children is null before you start the for loop. You will also need to decide what to do when children *is* null.

-Layne
Marcus Laubli
Ranch Hand

Joined: Dec 24, 2004
Posts: 116
Joel, Layne,

Thanks for taking this monster apart. It seems that the issue just raises more question than answers!

Here are a couple:

1) Is this behavior an OS (win2k) thing, or will it happen in other situations too?

What I'm trying to do is iterate to each file of the hard drive, read it, compress it, serialize the compressed file, then send it to another box for archival purposes.

With this goal in mind, what should I do to avoid strange side effects? I need an exact duplicate of the drive on the compressed side.

Help!

P.S. Is this better to be discussed in the I/O forum?

[ January 25, 2005: Message edited by: Marcus Laubli ]
[ January 25, 2005: Message edited by: Marcus Laubli ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Marcus Laubli:
Joel, Lane,

Thanks for taking this monster apart. It seems that the issue just raises more question than answers!

Here are a couple:

1) Is this behavior an OS (win2k) thing, or will it happen in other situations too?

What I'm trying to do is iterate to each file of the hard drive, read it, compress it, serialize the compressed file, then send it to another box for archival purposes.

With this goal in mind, what should I do to avoid strange side effects? I need an exact duplicate of the drive on the compressed side.

Help!

P.S. Is this better to be discussed in the I/O forum?

[ January 25, 2005: Message edited by: Marcus Laubli ]


First of all, my name is Layne.

Second of all, yes, this behavior is caused by the underlying operating system. The OS is responsible for interaction with the file I/O system on the harddrive, so it will signal Java when there is an error. This in turn causes your list of file names to contain a null reference. One situation where this might occur is if the OS decides you don't have permission to access a given file, as shown in Joel's example above. This is very common in Unix as it enforces a very strict permission system. It is becoming common in recent versions of Windows, although on Windows 9x or earlier systems, I don't think there was any kind of file permissions structure. I'm sure there are other situations where an I/O error will arise and cause this same behavior, but this is probably the most common.

So my suggestion is to check for null entries as you iterate through the list of files. This will make your code fairly robust since there isn't much you can do about a file I/O error.

HTH

Layne
Marcus Laubli
Ranch Hand

Joined: Dec 24, 2004
Posts: 116
Layne,

I changed the typo of your name. Sorry, I myself am a stickler on Marcus, not Marc or Mark. I changed it !

I guess I'll just have to log this type of actions so that we know which files may and which may not be copiedd to the other server.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strange for loop behavior