-1

I am trying to check a value of string.

However sometimes the string may contain a null value.

 Boolean checkValue =  segment.CheckStringValue(stringValue)

When this happens then I get the following error nullreferenceexception was unhandled

Is there a way to avoid this from happening?

user3191666
  • 159
  • 4
  • 17

2 Answers2

2

replace

Boolean checkValue =  segment.CheckStringValue(stringValue);

with

Boolean checkValue =  segment.CheckStringValue(stringValue ?? String.Empty);

or handle a null value within your method if (value == null) ...

fubo
  • 39,783
  • 16
  • 92
  • 127
  • The method title `CheckStringValue` would lead me to believe it would be better to have a check inside the method, Not much to be gained by checking against a different string. (Throwing the error might actually be the better thing to do) – Sayse May 29 '15 at 09:58
0

You can try doing this:

String stringCheck = "";
if(stringValue != null
  stringCheck=stringValue;
Boolean checkValue =  segment.CheckStringValue(stringCheck);
Ofir
  • 141
  • 14