File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes initalizing arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "initalizing arrays" Watch "initalizing arrays" New topic
Author

initalizing arrays

kevin schmidt
Ranch Hand

Joined: Aug 24, 2001
Posts: 85
Hi,
I'm declaring a "String[] keywords;" at the start of my program. Now I don't actually place anything into the array until I'm inside a couple for loops. When I compile, I get this error, "Variable keywords may not have been initialized." What do I do to fix this error. TIA.
kevin
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
The easiest is to just initialize the keywords variable to some temporary value, and then change it in your for loops.


"JavaRanch, where the deer and the Certified play" - David O'Meara
kevin schmidt
Ranch Hand

Joined: Aug 24, 2001
Posts: 85
I understand, but not. how do I initialize a whole array of strings?
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
You can do this at the beginning
String[] keywords = null
then in your loop/method
keywords = new String[whatever]

Bosun

Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Ian Darwin
author
Ranch Hand

Joined: Aug 03, 2001
Posts: 64
Originally posted by kevin schmidt:
I'm declaring a "String[] keywords;" at the start of my program. Now I don't actually place anything into the array until I'm inside a couple for loops. When I compile, I get this error, "Variable keywords may not have been initialized."

Cindy answered the "how to fix" but a bit more background may be helpful. The compiler is your friend when it gives you that message; the error message quoted above arises because there is at least one execution path (taking into account the flow of control during exception handling) that could possibly result in your trying to use the reference variable name "keywords" without having given it a value. Java is successfully preventing you from serious runtime errors. So you have to be a bit more careful when you take over responsibility and initialize the variables (BTW, I have looked into several complaints of people saying "the compiler is wrong" about these, but in every case, the compiler was correct).

As has been noted, the simplest way, if you must declare the variable in one place and assign to it in another, is to initialize it to "null" (the same rules apply to primitive types; initialize them to zero if numeric or false if boolean).

One thing further: variables declared as fields in a class are automatically assigned to the three values I listed, but this does not mean you should move local variables out to fields as one person once tried to convince me Just initialize the local variables and the compiler will be happy.
------------------
Ian Darwin,
Author of Java Cookbook: Solutions and Examples for Java Developers


Ian Darwin
Many questions are answered in my Java Cookbook, 2nd Edition
kevin schmidt
Ranch Hand

Joined: Aug 24, 2001
Posts: 85
I've tried the String[] keywords = null and then keywords = new String[list.length], where list.length is the number of files in the directory. Now I am getting an error that says :java.lang.ArrayIndexOutOfBoundsException = 4, which is the number of files in the directory. What would cause this exception?
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
If you have 4 items in an array, the max index is 3. 0, 1, 2 and 3

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: initalizing arrays
 
Similar Threads
Keywords & Reserved words
Do const & goto keyword?
SQL Server limiting result set
removing a sequence from ANY string
Java Class Keywords