I am reading an excel file and the contents are then to be stored in DB. The problem is space check in excel sheet column. Some of the columns are spaces and for them DB values will be null. I have a put check for null value while reading cells but somehow it isn't working as expected.
Code
if (workSheet.Cells[rowIterator, 12].Value != null)
store.WeeklyOff = workSheet.Cells[rowIterator, 12].Value.ToString();
if (workSheet.Cells[rowIterator, 13].Value != null)
store.OpenTime = workSheet.Cells[rowIterator, 13].Value.ToString();
if (workSheet.Cells[rowIterator, 14].Value != null)
store.CloseTime = workSheet.Cells[rowIterator, 14].Value.ToString();
In the above snippet I have put null value check for each cell. The issue is column 12 is also spaces and for this check works fine i.e. if
condition is false and it moves to next statement but from column 13 onwards the null check isn't working. While debugging I can see the null
for column 13 & 14, still != null
check isn't validated and the code in the if
statement gets executed and I get null pointer exception. I have tried a lot many other things for null check but nothing isn't working as expected. Struggling from last 1 day but nothing seem to be working. All the variables are defined as String
. Please advise.