Question

I have a class definition that looks like this:

public class SolrObj
{
[SolrUniqueKey("id")]
public int id { get; set; }

[SolrField("title")]
public string title { get; set; }

[SolrField("description")]
public string description { get; set; }

[SolrField("url")]
public string url { get; set; }
}

And in some code from which SolrObj is accessible, I have this:

SolrObj a = new SolrObj
{
    id = edit_id,
    title = textbox_title.Text,
    description = textbox_description.Text,
    url = textbox_url.Text,
};

However, when the above snippet runs, I get a NullReferenceException on it. I don't see how this can happen as I'm trying to define it right there. a is the null object that throws the exception. How can I fix this?

Sorry for the easy question. The same snippet above works elsewhere in another function, so I'm kind of puzzled here.

EDIT: I see that one of the Text attributes is null and causing this exception; thanks for the answers so far, sorry I'm stupid. How can I get around this? Is there a way I can test for null on the assignment and give an empty string instead? Maybe a ternary operator?

EDIT 2: This is a bad question, by the way. I truncated the class for posting here and excluded an element that used element.SelectedItem.Text. SelectedItem was the null value and the thing tripping us up -- commenters below questioning a TextBox's Text being null are correct, this is not null and shouldn't be null, which was part of the confusion. The null thing was element.SelectedItem (the test data does not have elements selected). Sorry for the confusion and thanks again for the help.

Was it helpful?

Solution

One of your source variables is null:

textbox_title
textbox_description
textbox_url

So when you try to reference it's .Text property it is throwing an Object Reference not set exception, because (null).Text is not a valid expression.

If it is null then you probably have some other error, probably on your .aspx/.ascx. Because normally you would expect a TextBox to exist if your markup is correct.

To check for null use this:

SolrObj a = new SolrObj
{
    id = edit_id,
    title = textbox_title != null ? textbox_title.Text : string.Empty,
    description = textbox_description != null ? textbox_description.Text : string.Empty,
    url = textbox_url != null ? textbox_url.Text : string.Empty,
};

But I strongly suspect that you have something else wrong, because as said before you would not expect a TextBox to be null.

You said that it works elsewhere - is it possible that you have copied the code, but not the markup? Do you actually have an <asp : TextBox id="textbox_title"> on your form?

OTHER TIPS

Are you sure textbox_title, textbox_description and textbox_url are all non-null?

The Text property being null won't cause a null reference exception on object creation, only if one of those variables is actually null. Judging by their names, they shouldn't be. If they may, for some reason, be null, you'll have to resort to

(textbox == null ? "" : textbox.Text);

However, if they all exist but Text might be null, you could use the null coalescing operator:

textbox.Text ?? ""

you need to check null before using it

if(edit_id != null & textbox_title!-= null & textbox_description!= null & textbox_url!=null)
{
SolrObj a = new SolrObj
{
    id = edit_id,
    title = textbox_title.Text,
    description = textbox_description.Text,
    url = textbox_url.Text,
};
}

To fix the problem that others have pointed out:

SolrObj a = new SolrObj
{
    id = edit_id,
    title = textbox_title != null ? textbox_title.Text : "",
    description = textbox_description != null ? textbox_description.Text : "",
    url = textbox_url != null ? textbox_url.Text : "",
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top