Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why do I get an initialization error here?

 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello;

I have declared an inner class as following:



When I compile the class I get the following error:



But when I declare selectedFileNames as just a String (not String[] and no [counter]), it works fine.

What am I doing wrong here.

Cheers.

[ July 28, 2008: Message edited by: Maduranga Liyanage ]
[ July 28, 2008: Message edited by: Maduranga Liyanage ]
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must initialize the array String[] selectedFileNames before using it.Something like this:

String[] selectedFileNames = new String[selectedFiles.length];

but you must know a priori the size
[ July 28, 2008: Message edited by: Manuel Leiria ]
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrr...
Thanks a lot mate.
I guess I have to use an ArrayList... I cannot know the length in advance.

Thanks again.
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maduranga Liyanage:
Arrr...
Thanks a lot mate.
I guess I have to use an ArrayList... I cannot know the length in advance.

Thanks again.


From your code I see that selectedFileNames array will have the same length as the selectedFiles array.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic