473,327 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Add items to list problem

Hello,

I have a class as follows:

public class Theme {

public Subject Subject { get; set; }
public List<LevelLevels { 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

Oct 30 '08 #1
6 1592
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.
--
Stanimir Stoyanov
http://stoyanoff.info

"shapper" <md*****@gmail.comwrote in message
news:de**********************************@k37g2000 hsf.googlegroups.com...
Hello,

I have a class as follows:

public class Theme {

public Subject Subject { get; set; }
public List<LevelLevels { 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
Oct 30 '08 #2
shapper wrote:
Hello,

I have a class as follows:

public class Theme {

public Subject Subject { get; set; }
public List<LevelLevels { 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.
--
Rudy Velthuis http://rvelthuis.de

"Emulate your heros, but don't carry it too far. Especially
if they are dead."
Oct 30 '08 #3
On Oct 30, 2:58*pm, shapper <mdmo...@gmail.comwrote:
Hello,

I have a class as follows:

* public class Theme {

* * public Subject Subject { get; set; }
* * public List<LevelLevels { 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
Oct 30 '08 #4
You cannot put it inline in an automatic property, can you?

"Stanimir Stoyanov" wrote:
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.
--
Stanimir Stoyanov
http://stoyanoff.info

"shapper" <md*****@gmail.comwrote in message
news:de**********************************@k37g2000 hsf.googlegroups.com...
Hello,

I have a class as follows:

public class Theme {

public Subject Subject { get; set; }
public List<LevelLevels { 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

Oct 30 '08 #5
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.

--
Stanimir Stoyanov
http://stoyanoff.info

"Family Tree Mike" <Fa************@discussions.microsoft.comwrote in
message news:05**********************************@microsof t.com...
You cannot put it inline in an automatic property, can you?

"Stanimir Stoyanov" wrote:
>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.
--
Stanimir Stoyanov
http://stoyanoff.info

"shapper" <md*****@gmail.comwrote in message
news:de**********************************@k37g200 0hsf.googlegroups.com...
Hello,

I have a class as follows:

public class Theme {

public Subject Subject { get; set; }
public List<LevelLevels { 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

Oct 30 '08 #6
On Oct 30, 7:28*pm, "Stanimir Stoyanov" <stoya...@REMOVETHIS.live.com>
wrote:
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.

--
Stanimir Stoyanovhttp://stoyanoff.info

"Family Tree Mike" <FamilyTreeM...@discussions.microsoft.comwrote in
messagenews:05**********************************@m icrosoft.com...
You cannot put it inline in an automatic property, can you?
"Stanimir Stoyanov" wrote:
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.
--
Stanimir Stoyanov
http://stoyanoff.info
"shapper" <mdmo...@gmail.comwrote in message
news:de**********************************@k37g200 0hsf.googlegroups.com....
Hello,
I have a class as follows:
*public class Theme {
* *public Subject Subject { get; set; }
* *public List<LevelLevels { 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
Thanks!
Oct 31 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Charlotte Henkle | last post by:
Hello; I'm pondering how to count the number of times an item appears in total in a nested list. For example: myList=,,] I'd like to know that a appeared three times, and b appeared twice,...
1
by: Kay | last post by:
I have a problem in inserting items in linked list. Hence, there are different way to insert the items to the list. The first one is loading data form a txt file(1). And the second one is input...
3
by: Jeremy Owens-Boggs | last post by:
We are trying to implement a dual list box selection where you have two list boxes, You highlight items in the right side list box, click a button and this moves those items over to the left hand...
1
by: Aaron Prohaska | last post by:
I'm having the problem with this drop down list on postback. For some reason both the ListItems get selected when I change the selected item. Using the code below I'm building the drop down list in...
5
by: Kay | last post by:
Hello, I have two list boxes on my form, one initially displays with items and the other displays blank, by clicking a button, it is possible to move items from one box to another and vice a...
10
by: Adam Clauss | last post by:
I have a page containing a list box. This list may contain duplicate items - in which the ORDER is important. ex: a b b a is significant as compared to: b
7
by: Flavio | last post by:
Hi, I have a QListview widget that allows me to store a bunch of strings in it. This strings can be visualized, sorted, selected, etc. My Problem is that I cant find a way to get the user...
13
by: Joel Koltner | last post by:
Is there an easy way to get a list comprehension to produce a flat list of, say, for each input argument? E.g., I'd like to do something like: for x in range(4) ] ....and receive
13
by: PetterL | last post by:
I writing a program where I read menu items from a file. But I have problem when I click an menu item i want it to mark that one as checked but I cant access the menu object of that item to see...
4
by: Keith Hughitt | last post by:
For example, If you have a list: <ul> <li>item 1 is short.</li> <li>item 2 is a little bit longer</li> </ul> regardless of the size of the contents of each list item, the element
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.