Reputation: 79
I have a problem with this code.
the function setListSize is used to set the size of an array via user input as long as it's in the range of [1-50].
The code in the function works when not in a function but when I put the code inside the function I keep getting asked to initialize listA.
Tried to do
int [] listA = null;
but then I got Null Pointer Exception error.
int [] listA;
setListSize(listA);
public static void setListSize (int [] list) {
Scanner reader = new Scanner (System.in);
while (true) {
System.out.print("Enter array size for between [1-50]: ");
int listSize = Integer.parseInt(reader.nextLine());
if (listSize > 50 || listSize < 1) {
System.out.println("Array size must be between [1-50]");
} else {
list = new int [listSize];
break;
}
}
}
What can I do to make this work please?
Thank you for reading
Upvotes: 0
Views: 2149
Reputation: 643
You should understand what Java does inside.
When you have a method(int[] array)
And you assign something to the variable: array = new int[size], Java creates a new array and points the variable 'array' to the new array.
So if you want to assign a value a field outside the method, you should just do it. Call listA directly and assign a value to it.
Upvotes: 1
Reputation: 41208
You don't want to pass in the Array (note that it is an Array not a List, they are different things):
public void setListSize () {
You want to set up your member array:
listA = new int [listSize];
Before you were passing an array into the function then overwriting the reference to that array local to the function only. What you need to do is modify the reference to the array in the main object, for which either listA needs to be static (so the static method can access it) or setListSize needs to be non-static.
To make this function generic do this:
public static int[] createList() {
Scanner reader = new Scanner (System.in);
while (true) {
System.out.print("Enter array size for between [1-50]: ");
int listSize = Integer.parseInt(reader.nextLine());
if (listSize > 50 || listSize < 1) {
System.out.println("Array size must be between [1-50]");
} else {
return new int [listSize];
}
}
}
Then in your calling function just do:
listA = createList();
listB = createList();
etc.
Upvotes: 3
Reputation: 20163
You need to make the array static, and NOT pass it in to the function. Passing it in does indeed create the array, but it only exists during the lifetime of the function. This works:
import java.util.Scanner;
public class SetListSize {
private static int [] listA;
public static final void main(String[] igno_red) {
SetListSize.setListSize();
for(int i : listA) {
System.out.println(i);
}
}
public static void setListSize () {
Scanner reader = new Scanner (System.in);
while (true) {
System.out.print("Enter array size for between [1-50]: ");
int listSize = Integer.parseInt(reader.nextLine());
if (listSize > 50 || listSize < 1) {
System.out.println("Array size must be between [1-50]");
} else {
//Reference the array statically
SetListSize.listA = new int [listSize];
break;
}
}
}
}
Output:
[C:\]java SetListSize
Enter array size for between [1-50]: 12
0
0
0
0
0
0
0
0
0
0
0
0
Upvotes: 2
Reputation: 109593
public static int[] setListSize () {
...
return new int[listSize];
The problem is that passing a variable to a function will not be able to assign to that variable; in fact you are passing the value null. And have in the function a local variable list
initialized with null.
Upvotes: 1