C# Can't iterate through string array -
i trying iterate through string array , if contains string add 1 integer i'm getting error on if statement saying:
nullreferenceexception unhandled object reference not set instance of object
/
(int = 0; < globaldata.cellh.length; i++) { if (globaldata.cellh[i].contains("m")) { globaldata.totalboys++; } else if (globaldata.cellh[i].contains("f")) { globaldata.totalgirls++; } else if (globaldata.cellh[i].contains(null)) { } }
fixed:
(int = 0; < globaldata.cellh.length; i++) { if (globaldata.cellh[i] == "m") { globaldata.totalboys++; } else if (globaldata.cellh[i] == "f") { globaldata.totalgirls++; } else if (globaldata.cellh[i] == "") { } }
you need perform null check first, try instead:
for (int = 0; < globaldata.cellh.length; i++) { if (globaldata.cellh[i] == null) { } else if (globaldata.cellh[i].contains("m")) { globaldata.totalboys++; } else if (globaldata.cellh[i].contains("f")) { globaldata.totalgirls++; } }
also check equality null, opposed checking if string contains null.
Comments
Post a Comment