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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:58:58+00:00 2026-06-17T02:58:58+00:00

I am running java code using Netbeans IDE in Ubuntu 12.04. I am trying

  • 0

I am running java code using Netbeans IDE in Ubuntu 12.04.

I am trying to create a file object with a pathname to a file directory and using the method listFiles() to return an array of pathnames under this file directory.

The code:

File allFile=new File("~/Desktop/matlab/CAT_00");
File[] fileList = allFile.listFiles();

However when i ran the the code, i get an exception declaring

Exception in thread "main" java.lang.NullPointerException
    at CatTest.main(CatTest.java:29)
Java Result: 1

So it appears that either my allFile object contains null object? I am not really sure myself. I tried checking the directory path that i wanted which was “~/Desktop/matlab/CAT_00” and it was correct. I tried using the debugging mode to check these 2 lines of code and found that listFiles() returned this exception error. However i do not understand why there should be an null exception error since there was files and directories under the CAT_00 main directory i created the File object with in the 1st place and hence should have returned an array of pathnames for these files and directories instead.

Help is much appreciated!!

  • 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-17T02:58:59+00:00Added an answer on June 17, 2026 at 2:58 am

    The reason why you’re getting a NullPointerException is because when you run listFiles() the path provided is does not actually exist.

    In Java you can not reference a file directly from ~/ because Java looks at file paths as if they’re URLs relative to where they are run. In your case since you are running inside Eclipse, it’s relative to your Eclipse workspace. Instead what you should use is

    String homeDir = System.getProperty("user.home");
    File allFile = new File(homeDir + "/Desktop/matlab/CAT_00");
    File[] fileList = allFile.listFiles()
    

    What System.getProperty("user.home") will do is get the user’s absolute home directory path, no matter the OS they’re running.

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

Sidebar

Related Questions

I'm using NetBeans IDE to code my Java program. A few days ago, I
I am running a shell command from Java code using ProcessBuilder.start() I need a
I am running ffmpeg.exe through a Java code to encode a video file. How
I have a created text file in unix enviroment using java code. For writing
I am trying to evoke FFMPEG directly in my Java code (running under the
In android app development wanted to create a EditText from java code without using
I have the following Java code running in a media module: File file =
I am trying to connect to HP Operations Manager Database using Java code in
I am getting some problem while executing binary executable file using Java code. My
I've been running different individual Java .java files in the Netbeans IDE by right-clicking

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.