I have a problem with Load and Save function when loading or saving text files

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
This is the two functions:

<pre class="prettyprint public void Save(string path , bool Locked)
{
string fn = path + "\" +"DATABASE" +"\" + wo_name + "\" + wo_name + ".txt";
OptionsFile setting_file = new OptionsFile(fn);
setting_file.SetKey("File Name", fn);
setting_file.SetKey("Version", version);
setting_file.SetKey("Button Lock", Locked.ToString());


setting_file.SetListFloatKey("Coordinates_X", woc.Point_X);
setting_file.SetListFloatKey("Coordinates_Y", woc.Point_Y);

setting_file.SetListIntKey("ConnectionStart", connectionStart);
setting_file.SetListIntKey("ConnectionEnd", connectionEnd);

}

public void Load( string path)
{

string fn = path + "\" + wo_name;
OptionsFile setting_file = new OptionsFile(fn);


woc.Point_X = new List<float>();
woc.Point_Y = new List<float>();

woc.Point_X = setting_file.GetListFloatKey("Coordinates_X");
woc.Point_Y = setting_file.GetListFloatKey("Coordinates_Y");

connectionStart = new List<int>();
connectionEnd = new List<int>();

connectionStart = setting_file.GetListIntKey("ConnectionStart");
connectionEnd = setting_file.GetListIntKey("ConnectionEnd");

lockObject = setting_file.GetKey("Button Lock");
}[/code]
The problem is when im trying to Load or Save when im running the program and the Lists either in the Save or Load functions are empty.
When im running the program first time and i didnt added any points or did connect two points yet so the Lists are empty.
But i do have a text file i saved before wich have a data inside for example:
<pre class="prettyprint File Name=C:UsersChocoladeAppDataLocalAnimationEditorAnimationEditorDATABASEHumanHuman.txt
Version=01.00
Button Lock=False
Coordinates_X=321
Coordinates_Y=204[/code]
So there is Coordinates X and Y meaning a point have been saved.
But when im running the program and make a Load right away all the Lists are empty.

The problem is in the OptionsFile <span class="x_word module:

<pre class="prettyprint public List<float> GetListFloatKey(string keys)
{
List<float> result = new List<float>();
string s = GetKey(keys);
string[] items = s.Split(new char[] { , });
float f;
foreach (string item in items)
{
if (float.TryParse(item, out f))
result.Add(f);
}
return result;
}[/code]
The error im getting is on:
<pre class="prettyprint string[] items = s.Split(new char[] { , });[/code]
The variable s is null. In the keys variable in this case its conatining "ConnectionStart"
The error is: Object reference not set to an instance of an object

Now thats when i load right away when the program is running. If i will add some points and connect them then it will be ok.
But the idea should be in the case that i run the program and load the file before i did anything.

If in the WireObject class in the Load and Save function i will add a check If (woc.Point_X ==0) {}
Else
Do the Loading all the GetKey lines.
But then it will be allways == 0 when im running the program.
So what can be a solution ? Either in the Save or Load functions or in both.




<hr class="sig danieli

View the full article
 
Back
Top