0

My code compiles, but I get this error at runtime:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at GUI.<init>(GUI.java:46)
    at GUI.main(GUI.java:252)
Caused by: java.lang.NullPointerException
    at java.util.Objects.requireNonNull(Objects.java:203)
    at java.util.Arrays$ArrayList.<init>(Arrays.java:3813)
    at java.util.Arrays.asList(Arrays.java:3800)
    at db.<clinit>(db.java:23)
    ... 2 more

This is the code causing the error:

public static String strLine;
public static String[] filearray;
public static List<String> list = Arrays.asList(filearray);


public static void load() throws IOException{

FileInputStream in = new FileInputStream("slist.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in));

filearray = new String[4];

while ((strLine = br.readLine()) != null) {

for (int j = 0; j < filearray.length; j++){
filearray[j] = br.readLine();

}

}
in.close();

Line 46 referenced in the error:

JList stafflist = new JList(db.list.toArray());

I am trying to load a text file as an Array and add it to the JList stafflist, but I get a runtime error.

Ian Lim
  • 3,979
  • 2
  • 26
  • 41
zuse12345
  • 3
  • 1
  • 3

2 Answers2

1

It's in your declaration:

public static String[] filearray;
public static List<String> list = Arrays.asList(filearray);

fileArray is null, and you're trying to create a List around it.

You can either initialize fileArray right here:

public static String[] filearray = new String[4];

Or call Arrays.asList(filearray) after putting the values into filearray.

Anubian Noob
  • 12,897
  • 5
  • 47
  • 69
0

The problem that I see in your code is here:

public static String[] filearray;
public static List<String> list = Arrays.asList(filearray);

You are trying to convert filearray that is null at the time to list using Arrays.asList() method.

You should initialize filearray first.

Mateusz Sroka
  • 2,057
  • 1
  • 15
  • 19