0

I have a search page which is using strongly typed objects, but I have the values broken into specific groups.

Code behind page calls the following when the user clicks the search button (none of these fields are empty):

SearchCriteria sc = new SearchCriteria();

sc.Generic.id = txtId.Text;
sc.Generic.maxReturned = rblMaxReturned.SelectedIndex;
sc.DisplayOnly.category = txtCategory.Text;
sc.DisplayOnly.type = txtType.Text;
sc.Building.address = txtAddress.Text;
sc.Building.city = txtCity.Text;

The DataType file is defined like this:

[Serializable]
public class SearchCriteria 
{
    public _Generic Generic { get;set; }
    [Serializable]
    public class _Generic 
    {
        public int id {get;set;}
        public int maxReturned {get;set;}
    }

    public _DisplayOnly DisplayOnly { get;set; }
    [Serializable]
    public class _DisplayOnly 
    {
        public int category {get;set;}
        public int type {get;set;}
    }

    public _Building Building { get;set; }
    [Serializable]
    public class _Building 
    {
        public int address {get;set;}
        public int city {get;set;}
    }
}

When the code executes, I get a nullreferenceerror even though all the items in the various textboxes have a value. However, if I take out the public _Building Building { get;set; } and call the class directly it works and populates the values. What's the best solution here? Should I not use intermediary definition and call the class directly? If so, how can I call the different groups without making four different calls on the code behind page?

2 Answers 2

1

You need to initialize the internal class instances. Simply declaring the variables doesn't mean that you can access their properties without creating the instances. You could easily do that in the constructor of the SearchCriteria class

[Serializable]
public class SearchCriteria 
{
    public SearchCriteria()
    {
         // Without these initialization the internal variables are all null
         // and so assigning any property of a null object causes the error
         Generic = new _Generic();
         DisplayOnly = new _DisplayOnly()
         Building = new _Building();
    }
    .....
}
Sign up to request clarification or add additional context in comments.

Comments

0

When you create a new instance of your SearchCriteria class, the properties are not initialized, and so they all have a value of null. So now look at the very first line where you try to use one of those properties:

sc.Generic.id = txtId.Text;

Here, txtID.Text is perfectly fine, but sc.Generic is null. When you try to look up the it's .id property for assignment, that's where the exception is thrown.

To fix this, you need to initialize each of those properties to have an instance of their type. Additionally, it's probably a good idea to use a private set, like so:

public _Generic Generic { get;private set; }

This will still allow to make all the same assignments that are currently written, because that only requires a get action to retrieve the type instance. The assignment/set operation is on the property of the property.

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.