Add items to list problem

  • Thread starter Thread starter shapper
  • Start date Start date
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
 
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.
 
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.
 
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
 
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.
 
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!
 
Back
Top