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 6230023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:40:21+00:00 2026-05-24T09:40:21+00:00

import java.io.File; public class filesinDirectory { public static void main(String[] args) { File path

  • 0
import java.io.File;


public class filesinDirectory {

public static void main(String[] args) {
    File path = new File("/home/vito/Desktop/Dir1");
    File[] files = path.listFiles();
    int length = files.length; // line 9

    for (int i = 0; i != length; i++) {
        if (files[i].isFile()) 
        System.out.println( i + ": " + files[i].getName());
    }
}
}

With the above code, every time I try to run it I get a java.lang.NullPointerException error on line 9. However I am not sure what the source of this error is.

  • 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-05-24T09:40:22+00:00Added an answer on May 24, 2026 at 9:40 am

    Javadoc says:

    File.listFiles()
    
    Returns:
        An array of abstract pathnames denoting 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 either the directory does not exist or an I/O error occured.

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

Sidebar

Related Questions

This is my java file : import java.io.File; import java.lang.String; public class ListFiles {
import java.io.*; public class Auto { /** * @param args * @throws IOException */
If i have my class as import java.io.File; import java.io.FileOutputStream; //Extends Activity public class
package com.example.helloandroid; import java.io.File; import android.os.AsyncTask; import android.os.Environment; import android.widget.Toast; public class CheckTask extends
package jtextareatest; import java.io.FileInputStream; import java.io.IOException; import javax.swing.*; public class Jtextareatest { public static
Take the following generics example import java.util.List; import java.util.ArrayList; public class GenericsTest { private
I have this test app: import java.applet.*; import java.awt.*; import java.net.URL; public class Test
I've created this simple program in Eclipse: import java.util.*; import java.io.*; public class prob1
i have two programs one in directory /home/redhat/Documents/java1/j1 Demo1.java package j1; public class Demo1
import java.io.FileNotFoundException; import java.lang.SecurityException; import java.util.Formatter; import java.util.FormatterClosedException; import java.util.NoSuchElementException; import javax.swing.JOptionPane; public class

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.