Question

this is my first time posting on stackoverflow. I have a question about a series of errors that I have been encountering when I try to read in data from a generic text file in Netbeans IDE 7.4. I am using a 2009 iMac with Mac OS X Mavericks.

import java.util.Scanner;
import java.io.File;
public class two {
    public static void main(String[] args) throws Exception
    {

        System.out.println("Directory: "+System.getProperty("user.dir"));
        File f = new File("newfile.dat");
        Scanner s = new Scanner(f);

    }

}

And this code will return this set of errors:

Directory: /Users/omavine/Desktop/aPlusComputerScience
Exception in thread "main" java.io.FileNotFoundException: newfile.dat (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:143)
    at java.util.Scanner.<init>(Scanner.java:656)
    at apluscomputerscience.two.main(two.java:22)
    at apluscomputerscience.APlusComputerScience.main(APlusComputerScience.java:21)

Java Result: 1

This would indicate to me that the file simply was not being searched for in the correct path, however, when I compare the filename with the absolute path:

        System.out.println("Directory: "+System.getProperty("user.dir"));
        File f = new File("newfile.dat");
        System.out.println("Path: "+f.getAbsolutePath());

Then the output is as follows:

Directory: /Users/omavine/Desktop/aPlusComputerScience
Path: /Users/omavine/Desktop/aPlusComputerScience/newfile.dat

Which would indicate (to me) that the file is being searched for in the correct place whether I look for it with the explicit pathname or not.

However, when I try to construct the Scanner, (even with the absolute pathname):

Directory: /Users/omavine/Desktop/aPlusComputerScience
Path: /Users/maven/Desktop/aPlusComputerScience/newfile.dat
Exception in thread "main" java.io.FileNotFoundException: /Users/omavine/Desktop/aPlusComputerScience/newfile.dat (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:143)
    at java.util.Scanner.<init>(Scanner.java:656)
    at apluscomputerscience.two.main(two.java:23)
    at apluscomputerscience.APlusComputerScience.main(APlusComputerScience.java:21)
Java Result: 1

Still the same error. Interestingly enough, attempting to construct the scanner by using:

File f = new File("newfile.dat");
Scanner s = new Scanner(f.getClass().getResourceAsStream("newfile.dat"));

Returns:

Directory: /Users/omavine/Desktop/aPlusComputerScience
Path: /Users/omavine/Desktop/aPlusComputerScience/newfile.dat
Exception in thread "main" java.lang.NullPointerException
    at java.io.Reader.<init>(Reader.java:78)
    at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
    at java.util.Scanner.<init>(Scanner.java:608)
    at apluscomputerscience.two.main(two.java:23)
    at apluscomputerscience.APlusComputerScience.main(APlusComputerScience.java:21)
Java Result: 1

(Reading in the file with the getClass().getResourceAsStream() seems to return a null pointer exception, for whatever reason.)

I'd like to be able to read files on my home computer. This type of scenario has never happened to me at other computers with JCreator IDE. Can anyone make heads or tails of this dilemma?

Was it helpful?

Solution

the output clearly indicates you are looking at different directories.

Directory: /Users/omavine/Desktop/aPlusComputerScience
Path: /Users/maven/Desktop/aPlusComputerScience/newfile.dat

the working directory is in omavine's desktop. your file is in maven's desktop. Instead of giving the relative path, try giving the actual path of the file.

File f = new File("/Users/maven/Desktop/aPlusComputerScience/newfile.dat");

this ought to work.

EDIT: about the NullPointerException for getResourceAsStream

the java API doc statest that

Returns: An input stream for reading the resource, or null if the resource could not be found

and the Scanner throws the NullPointerException.

refer javadocs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top