0

this is the line of code where it shows the error

var physician = (from userAccounts in myDb.Physicians
                            where userAccounts.Phy_UserName == txtUserName.Text
                            select userAccounts).FirstOrDefault();

            setFirstName(physician.Phy_FName);

But in my setter I have a pre condition that if it's a null value it wouldn't do anything but how come it still shoes that error? here's my setters code

public void setFirstName(string fName) {
            if (fName == null)
            {
            }
            else {
                firstName = fName;
            }
        }
1

5 Answers 5

4

I'm guessing that your physician query returns null.

You're then attempting to call a property, Phy_FName, on a null value.

Checking for a null value in your setFirstName method will not protect you in this case, because Phy_FName isn't what's null, physician is.

As an aside, you may also want to check that fName isn't an empty string in your setFirstName method. You could check against both conditions by using if (!String.IsNullOrEmpty(fName))

Sign up to request clarification or add additional context in comments.

8 Comments

That was my assumption at first, however the FirstOrDefault should prevent against that.
no, FirstOrDefault() will return a null value if there's no results. First() will throw an exception.
Sorry, I wasnt thinking of what the default value of a reference type was. My apologies, and thanks for reminding me :)
@user962206 - See my update answer. The problem isn't that Phy_FName is null, it's that physician is null. If physician is null, then attempting to call any property or method that belongs to it will result in the exception you're seeing.
@user962206 - Sort of. Imagine I ask you to read a page out of a book. But I never gave you the book. ... You're trying to access something on a physician, but you have no physician. I would recommend reading the link that @John Saunders posted in the comment to your question. I think it would be very helpful for you.
|
1

Try this:

var physician = (from userAccounts in myDb.Physicians
                            where userAccounts.Phy_UserName == txtUserName.Text
                            select userAccounts).FirstOrDefault();

if(physician != null)
{
  setFirstName(physician.Phy_FName);
}
else
{
  //Throw Error or any any other processing as needed.
}

Comments

0

I bet physician is null. Are you sure you're getting anything back from the query?

Comments

0

To clarify on a wrong assumption of my own that maybe you were thinking also:

FirstOrDefault will return defaults if there is one, but the default for an object is null. So, most likely your Linq query is returning a null value for physician. Of which you are then trying to access the Phy_FName.

Here is a SO question that clarifies this more in depth: Specify default value for a reference type

1 Comment

soo it means the null doesn't mean for Phy_FName only? the Physician Object is null? is that it?
0

The Phy_FName property will be evaluated either way so make sure physician isn't null before trying to read its properties.

You'll need to check the value of physician before attempting to use it:

if (physician != null)
    setFirstName(physician.Phy_FName);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.