- #1
SlurrerOfSpeech
- 141
- 11
I have a class like
but when I use that class in the following procedure
I'm getting a NullReferenceException on the line
and I know (because I've stepped through and checked) that
and
are not NULL. Therefore NULL is some part of
and I'm wondering if you can help me figure out why.
Code:
public class FriendList
{
public Dictionary<string, int> names { get; set; } // List of friends names and number of occurrences (in case two or more friends have the same name)
public DateTime timestamp { get; set; } // date/time on the data file
public static FriendList operator / ( FriendList first, FriendList second )
{
// Returns a FriendList of every friend in first but not second, with the timestamp
// of the returned object being an arbitrary value.
FriendList firstNotSecond = new FriendList();
foreach ( KeyValuePair<string,int> thisName in first.names )
{
int secondNum = second.names.ContainsKey(thisName.Key) ? second.names[thisName.Key] : 0;
if ( thisName.Value > secondNum )
{
firstNotSecond.names[thisName.Key] = thisName.Value - secondNum;
}
}
return firstNotSecond;
}
public void PrintInfo ( )
{
Console.WriteLine("Friends from list timestamped with {0}", this.timestamp.ToString());
foreach ( KeyValuePair<string, int> thisFriend in this.names )
{
Console.WriteLine("\t\t{0} ({1})", thisFriend.Key, thisFriend.Value);
}
}
public int CountFriends ( )
{
int count = 0;
foreach ( KeyValuePair<string, int> namein this.names )
{
count += name.Value;
}
return count;
}
}
but when I use that class in the following procedure
Code:
private void BuildFriendHistory ( )
{
// Takes all text files currently in the Data folder
// and extracts their friends lists into a List<FriendList>.
// Prints any error encountered along the way.
this._fhist = new List<FriendList>();
foreach ( string thisFilePath in Directory.GetFiles(this._datadir) )
{
FriendList flist = new FriendList();
string line;
System.IO.StreamReader file = new System.IO.StreamReader(thisFilePath);
while ( (line = file.ReadLine()) != null )
{
int k = line.LastIndexOf(' ');
flist.names[line.Substring(0, k)] = Int32.Parse(line.Substring(k + 1));
}
this._fhist.Add(flist);
}
}
I'm getting a NullReferenceException on the line
Code:
flist.names[line.Substring(0, k)] = Int32.Parse(line.Substring(k + 1))
and I know (because I've stepped through and checked) that
Code:
line.Substring(0, k)
and
Code:
Int32.Parse(line.Substring(k + 1))
are not NULL. Therefore NULL is some part of
Code:
flist.names
and I'm wondering if you can help me figure out why.