C#: NullReferenceException

trevor1940
trevor1940 used Ask the Experts™
on
Hi
In the code bellow I'm getting null because the profile path in the database can be  null

How do I test for this so it dosn't throw a  NullReferenceException

as can be seen in the picture person isn't null person.JPG

        public ActorBox(person person)
        {
            InitializeComponent();
            this.person = person;
            if( (person.profile_path.EndsWith( "JohnWayneBig.png")) || ( person.profile_path is null) )
            {
                ThumbPhotoPath = ImgPath + "JohnWayneSml.png";
                FullPhotoPath = ImgPath + "JohnWayneBig.png";

            }
            else
            {
                ThumbPhotoPath = ImgURL + "w45" + person.profile_path;
                FullPhotoPath = ImgURL + "original" + person.profile_path;
            }
            

            ThumbPictureBox.LoadAsync(ThumbPhotoPath);
            ActorNameLbl.Text = person.name;
        }

Open in new window



System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=FilmsDB
  StackTrace:
   at FilmsDB.ActorBox..ctor(person person) in D:\Vb\MovieDB\FilmsDB\ActorBox.cs:line 34

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
JohnWayne is not Michael Caine

Commented:
Cna you explain, what is your purpose?
NullReferenceException throws, when you ask some property from null object.
For example, if profile_path prorerty is null, this exception will throw if you try to get string length value;

// exception
int len  =person.profile_path.Length;

Open in new window

Eduard GherguArchitect - Coder - Mentor

Commented:
        public ActorBox(person person)
        {

if (person == null)
{
      throw new ArgumentNullException(nameof(person));
}

            InitializeComponent();
            this.person = person;
            if( (person.profile_path.EndsWith( "JohnWayneBig.png")) || ( person.profile_path is null) )
            {
                ThumbPhotoPath = ImgPath + "JohnWayneSml.png";
                FullPhotoPath = ImgPath + "JohnWayneBig.png";

            }
            else
            {
                ThumbPhotoPath = ImgURL + "w45" + person.profile_path;
                FullPhotoPath = ImgURL + "original" + person.profile_path;
            }
            

            ThumbPictureBox.LoadAsync(ThumbPhotoPath);
            ActorNameLbl.Text = person.name;
        }

Open in new window

OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Author

Commented:
Erm
Actor box shows an Image like if person.profile_path is null I'm showing a backup default image "JohnWayneBig.png"

Person is not null only  person.profile_path
Top Expert 2016

Commented:
In SQL if you do a comparison  it returns a null. This is very different from the CLR null semantics, where the null-equals-null comparison returns true.

The following LINQ query is expressed in the CLR, but it is executed in the data source. Because there is no guarantee that CLR semantics will be honored at the data source, the expected behavior is indeterminate.
https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/ef/language-reference/null-comparisons

maybe try isnullorempty

Author

Commented:
I assume you mean thus
 if( (person.profile_path.EndsWith( "JohnWayneBig.png")) || ( string.IsNullOrEmpty( person.profile_path) == true) )

Open in new window


It still causes a null exception

Running this SQL, on the Database returns 5,000+ null profiles_paths
select * from person where profile_path is null;

Open in new window


I could run an SQL update cmd but that doesn't  account for future actors with no picture and doesn't explain why

The last sentence of you link
o accept possible null values, cast the types and the properties of the types that receive query results to nullable types.

Dose that mean create a Null string  something like

            string profile_path = null;
            profile_path = person.profile_path;
            if  (string.IsNullOrEmpty(profile_path) ) 

Open in new window


The above works but seems really odd
Analyst Assistant
Commented:
Can't you use the null-collation (coalescing ?) operator, &&, something like this?
profile_path = person.profile_path) ?? "JohnWayneBig.png";

Open in new window

Eduard GherguArchitect - Coder - Mentor

Commented:
if( (string.IsNullOrWhitespace(person.profile_path) || person.profile_path.EndsWith( "JohnWayneBig.png"))
            {
                ThumbPhotoPath = ImgPath + "JohnWayneSml.png";
                FullPhotoPath = ImgPath + "JohnWayneBig.png";

            }
            else
            {
                ThumbPhotoPath = ImgURL + "w45" + person.profile_path;
                FullPhotoPath = ImgURL + "original" + person.profile_path;
            }

Author

Commented:
Thanx for your help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial