Problem with List<>
-
Hi, I have a problem with VS2010beta2 and .NET Framework Client Profile, although I would expect the problem to be something to do with my inexperienced coding. I have this block of code..
struct sGamePort { public string sgpProtocol; public UInt16 sgpPortStart; public UInt16 sgpPortEnd; } struct sGamePortItem { public string sgpName; public List<sGamePort> sgpPortList; } List<sGamePortItem> sGames; private sGamePort ParsePorts(string\[\] line) { sGamePort gp = new sGamePort(); gp.sgpProtocol = line\[0\]; int num = line.Count(); if (num == 2) { // single port opened... gp.sgpPortStart = Convert.ToUInt16(line\[1\]); gp.sgpPortEnd = Convert.ToUInt16(line\[1\]); } if (num == 3) { // port range opened... gp.sgpPortStart = Convert.ToUInt16(line\[1\]); gp.sgpPortEnd = Convert.ToUInt16(line\[2\]); } return gp; }
Where line is a string array which essentially holds some tokens. ie. line[0] = "NAME" line[1] = "JOHN" line[2] = "YOUNG" Now, in a seperate function I have
sGamePortItem pi = new sGamePortItem();
pi.sgpProtocol = "TCP";
pi.sgpPortList.Add(ParsePorts(strCMD));This gives me a Null Reference Exception, so I changed it to this...
sGamePortItem pi = new sGamePortItem();
pi.sgpProtocol = "TCP";
sGamePort test = new sGamePort();
test = ParsePorts(strCMD);
pi.sgpPortList.Add(test); // ERROR HERENow, the error only occurs on the specified line above.. What am I doing wrong? Is it to do with me using the new keyword incorrectly? Or do I not use 'new'? Any help would be appreciated as this has been confusing me for a few hours now, lol Thanks again. John
Yippee Kai Yai Signed Polomint.......
-
Hi, I have a problem with VS2010beta2 and .NET Framework Client Profile, although I would expect the problem to be something to do with my inexperienced coding. I have this block of code..
struct sGamePort { public string sgpProtocol; public UInt16 sgpPortStart; public UInt16 sgpPortEnd; } struct sGamePortItem { public string sgpName; public List<sGamePort> sgpPortList; } List<sGamePortItem> sGames; private sGamePort ParsePorts(string\[\] line) { sGamePort gp = new sGamePort(); gp.sgpProtocol = line\[0\]; int num = line.Count(); if (num == 2) { // single port opened... gp.sgpPortStart = Convert.ToUInt16(line\[1\]); gp.sgpPortEnd = Convert.ToUInt16(line\[1\]); } if (num == 3) { // port range opened... gp.sgpPortStart = Convert.ToUInt16(line\[1\]); gp.sgpPortEnd = Convert.ToUInt16(line\[2\]); } return gp; }
Where line is a string array which essentially holds some tokens. ie. line[0] = "NAME" line[1] = "JOHN" line[2] = "YOUNG" Now, in a seperate function I have
sGamePortItem pi = new sGamePortItem();
pi.sgpProtocol = "TCP";
pi.sgpPortList.Add(ParsePorts(strCMD));This gives me a Null Reference Exception, so I changed it to this...
sGamePortItem pi = new sGamePortItem();
pi.sgpProtocol = "TCP";
sGamePort test = new sGamePort();
test = ParsePorts(strCMD);
pi.sgpPortList.Add(test); // ERROR HERENow, the error only occurs on the specified line above.. What am I doing wrong? Is it to do with me using the new keyword incorrectly? Or do I not use 'new'? Any help would be appreciated as this has been confusing me for a few hours now, lol Thanks again. John
Yippee Kai Yai Signed Polomint.......
The problem is that your line:
public List<sGamePort> sgpPortList;
should be:
public List<sGamePort> sgpPortList = new List<sGamePort>();
Need to assign a target to
sgpPortList
before you dereference it, hence the null ref exception. -
The problem is that your line:
public List<sGamePort> sgpPortList;
should be:
public List<sGamePort> sgpPortList = new List<sGamePort>();
Need to assign a target to
sgpPortList
before you dereference it, hence the null ref exception.Damn, I knew it would be something trivial... Thanks for waking me up to a silly mistake, :-D :-D :-D EDIT: Now I'm getting the error "cannot have instance field initializers in structs"... Hmmm, this could be a pain... EDIT AGAIN: I realised what was wrong.. I hadn't been initialising the sGamePortItem in the list..
sGamePortItem pi = new sGamePortItem();
pi.sgpPortList = new List<sGamePort>(); // THIS WAS NEEDED.Thank you for your help, it made me think more about the initialisation...
Yippee Kai Yai Signed Polomint.......
modified on Thursday, December 3, 2009 2:09 PM
-
Hi, I have a problem with VS2010beta2 and .NET Framework Client Profile, although I would expect the problem to be something to do with my inexperienced coding. I have this block of code..
struct sGamePort { public string sgpProtocol; public UInt16 sgpPortStart; public UInt16 sgpPortEnd; } struct sGamePortItem { public string sgpName; public List<sGamePort> sgpPortList; } List<sGamePortItem> sGames; private sGamePort ParsePorts(string\[\] line) { sGamePort gp = new sGamePort(); gp.sgpProtocol = line\[0\]; int num = line.Count(); if (num == 2) { // single port opened... gp.sgpPortStart = Convert.ToUInt16(line\[1\]); gp.sgpPortEnd = Convert.ToUInt16(line\[1\]); } if (num == 3) { // port range opened... gp.sgpPortStart = Convert.ToUInt16(line\[1\]); gp.sgpPortEnd = Convert.ToUInt16(line\[2\]); } return gp; }
Where line is a string array which essentially holds some tokens. ie. line[0] = "NAME" line[1] = "JOHN" line[2] = "YOUNG" Now, in a seperate function I have
sGamePortItem pi = new sGamePortItem();
pi.sgpProtocol = "TCP";
pi.sgpPortList.Add(ParsePorts(strCMD));This gives me a Null Reference Exception, so I changed it to this...
sGamePortItem pi = new sGamePortItem();
pi.sgpProtocol = "TCP";
sGamePort test = new sGamePort();
test = ParsePorts(strCMD);
pi.sgpPortList.Add(test); // ERROR HERENow, the error only occurs on the specified line above.. What am I doing wrong? Is it to do with me using the new keyword incorrectly? Or do I not use 'new'? Any help would be appreciated as this has been confusing me for a few hours now, lol Thanks again. John
Yippee Kai Yai Signed Polomint.......
You need to initialize the list of the struct "sGamePortItem" before assigning the object.