Add items to list problem

S

shapper

Hello,

I have a class as follows:

public class Theme {

public Subject Subject { get; set; }
public List<Level> Levels { get; set; }
public string Note { get; set; }

public Theme() {
this.Levels.Add(new Level { Type = LevelType.Basico,
Description = "Test" });
}

Level is a class with two properties:
- Type (of type LevelType which is an Enum)
- Description which is a string

I am getting an error:
Object reference not set to an instance of an object.

What am I doing wrong?

Probably this is something simple but I have been around this and I
can't figure out what I am doing wrong.

Thanks,
Miguel
 
S

Stanimir Stoyanov

Hi Miguel,

It appears that this.Levels (actually, the field that is used in the Levels
property) is not initialized. Instantiate it as new List<Level>() in either
your constructor before the this.Levels.Add() call or inline beside the
declaration.
 
R

Rudy Velthuis

shapper said:
Hello,

I have a class as follows:

public class Theme {

public Subject Subject { get; set; }
public List<Level> Levels { get; set; }
public string Note { get; set; }

public Theme() {
this.Levels.Add(new Level { Type = LevelType.Basico,
Description = "Test" });
}

Level is a class with two properties:
- Type (of type LevelType which is an Enum)
- Description which is a string

I am getting an error:
Object reference not set to an instance of an object.

What am I doing wrong?

You never instantiate the list, I guess. Somewhere, you should have
something like:

Levels = new List<Level>();

You could even put such code in the getter, i.e. if Levels is
unassigned, then instantiate it.
 
I

Ignacio Machin ( .NET/ C# MVP )

Hello,

I have a class as follows:

  public class Theme {

    public Subject Subject { get; set; }
    public List<Level> Levels { get; set; }
    public string Note { get; set; }

    public Theme() {
        this.Levels.Add(new Level { Type = LevelType.Basico,
Description = "Test" });
    }

Level is a class with two properties:
- Type (of type LevelType which is an Enum)
- Description which is a string

I am getting an error:
Object reference not set to an instance of an object.

What am I doing wrong?

Probably this is something simple but I have been around this and I
can't figure out what I am doing wrong.

Thanks,
Miguel

You are missing a () set
this.Levels.Add(new()

Tip:
Download & install SP1 , it has a very improved intellisence
 
S

Stanimir Stoyanov

No, the compiler takes care of creating a private field for that purpose in
that case, so it is not accessible to us for modifications. That was
probably a mistake on my part for not interpreting the code Miguel posted
correctly. I figured he might have omitted unnecessary getter/setter code
for his post.
 
S

shapper

No, the compiler takes care of creating a private field for that purpose in
that case, so it is not accessible to us for modifications. That was
probably a mistake on my part for not interpreting the code Miguel posted
correctly. I figured he might have omitted unnecessary getter/setter code
for his post.

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Please help: Object reference not set to an instance of an object ... 2
Parse Array 2
Enum Extentions 7
Enum TypeConverter 3
List Contains Words 4
Null 3
Using Queue with multiple class objects 3
List. CSV. Stream 2

Top