Biggar
Biggar

Reputation: 3

Object reference not set to instance of object, despite creating a new object

I'm currently working on a basic drawing program. One of the requirements is to be able to save the list of drawn objects and load it back in. So far, I have written a save function that exports all items of the list to an XML format, and I'm currently working on the loading-part.

Whenever the program encounters a "< RechthoekTool >" (Dutch for RectangleTool), it executes the following code:

//Create new tool.
RechthoekTool tool = new RechthoekTool();

//Turn <Startpoint> and <Endpoint> into actual points
var sp = Regex.Replace(xn["Startpunt"].InnerText, @"[\{\}a-zA-Z=]", "").Split(',');
tool.startpunt = new Point(int.Parse(sp[0]), int.Parse(sp[1]));
var ep = Regex.Replace(xn["Eindpunt"].InnerText, @"[\{\}a-zA-Z=]", "").Split(',');
tool.eindpunt = new Point(int.Parse(ep[0]), int.Parse(ep[1]));

//Set colour and width of brush
string kleur = xn["Dikte"].InnerText;
kleur.Replace(@"Color [", "");
kleur.Replace(@"]", "");
Color c = Color.FromName(kleur);
tool.kwastkleur = c;
tool.kwast = new SolidBrush(c);
tool.dikte = int.Parse(xn["Dikte"].InnerText);

//Add to list
s.listItems.Add(tool);

Whenever I run the program, I get the 'NullReferenceException was unhandled' error("Object reference not set to an instance of an object.") at

s.listItems.Add(tool);

I do, however, instantiate the tool right at the beginning, don't I? What could be causing this error? Some Googling told me it might be because I forgot to assign a property, but as far as I can tell I've got them all covered...

Help would be greatly appreciated.

Upvotes: 0

Views: 306

Answers (3)

Brian
Brian

Reputation: 5119

If you don't instantiate something using the new keyword; it simply won't work.

Using your code, try this:

//Create new tool.
RechthoekTool tool = new RechthoekTool();

//Turn <Startpoint> and <Endpoint> into actual points
var sp = Regex.Replace(xn["Startpunt"].InnerText, @"[\{\}a-zA-Z=]", "").Split(',');
tool.startpunt = new Point(int.Parse(sp[0]), int.Parse(sp[1]));
var ep = Regex.Replace(xn["Eindpunt"].InnerText, @"[\{\}a-zA-Z=]", "").Split(',');
tool.eindpunt = new Point(int.Parse(ep[0]), int.Parse(ep[1]));

//Set colour and width of brush
string kleur = xn["Dikte"].InnerText;
kleur.Replace(@"Color [", "");
kleur.Replace(@"]", "");
Color c = Color.FromName(kleur);
tool.kwastkleur = c;
tool.kwast = new SolidBrush(c);
tool.dikte = int.Parse(xn["Dikte"].InnerText);

List<RechthoekTool> s = new List<RechthoekTool>();  // You can now use your list.
//Add to list
s.listItems.Add(tool);

Upvotes: 1

gleng
gleng

Reputation: 6304

The problem is that you aren't instantiating listItems or s. Not enough information is given to tell you how to instantiate s but you can do the other one like this:

s.listItems = new List<RechthoekTool>();

Upvotes: 2

David Arno
David Arno

Reputation: 43254

The error is due to s or s.listItems not being instantiated.

Without seeing more code, it's difficult to know which is null, but at a guess you are creating a new object for s, which contains a property/field listItems, but you aren't assigning a list to listItems.

Upvotes: 9

Related Questions