0

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.

1
  • As David Arno said in his answer, check that s is not null and that s.listItems is not null. Commented Nov 4, 2013 at 20:50

3 Answers 3

9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks; listItems was instantiated in a different class, but I didn't link it properly. It's working now.
@Biggar - If David solved your problem, please accept his answer!
2

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>();

Comments

1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.