I got Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at this line.

File f1= new File(args[0]);

I guess i have to give a command line argument. How to give a file as command line argument. Or is this someother one?

Recommended Answers

All 14 Replies

Just type it like this in the console.
java progname filename

I am working in Netbeans IDE. I cannot type in output window.

Oh.. I'm Sorry !! I haven't worked on one !

I guess i have to give a command line argument.

As already siad here.

I am working in Netbeans IDE. I cannot type in output window.

Look at the project properties. There is a way to either "hard code" arguments or to get it to pop up a dialog asking for them.

Look at the project properties. There is a way to either "hard code" arguments or to get it to pop up a dialog asking for them.

Ya. By using properties dialog box only. But when i execute the run() method it shows FileNotFoundException at line:

FileReader rd = new FileReader(args[0]);

Type the full path to the file (use forward slants "/" not backslants "\", and yes, even on windows).

That means the file either does not exist under the path given, or it cannot be accessed (user permissions).

Type the full path to the file (use forward slants "/" not backslants "\", and yes, even on windows).
That means the file either does not exist under the path given, or it cannot be accessed (user permissions).

I have given the full path . But still it shows error.

Exception in thread "main" java.io.FileNotFoundException: gridp.ip.txt (The system cannot find the file specified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:106)
        at java.io.FileReader.<init>(FileReader.java:55)
        at gridp.Sudoku.main at line:   FileReader rd = new FileReader(args[0]);

Java Result: 1

"The system cannot find the file specified" - this means the system cannot fint the file specified

Place this line to help you spot problems:

//this checks the user has given you a filename
if (args.length > 0)
{
   filename = args[0];
   System.out.println("Filename: " + filename):

   //you should surround this with a try-catch statment to catch when the user
   //inputs an error  or file that doesnt exist
   FileReader rd = new FileReader(filename);
}
else
{
   System.out.println("You have not entered a filename!"):
}

the print line will help you see any erros in your filename - paste it into my-computer and see whether it exists etc

Exception in thread "main" java.io.FileNotFoundException: gridp.ip.txt (The system cannot find the file specified)

"gridp.ip.txt" is not the full path. The full path would be "C:/whatever/directory/the/file/is/in/gridp.ip.txt".

Actually in the netbeans inside the project folder inside the source package i created one package gridp. In this package only i am having main method containing class and that ip.txt. so that only i gave in the properties window gridp.ip.txt in the argument field. I tried projectfolder.package.filename. For this too i got same error.

Because the package path has nothing to do with file location. If you want to open a file based on package path use getResource to get a URL and the new File with the toURI method of that URL. (see the API docs for those). If you are only going to be readaing, then use getResourceAsStream to get an InputStream directly. If you plan on writing to it you don't want to use that and you won't be able to jar this file into the app (and if you do plan on jarring the file into the app you have to use getReourceAsStream as it will no longer be a File, but rather an entry inside of a jarfile).

Edit: I did explicitly state full path and I also gave an explicit example.

Because the package path has nothing to do with file location. If you want to open a file based on package path use getResource to get a URL and the new File with the toURI method of that URL. (see the API docs for those). If you are only going to be readaing, then use getResourceAsStream to get an InputStream directly. If you plan on writing to it you don't want to use that and you won't be able to jar this file into the app (and if you do plan on jarring the file into the app you have to use getReourceAsStream as it will no longer be a File, but rather an entry inside of a jarfile).

Edit: I did explicitly state full path and I also gave an explicit example.

I posted it in hastyness. I start from directory(drive partition).It worked. Now all errors get cleared. And i got the output. And my doubt is if code expects a command line argument it will shows any exception ?

That's what the ArrayIndexOutOfBounds was. If you want your program to through a "special" exception due to the lack of command line arguments than add an if statement checking the length of the args array and throw one if it is not at least as long as you expect.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.