Jump to content

Object reference not set to an instance of an object

SoftwareNinja
Go to solution Solved by minibois,
1 minute ago, ScottDurkin said:

Within my Search.cshtml file up the top I am trying to create a list using this object but every time I add something to the IList I get an error.

image.png.c1c36e88ad1b77bc9420fe9389213d31.png

Error when loading the page.

The app allows me to populate the brand but not the list? how can I resolve this?

pModels.models is a List/Dictionary in the MODELS class that is probably not instantiated.

 

There are basically three options on how to fix this (from best to worst IMO)

1. (Add a function in MODELS to add models). Make a function in MODELS that is called "AddModel". This function inside MODELS will get a string as a parameter, it will check if the list in the class is already instantiated and if not, instantiate it. Then it will add whatever you want, to the list.

2. (Instantiate the list inside MODELS' constructor) The reason why this is not preferred is because you might not always need the list I guess.

3. (Instantiate the list above the pModels.models.Add) That is probably quite bad practice, just like adding something to a class' list outside of said class could be seen as bad practice. Because if someone wants to delete the models-list in MODELS, they now have to check in whatever other classes it is used.

Hi all,

 

I am working on an .net Web Application. I have created a class known as "MODELS" which has a String Variable and a String IList Variable.

 image.png.4be5bb0e318fc9105b79bea49f41ebe9.png

 

Within my Search.cshtml file up the top I am trying to create a list using this object but every time I add something to the IList I get an error.

image.png.c1c36e88ad1b77bc9420fe9389213d31.png

Error when loading the page.

image.thumb.png.2f0a6a481be64936947f9a53e8629d21.png

 

The app allows me to populate the brand but not the list? how can I resolve this?

 

Thanks in advance and look forward to hearing back from you all.

 

Cheers.

Link to comment
Share on other sites

Link to post
Share on other sites

I have solved my own problem..

    List<MODELS> MakeModel = new List<MODELS>();

    MODELS pModels = new MODELS();
    pModels.models = new List<String>();
    pModels.brand = "Audi";

    for(int i = 0; i < 8; i++)
        pModels.models.Add("A" + Convert.ToString(i+1));

    MakeModel.Add(pModels);

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, ScottDurkin said:

Within my Search.cshtml file up the top I am trying to create a list using this object but every time I add something to the IList I get an error.

image.png.c1c36e88ad1b77bc9420fe9389213d31.png

Error when loading the page.

The app allows me to populate the brand but not the list? how can I resolve this?

pModels.models is a List/Dictionary in the MODELS class that is probably not instantiated.

 

There are basically three options on how to fix this (from best to worst IMO)

1. (Add a function in MODELS to add models). Make a function in MODELS that is called "AddModel". This function inside MODELS will get a string as a parameter, it will check if the list in the class is already instantiated and if not, instantiate it. Then it will add whatever you want, to the list.

2. (Instantiate the list inside MODELS' constructor) The reason why this is not preferred is because you might not always need the list I guess.

3. (Instantiate the list above the pModels.models.Add) That is probably quite bad practice, just like adding something to a class' list outside of said class could be seen as bad practice. Because if someone wants to delete the models-list in MODELS, they now have to check in whatever other classes it is used.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, minibois said:

pModels.models is a List/Dictionary in the MODELS class that is probably not instantiated.

 

There are basically three options on how to fix this (from best to worst IMO)

1. (Add a function in MODELS to add models). Make a function in MODELS that is called "AddModel". This function inside MODELS will get a string as a parameter, it will check if the list in the class is already instantiated and if not, instantiate it. Then it will add whatever you want, to the list.

2. (Instantiate the list inside MODELS' constructor) The reason why this is not preferred is because you might not always need the list I guess.

3. (Instantiate the list above the pModels.models.Add) That is probably quite bad practice, just like adding something to a class' list outside of said class could be seen as bad practice. Because if someone wants to delete the models-list in MODELS, they now have to check in whatever other classes it is used.

I will go with your options one. I like that better. Thank you!

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, minibois said:

2. (Instantiate the list inside MODELS' constructor) The reason why this is not preferred is because you might not always need the list I guess.

This is the one I usually prefer (well depending on the circumstance)...if you have a lot of add functions; doing so in the constructor lets it have a minimal CPU footprint (and if other functions go to use the internal models variable you don't have to worry about if it was instantiated).

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

On 5/13/2020 at 1:00 AM, wanderingfool2 said:

This is the one I usually prefer (well depending on the circumstance)...if you have a lot of add functions; doing so in the constructor lets it have a minimal CPU footprint (and if other functions go to use the internal models variable you don't have to worry about if it was instantiated).

This. Or set the default instantiation on the property, like this :

public IList<Thing> { get; set; } = new List<Thing>();

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×