Recently I'm developing a software that parses and displays XML information from a website. Simple enough right?
I'm getting LOADS of NullReferenceExceptions. For example, this method:
private void SetUserFriends(List<Friend> list)
{
int x = 40;
int y = 3;
if (list != null)
{
foreach (Friend friend in list)
{
FriendControl control = new FriendControl();
control.ID = friend.ID;
control.URL = friend.URL;
control.SetID(friend.ID);
control.SetName(friend.Name);
control.SetImage(friend.Photo);
control.Location = new Point(x, y);
panel2.Controls.Add(control);
y = y + control.Height + 4;
}
}
}
I had to wrap an ugly as sin If around the actual foreach loop in order to prevent an exception.
I feel I'm just putting bandaids on a flat tire instead of actually fixing the problem. Is there any way I can tackle this problem? Maybe a book I should read regarding programming patterns or what not?
Really, I'm lost. I'm probably asking the wrong questions.
SetUserFriends
. If you assume the list of friends should not benull
(which is a fair enough assumption, I would say), then the bug is in whatever is passing innull
. Use the debugger to look up the call stack when you get the exception. – Dean Harding