I'm solving a problem where I am given a 2D array. The problem is, it is possible for one of the two arrays not to exist in the given 2D array.
I figured I could do a simple length check or null check but neither of those work. I get an arrayIndexOutOfBounds exception either way.
String smartAssigning(String[][] information) {
int[] employee1 = new int[3];
int[] employee2 = new int[3];
String name1 = "";
String name2 = "";
if(information[1].length <= 0 || information[1] == null)
{ return information[0][0];}
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
at _runefvga.smartAssigning(file.java on line 7)
... 6 more
the first array at index 0 exists but the second array at index 1 does not exist. Is there another way to check for this?
Employeeclass. Don't use so many parallel arrays/variables.[1]but returning[0]?(information[1].length <= 0 || information[1] == null)? So it's: dereference the possible null pointer first and then check, if it wasnull?information[0][0]without checking thatinformation[0]is not null. You are also checking ifinformation[1]is not null after checking its length...the null check should be first