PC Review


Reply
Thread Tools Rate Thread

Add items to list problem

 
 
shapper
Guest
Posts: n/a
 
      30th Oct 2008
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

 
Reply With Quote
 
 
 
 
Stanimir Stoyanov
Guest
Posts: n/a
 
      30th Oct 2008
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" <(E-Mail Removed)> wrote in message
news:de7831c8-9af3-4869-90a2-(E-Mail Removed)...
> 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
>


 
Reply With Quote
 
Rudy Velthuis
Guest
Posts: n/a
 
      30th Oct 2008
shapper wrote:

> 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.
--
Rudy Velthuis http://rvelthuis.de

"Emulate your heros, but don't carry it too far. Especially
if they are dead."
 
Reply With Quote
 
Family Tree Mike
Guest
Posts: n/a
 
      30th Oct 2008
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" <(E-Mail Removed)> wrote in message
> news:de7831c8-9af3-4869-90a2-(E-Mail Removed)...
> > 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
> >

>
>

 
Reply With Quote
 
Ignacio Machin ( .NET/ C# MVP )
Guest
Posts: n/a
 
      30th Oct 2008
On Oct 30, 2:58*pm, shapper <mdmo...@gmail.com> wrote:
> 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
 
Reply With Quote
 
Stanimir Stoyanov
Guest
Posts: n/a
 
      30th Oct 2008
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" <(E-Mail Removed)> wrote in
message news:05906494-A10A-478A-9BAE-(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
>> news:de7831c8-9af3-4869-90a2-(E-Mail Removed)...
>> > 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
>> >

>>
>>


 
Reply With Quote
 
shapper
Guest
Posts: n/a
 
      31st Oct 2008
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.com> wrote in
> messagenews:05906494-A10A-478A-9BAE-(E-Mail Removed)...
>
> > 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.com> wrote in message
> >>news:de7831c8-9af3-4869-90a2-(E-Mail Removed)....
> >> > 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


Thanks!
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Items Selected from List Box kc-mass Microsoft Access 2 2nd Jan 2010 12:01 PM
Problem counting items in List Box Silvio Microsoft Access Form Coding 5 31st Jul 2009 07:04 PM
List Items problem Cameron Microsoft Frontpage 1 4th Apr 2008 11:13 PM
Roll items in list problem =?Utf-8?B?TG9uaSAtIFJXVA==?= Microsoft Access Form Coding 6 27th Dec 2005 02:02 PM
Problem in disbaleing list items of checkboxlist =?Utf-8?B?UlM=?= Microsoft ASP .NET 1 25th Mar 2004 01:23 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:52 PM.