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?

Edited 5 Years Ago by stephy1: n/a

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

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.

Edited 5 Years Ago by masijade: n/a

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

Edited 5 Years Ago by stephy1: n/a

"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

Edited 5 Years Ago by hanvyj: n/a

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".

Edited 5 Years Ago by masijade: n/a

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.

Edited 5 Years Ago by stephy1: n/a

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.

Edited 5 Years Ago by masijade: n/a

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.

This question has already been answered. Start a new discussion instead.