I have the following code:
for (int i=0; i<wallMaterialName.size(); i++){
if (Math.abs(calculatedWallUValue - alternativeWallUValue) > 0.01){
if(wallMaterialName.get(i).charAt(0) == 'A'){ //*****ERROR IN HERE*****
PreparedStatement prepStateMat = con.prepareStatement("select * from concretestonefloor where value<? order by value desc limit 1");
prepStateMat.setFloat(1, (Float)wallMaterialLambda.get(i));
ResultSet rsMaterial = prepStateMat.executeQuery();
//...
}
//...
}
}
When I run the code, a “java.lang.NullPointerException” appear.
The error comes from this code:
if(wallMaterialName.get(i).charAt(0) == 'A')
I don’t know why the arraylist is empty since I have checked that it contains some data. Anybody knows how to solve this problem? Thanks.
The array list is not empty, otherwise you’d get an index out of bounds exception (which is impossible to produce in your code, because your loop checks
i < wallMaterialName.size()). However, your array list contains anullobject at indexi.Change your code as follows to fix the error: