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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -