Reputation: 159
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
Upvotes: 1
Views: 18966
Reputation: 450
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.
Upvotes: 0
Reputation: 26978
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());
Upvotes: 3
Reputation: 2108
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.
svArray
now occur on the same line within your if
statement.System.out.println
call now also happens inside your if
statement so that svArray
is still in scope.toString()
method, rather than attempting to access an attribute called toString
(which doesn't exist).Upvotes: 0
Reputation: 2429
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.
Upvotes: 1
Reputation: 7466
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.
Upvotes: 2