Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9120159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:33:56+00:00 2026-06-17T05:33:56+00:00

Today I wrote a small Java program to find files on my harddisk, partly

  • 0

Today I wrote a small Java program to find files on my harddisk, partly because Windows lacks a proper one and partly because it was for fun. It simply iterates over all childs of a directory and recursively through all their children if they are directories. But then I had a NullPointerException. After some System.out.println()'s and a try-catch blockI found out this happened on some directories like D:\System Volume Information, C:\Users\Public\Documents\My Videos and C:\ProgramData\Templates. Windows itself says, when input in the Explorer address bar “Access denied”. When exploring, they seem to be locked. The source from java.io.File tells me:

public File[] listFiles() {
    String[] ss = list();
    if (ss == null) return null; //This line seems to cause the NPE
    int n = ss.length;
    File[] fs = new File[n];
    for (int i = 0; i < n; i++) {
        fs[i] = new File(ss[i], this);
    }
    return fs;
}

My question is, why is there no proper say, IOException, SecurityException, or even an IDontCareWhatKindOfExceptionButPleaseNoNullPointerExceptionException?

I know I can just try-catch for NPEs, or check for null, but I’m really curious of why there’s no proper Exception thrown.

Since I dislike try/catch and checking for null because of the aesthetic value of code, I made some nasty, nasty reflection code (with lots of hypocritical try/catch blocks) which emulates some methods from java.io.File, since they’re private, and some packages (like java.io.FileSystem) are not visible. And because I do not care about some Windows files, I simply ignore them using this code, giving me an empty array:

public static File[] listFiles(File mThis) {
    String[] ss = list(mThis);
    if (ss == null) {
        //System.out.println("ss is null");
        return new File[] {};
    }
    int n = ss.length;
    File[] fs = new File[n];
    for (int i = 0; i < n; i++) {
        fs[i] = getNewFile(ss[i], mThis);
    }
    return fs;
}

EDIT: to avoid misinterpretations: I do use try/catch, I merely don’t like them aesthetically.

EDIT2: I’d rather use a GUI than the command prompt. And I very much dislike the Windows Search GUI. It’s almost painful to use.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:33:58+00:00Added an answer on June 17, 2026 at 5:33 am

    Short answer:

    • install JDK 7;
    • use the new Files API.

    You will never come back to File after that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today I wrote a small predicate to find matching symbols in a container. But
I wrote this small program today and I was blown away by the results.
Today I was wondering about c++ destructors so I wrote a small test program.
I wrote my first program in Haskell today. It compiles and runs successfully .
I wrote a CredentialProvider that allows to log in to Windows. But today I
I just wrote an hello world in windows 8 app and today I read
Earlier today I wrote a small test app for iteratees that composed an iteratee
I wrote a text mode batch C++ program that since today is giving me
I'm using Dr. Memory for memory debugging of a C program I wrote today.
Hey there. Today I wrote a small benchmark script to compare performance of copying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.