7 Replies - 426 Views - Last Post: 20 May 2020 - 12:26 PM Rate Topic: -----

#1 DarenR   User is offline

  • D.I.C Lover

Reputation: 703
  • View blog
  • Posts: 4,503
  • Joined: 12-January 10

system null reference exception

Posted 20 May 2020 - 08:09 AM

error:

"Object reference not set to an instance of an object."



            foreach (PropertyInfo prop in props){
                try
                {
                    var item = prop.Name;
                    var val = prop.GetValue(obj).ToString() != null ? prop.GetValue(obj).ToString() : "";   <--- happens here
                }
                catch (Exception e)
                {
                    Response.Write("<script>alert('" + e + "');</script>");

                }
         


            }



var val = prop.GetValue(obj).ToString() != null ? prop.GetValue(obj).ToString() : ""; 


is looking for null when it gets to bcc in email it says yes its null --- so shouldnt that line say if it's null make it "" and not error out yet it errors out when it hits null or do you think i should say string.empty()

Is This A Good Question/Topic? 0
  • +

Replies To: system null reference exception

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15728
  • View blog
  • Posts: 63,014
  • Joined: 12-June 08

Re: system null reference exception

Posted 20 May 2020 - 08:16 AM

which object is null in there? prop or obj?
Was This Post Helpful? 0
  • +
  • -

#3 DarenR   User is offline

  • D.I.C Lover

Reputation: 703
  • View blog
  • Posts: 4,503
  • Joined: 12-January 10

Re: system null reference exception

Posted 20 May 2020 - 08:23 AM

obj value is null
prop shows bcc

obj shows null
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15728
  • View blog
  • Posts: 63,014
  • Joined: 12-June 08

Re: system null reference exception

Posted 20 May 2020 - 08:34 AM

I'm wondering if the 'to string' is chucking issues.
Was This Post Helpful? 1
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7391
  • View blog
  • Posts: 24,952
  • Joined: 05-May 12

Re: system null reference exception

Posted 20 May 2020 - 08:36 AM

Instead of looking at where the IDE stops, look at the actual exception callstack. Where is the exception being thrown? Is it being thrown by GetProp(), or is it being thrown by calling ToString() on a bull object returned by GetProp()?
Was This Post Helpful? 0
  • +
  • -

#6 DarenR   User is offline

  • D.I.C Lover

Reputation: 703
  • View blog
  • Posts: 4,503
  • Joined: 12-January 10

Re: system null reference exception

Posted 20 May 2020 - 08:40 AM

it was that dumb to string() good call
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15728
  • View blog
  • Posts: 63,014
  • Joined: 12-June 08

Re: system null reference exception

Posted 20 May 2020 - 08:46 AM

Don't forget you can pimp out the coalesce operator.

https://docs.microso...escing-operator

            string derp = null;
            var dal = derp == null ? "" : derp;
            Console.WriteLine($"{dal}");

            derp = "heck";
            dal = derp == null ? "b" : derp;
            Console.WriteLine($"{dal}");


            string nard = null;
            var val = nard ?? "a"; //coalesce operator
            Console.WriteLine($"{val}");

            nard = "heck";
            val = nard ?? "a";
            Console.WriteLine($"{val}");

Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7391
  • View blog
  • Posts: 24,952
  • Joined: 05-May 12

Re: system null reference exception

Posted 20 May 2020 - 12:26 PM

And better yet, combine that with the "Elvis" operator and you can have:
var val = prop?.GetValue(obj)?.ToString() ?? "";


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1