Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

how to stop "array may not be initialized" warning

Posted on 2003-12-01
7
369 Views
Last Modified: 2010-03-31

my class has an array of ints named "calls" and an int "calls_size" to keep track of it's size. I know this screams for a Vector but I think it is a pain in the butt using a vector for ints, too much screwing around with Object nonsense

anyway, this function should return an array of ints matching "calls" but with "calls_size" at the begining, so if calls==10,11,12 then results=3,10,11,12

it has been awhile since I have used java, especially with arrays, so I'm not sure if my whole open ended array idea will work, but at any rate, line 06 gives me a compilation error:  "variable results might not have been initialized".
I am quite aware of this fact and I don't care if it is initialized or not since I am manually tracking the size. This error stops compilation, however. Is there any way to demote the error to simply a warning? Any suggestions regarding my scheme would also be welcomed.

01 public int[] getCalls()
02 {
03    try
04    {
05        int[] results;
06        results[0] = calls_size;
07        for(int i = 1; i <= calls_size; i++)
08        {
09            results[i] = calls[i];
10        }
11        return results;
12   }
   ...snip ...

Thanks,
Ken Goudsward
0
Comment
Question by:kengoudsward
  • 5
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9855888
int[] results = new int[calls_size+1];
0
 
LVL 92

Expert Comment

by:objects
ID: 9855903
You have to declare the size of the array.
0
 
LVL 92

Expert Comment

by:objects
ID: 9856089
You can actually get the size of an array using:

myarray.length
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 92

Accepted Solution

by:
objects earned 150 total points
ID: 9856100
To have a dynamic int array you could create a wrapper class around Vector, something like:

public class IntArray
{
   private Vector v = new Vector();

   public void add(int i)
   {
      v.add(new Integer(i));
   }

   public int get(int index)
   {
      Integer i = (Integer) v.get(index);
      return i.intValue();
   }

   ...
}
0
 

Assisted Solution

by:stevebeech
stevebeech earned 50 total points
ID: 9856937
Ken,

Because the initialisation is inside the try{} block then the possibility occurs that it might not get initialised - not likely as its the first command in the block, and you're returning the result from within the block. But, what if you were returning from outside? Then you'd have a problem - the try block could fail and results would never have been initialised.
The simple solution (as Objects says) is simply to initialise the array to some value:

int[] results = new int[calls_size+1];

Steve

0
 

Author Comment

by:kengoudsward
ID: 9862255
Thanks guys,
can't test it today, but...
I like that wrapper class solution!
I will also try putting the array declaration before the 'try' block
0
 
LVL 92

Expert Comment

by:objects
ID: 9862318
> I will also try putting the array declaration before the 'try' block

That won't help. Even if you get rid of the compiler error you'll get a NPE at runtime.

http://www.objects.com.au
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question