1

I created class Supervisor which contains the constructor and a toString method. However when I try to print an Index of the array an error occur,"Variable svArray might not have been initialized. What can I do to solve this ?

Supervisor[] svArray;
if (mainChoice == 3){
        String ID, name, department, resarch;
        System.out.println("Enter How Many Supervisor you want to enter : ");
        svNumber = input.nextInt();
        svArray = new Supervisor[svNumber];
        for (int i = 0; i < svNumber; i++) {
            System.out.println("---Enter Supervisor " + (i + 1) + " Of " + svNumber + " ---");
            System.out.println("Enter staff ID : ");
            ID = input.next();
            System.out.println("Enter name : ");
            name = input.next();
            System.out.println("Enter department : ");
            department = input.next();
            svArray[i]=new Supervisor(ID, name, department);\
        }


    }
System.out.print(svArray[1].toString)
//Error, variable svArray might not have been initialized
1
  • 3
    You can't use a un-initialized variable in java. what about if mainChoice is not equal to 3. Commented Apr 14, 2014 at 16:06

5 Answers 5

3

The problem is that on this line:

System.out.print(svArray[1].toString)

svArray hasn't been initialized in case that mainChoice != 3, because you initialize the array inside of if statement which is not executed.

To overcome this problem you can initialize the array to null:

Supervisor[] svArray = null;

And then check for null:

if(svArray != null) System.out.print(svArray[1].toString());
Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't compile because arrays don't have a toString attribute (admittedly the error is present in the OP's code as well).
@JonK but you call toString on first element of said array which is Supervisor type
But you aren't calling toString(). That's my point. Method calls have brackets.
@JonK Oh I see, thanks! I didn't noticed that and just blindly copy&pasted.
2

Simply put you initialize the array inside the if statement but use it outside. So if the content of the if statement never runs (that is whenever mainChoice is not 3) the array is used at the end without being initialized.

Comments

1

If mainChoice is not equal to 3, svArray never gets initialized. You need to either place the print statement into the if block or unconditionally initialize svArray[1] to some value.

Comments

0

Local variables differ from member variables in that they don't get automatically initialised to a default value for you. Java doesn't allow you to use a variable that hasn't been initialised yet, and the JVM can't know if mainChoice is always going to be 3. Because of this, the compiler can't be certain that your array will have been initialised by the time the call to System.out.print(svArray[1].toString) is attempted - hence your error.

It's not clear why you're declaring svArray outside of your loop, so this code may not be entirely appropriate:

if (mainChoice == 3){
    String ID, name, department, resarch;
    System.out.println("Enter How Many Supervisor you want to enter : ");
    svNumber = input.nextInt();
    Supervisor[] svArray = new Supervisor[svNumber];
    for (int i = 0; i < svNumber; i++) {
        System.out.println("---Enter Supervisor " + (i + 1) + " Of " 
            + svNumber + " ---");
        System.out.println("Enter staff ID : ");
        ID = input.next();
        System.out.println("Enter name : ");
        name = input.next();
        System.out.println("Enter department : ");
        department = input.next();
        svArray[i]=new Supervisor(ID, name, department);\
    }
    System.out.print(svArray[1].toString())
}

There are three important changes to note here.

  • The declaration and initialisation of svArray now occur on the same line within your if statement.
  • The System.out.println call now also happens inside your if statement so that svArray is still in scope.
  • It now correctly invokes the toString() method, rather than attempting to access an attribute called toString (which doesn't exist).

Comments

0
 svArray[i]=new Supervisor(ID, name, department);

You initialized the above array inside the if statement. So when you're saying:System.out.print(svArray[1].toString)Java does not know that you initialized it inside the if statement. anything inside a block{} has a scope that is only valid inside that specific block segment, if that block is not the class type. Suppose you define something out of the class block then you will not get your variable or declaration working within the class block. However, if you use ArrayList then you may not need to initialize the size of it so just simply declaring the ArrayList in the class block will let you use it/initialize it in other methods/ statements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.