Object reference exception when I try assign cell value to string [duplicate] - c#

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 8 years ago.
I tried convert to string value of cell in a datagridview:
string t = row.Cells[0].Value.ToString()== null ? String.Empty : row.Cells[0].Value.ToString();
MessageBox.Show(t);
MessageBox shows correctly value but application gives exception:
Object reference not set to an instance of an object.

Value property could be null; Try this
string t = row.Cells[0].Value == null ? String.Empty : row.Cells[0].Value.ToString();

Related

c# replace character by an empty string [duplicate]

This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 5 years ago.
I'm getting a string from a Json :
var value = JsonObject["price"]; //value = "1,560";
i'm trying to replace the ',' with an empty string :
value.Replace(",",string.Empty);
but i'm still getting the value with "," that's so strange and i'm stuck at it
thanks in advance
value = value.Replace( ", ", string.Empty);
strings in .net are immutable.
Per the documentation for String.Replace:
Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
It gives you a new string; it doesn't modify the existing one. So you need to assign the result to a variable:
value = value.Replace(",", string.Empty);

Casting XElement to int? fails when xsi:nil = "true" is specified [duplicate]

This question already has answers here:
LinqToXml does not handle nillable elements as expected
(3 answers)
Closed 6 years ago.
The XElement has explicit support for casting to Nullable<int> but it's not working as I expected. The following unit test demonstrates the problem:
[TestMethod]
public void CastingNullableInt()
{
var xdoc = XDocument.Parse("<root xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><okay>123</okay><boom xsi:nil=\"true\"/></root>");
Assert.AreEqual(123, (int?)xdoc.Root.Element("okay"));
Assert.IsNull((int?)xdoc.Root.Element("boom"));
}
The test should pass the last assert. Instead it gives an FormatException:
Input string was not in a correct format.
Why doesn't it parse to null correctly here?
Linq to XML is not schema aware so it will not convert xsi:nil = "true" to a nullable variable. To test this you would need to do something like:
Assert.IsTrue((bool?)xdoc.Root.Element("boom").Attribute("{http://www.w3.org/2001/XMLSchema-instance}nil") == true);
XElement does not parse <boom xsi:nil=\"true\"/> correctly. It only works if you omit <boom xsi:nil=\"true\"/>, then the value is null and it returns (int?)null.
A workaround could be to first check on the value not being empty:
!string.IsNullOrEmpty((string)xdoc.Root.Element("boom"))
? (int?)xdoc.Root.Element("boom")
: null
;
You can check IsEmpty property:
var value = xdoc.Root.Element("boom").IsEmpty ? null : (int?)xdoc.Root.Element("boom");

C# System.NullException convert null to blank or no value [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 7 years ago.
When using the following method there are use cases where one of the parameters, for this example RC_2 (datetime) may pass a null value. I need to have that null value converted into either a blank value or a string value so that it can be passed to a stringbuilder below is what I'm currently using,
public static string CHG_FixDatetime(string RC_1, string RC_2, string seperator)
{
var sb = new StringBuilder();
sb.Append(RC_1);
sb.Append(RC_2);
sb.Append(seperator);
DateTime dt;
if (!string.IsNullorWhiteSpace(RC_2))
sb.append(RC_2)
else
{
sb.Append(Convert.ToString(RC_2));
}
return sb.ToString();
}
I've tried multiple configurations and variations, but I'm unable to get past the System.NullException that is thrown when the RC_2 value is Null. Any help is appreciated.
Try the following solution
RC_2 = String.IsNullOrWhiteSpace(RC_2) ? "" : RC_2;

How to check if a variable is empty in wp7? [duplicate]

This question already has answers here:
Easier way of writing null or empty?
(7 answers)
How can I check whether a string variable is empty or null in C#? [duplicate]
(6 answers)
Closed 9 years ago.
In my app, I want to check if a string variable is empty.
I've handled it as follows,
if ((Name == null) || (Name == ""))
{
//Handled
}
But it passes this condition, if the value is given as " "(whitespace).
How can i detect if the variable contains only whitespaces??
Thanks in advance!
Use String.IsNullOrWhiteSpace:
if (String.IsNullOrWhiteSpace(Name))
{
//Handled
}
String.IsNullOrWhiteSpace(Name)
And MSDN article about it
Use String.IsNullOrEmpty to check null or empty and String.IsNullOrWhiteSpace to check null or whitespace
Use String.IsNullOrEmpty to check if it is null and String.IsNullOrWhiteSpace
to chack for whitespace!! For your case String.IsNullOrWhitespace(Name)

String.Empty is not a constant value? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why isn’t String.Empty a constant?
To make my code more readable I tried to assign String.Empty to a constant value:
const string PLATYPUS_ADDED_AND_ACCEPTED = string.Empty;
if (false) { }
else
{
toolTip = PLATYPUS_ADDED_AND_ACCEPTED;
}
but I get "the expression being added must be constant"
Isn't String.Empty always the same thing? That seems pretty constant to me.
string.Empty is a readonly field, not a constant.
The compiler has no way to know this will always be the same value.

Categories