1

Possible Duplicate:
What is a NullReferenceException in .NET?

When i am trying to access value stored in object it shows null reference error when the value is null.How to check for null values???

str = "<html>" + objName.Name + "</html>"

Thanks in Advance

Community
  • 1
  • 1
Rachel
  • 25
  • 4
  • i tried something like str = ""+ if(objName.name.count>0){objName.name} +""..but if cannot be used like this – Rachel Aug 31 '11 at 06:14

4 Answers4

4

How about:

if(objName != null)
{
    str = "<html>" + objName.Name + "</html>"
}
marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
  • Its unclear if they intend to add the even if it doesn't have name value. I would assume if its and html tag that they would want to include it to be complete if its an html page. – Celess Aug 31 '11 at 06:27
4

something like:

if(objName != null && !string.IsNullOrEmpty(objName.Name))
{
  str = "<html>" + objName.Name + "</html>"
}
Davide Piras
  • 42,120
  • 10
  • 86
  • 140
1

Check null for object like below :

str="<html>"+ objName != null ? objName.Name : string.empty + "</html>"
Upendra Chaudhari
  • 6,197
  • 5
  • 21
  • 42
0

Simple case:

str = "<html>" + (objName != null ? objName.Name : "") + "</html>";

Little more performant:

if (objName == null)
   str = "<html></html>";
else
   String.Concat("<html">, objName.Name, "</html>");

If you are doing this a lot, consider using StringBuilder, which might look someting like:

var html = new StringBuilder();
html.Append("<html>");
html.Append(objName != null ? objName.Name : "");
...
html.Append("</html>";

If you are adding several tags together, and not using StringBuilder you need to do someting like this:

str = "<html>" + (objName != null ? objName.Name : "") + "</html>";
str += "<script>" + (objName2 != null ? objName2.Name : "") + "</script>";

... where you put a += to concantinate the new part with the last. Note also that "" is effectively the same as String.Empty. You might benifit greatly from using "" instead of String.Empty, until you learn to make the code shorter and more readable.

For your code:

string str;

str = "<body lang=EN-US style='tab-interval:.5in'>";
str += "<div class=Section1>";
str += "<img img width=100 height=50 id='myImg' src='C:/test.jpg'>";
str += "<p style='font-weight:bold; color:green;'>";
str += "<u> Test Name </u></p>" + objTest.name.ToString() + "<br/>";
str += objname.name.ToString() != null ? string.Join(", ", objname.name.ToArray()) : String.Empty;
str += "</body> </html> "; 

... is going to make this much easier to figure out. You shoudl try the StringBuilder aproach which is nearly the same as this. Should make it much easier overall, I promise :)

So specfically here are a few problems:

`... + objTest.name.ToString() + ...`

This will fail if objTest is null, and makes the

`objname.name.ToString() != null`

not mean much.

You probably intend to do something like this (for both your objTest and objname):

str += "<u> Test Name </u></p>" + (objname != null && objname.name != null ? objname.name.toString() : "") + "<br/>";

This protects both objname and objname.name.

Even better would be:

string str;
var objNameName = (objname != null) ? objname.name : null;
var objTestName = (objTest != null) ? objTest.name : null;

str = "<body lang=EN-US style='tab-interval:.5in'>";
str += "<div class=Section1>";
str += "<img img width=100 height=50 id='myImg' src='C:/test.jpg'>";
str += "<p style='font-weight:bold; color:green;'>";
str += "<u> Test Name </u></p>" + (objTestName != null ? objTestName.ToString() : "") + "<br/>";
str += objNameName != null ? string.Join(", ", objNameName.ToArray()) : "";
str += "</body> </html> "; 

Celess
  • 958
  • 8
  • 18
  • Celess after using str=""+ objName != null ? objName.Name : string.empty + "" the formatting i did under html tag disappeared – Rachel Aug 31 '11 at 07:11
  • I added more. See how that works for you and maybe helps it all make more sense. This part is hard at first, gets better. – Celess Aug 31 '11 at 07:49