We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,708 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Array Out Of Bounds reading in file

So when I compile, everything will compile just fine.

When I go to run the program, I get this error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
	at ClientForNoDupCollection.main(ClientForNoDupCollection.java:20)

I go to look at line 20(actually 19-21)

public static void main(String args[]) {
		File file = new File(args[0]);
		Scanner cReader = null;

I'm attempting to be able to do command line execution of the files.

I'm not 100% sure where I went wrong there.

3
Contributors
4
Replies
8 Hours
Discussion Span
1 Year Ago
Last Updated
5
Views
TheComputerGuy
Light Poster
32 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

When you run, how did you run it? If you use IDE (not command line execution), you may not pass the argument in the command to the method. As a result, your args[] array is size 0.

// correct command line
java ClientForNoDupCollection myfile.ext
// if the myfile.ext exists, it is OK here
// if it doesn't exist, File class will throw NPE

// incorrect
java ClientForNoDupCollection
Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

So I tried what you referred to.

I get this error now.

java.lang.NullPointerException
	at ClientForNoDupCollection.main(ClientForNoDupCollection.java:41)

Looked at line 41.

while (cReader.hasNext())

Looks like it's referring back to the top...as being null.

TheComputerGuy
Light Poster
32 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Then you need to show me your whole class code then. I can't pinpoint how it happen by seeing only this much portion of the code. :(

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

So I tried what you referred to.

I get this error now.

java.lang.NullPointerException
	at ClientForNoDupCollection.main(ClientForNoDupCollection.java:41)

Looked at line 41.

while (cReader.hasNext())

Looks like it's referring back to the top...as being null.

Uhm ...

public static void main(String args[]) {
		File file = new File(args[0]);
		Scanner cReader = null;
masijade
Industrious Poster
Team Colleague
4,253 posts since Feb 2006
Reputation Points: 1,484
Solved Threads: 494
Skill Endorsements: 16

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0671 seconds using 2.68MB