Checking the validity of index before using it is also a good idea:
if ( index == null || index.length == 0 ) return -1;
Your while loop should look more like this:
while ( x < index[0].length )
{
if ( index[0][x] == null )
{
x++;
continue; // skip possible null entries.
}
if ( index[0][x].contains(var) )
{
System.out.println("x = " + x + ", val = " + index[0][x]);
return x; // return the position found.
}
x++;
}
return -1;
Using a for loop (which I prefer):
for ( int x = 0; x < index[0].length; x++ )
{
if ( index[0][x] == null )
continue; // skip possible null entries.
if ( index[0][x].contains(var) )
{
System.out.println("x = " + x + ", val = " + index[0][x]);
return x; // return the position found.
}
}