Im getting exception Index was out of range. Must be non-negative and less than the size of the coll

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im trying to Load a file i created before. The content of the file is:File Name=C:Usersbout0_000AppDataLocalAnimationEditorAnimationEditorDATABASEHumanWalking.woa
Object Name=Human
Animation Name=Walking
picturebox.Width=873
picturebox.Height=474
Frame_X_1 =426,427,428,439,440,434,438,429,433,444,425,433
Frame_Y_1 =244,236,227,232,244,241,244,230,233,238,230,245
Number Of Frames=1

Now this is the code that load the file:rivate void loadAnimationToolStripMenuItem_Click(object sender, EventArgs e)
{
Stream myStream = null;
file1 = "";
DialogResult result1;
result1 = new DialogResult();
openFileDialog1.Title = "Select a text file to load";
openFileDialog1.InitialDirectory = @"C:UsersChocoladeAppDataLocalAnimationEditorAnimationEditorDATABASE";
openFileDialog1.FileName = null;
openFileDialog1.Filter = "Text File|*.woa";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
newanimfalse = true;
FileInfo fi;
file1 = openFileDialog1.FileName;
fi = new FileInfo(file1);
string t = fi.DirectoryName;
string fn = fi.Name;
string noExtenstion = Path.GetFileNameWithoutExtension(fn);
// to make an instance of wireobjectanimation here too ? with what name ?
// to enable save object even before i added even one points even if pictureBox1 is empty.
wireObjectAnimation1 = new WireObjectAnimation(t, fn);
wireObject1 = wireObjectAnimation1.wob;
//wireObjectAnimation1.Load(t,fn);
textBox3.Enabled = false;

trackBar1.Enabled = false;
saveBaseFileToolStripMenuItem.Enabled = true;
textBox4.Text = noExtenstion;
textBox3.Text = wireObject1.objectName;
currentFrameIndex = trackBar1.Value;
textBox1.Text = "Frame Number : " + trackBar1.Value;
wireObject1.woc.Set(wireObjectAnimation1.GetFrame(currentFrameIndex));
locked("True");
pictureBox1.Refresh();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
if (result1 == DialogResult.Cancel)
{
}
trackBar1.Enabled = true;
textBox4.Enabled = false;
button8.Enabled = false;
}This is the wireobjectanimation code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using DannyGeneral;

namespace AnimationEditor
{
class WireObjectAnimation
{
private bool fnExt;
private List<WireObjectCoordinates> wocl = new List<WireObjectCoordinates>();

private WireObject wo1 = null;

string name;
public WireObjectAnimation(string name,WireObject wo)
{
fnExt = false;
this.name = name;

wo1 = wo;

WireObjectCoordinates woc;
woc = new WireObjectCoordinates(wo.woc);
wocl.Add(woc);

}

public WireObjectAnimation(string pathName, string fn)
{
fnExt = false;
Load(pathName, fn);
}





public void Save(string path , string fileName , PictureBox pb)
{
int framesNumber = 0;
string fn;
if (fnExt == true)
{
string t = Path.GetFileNameWithoutExtension(this.name);
fn = path + "\" + "DATABASE" + "\" + fileName + "\" + t + ".woa";
}
else
{
fn = path + "\" + "DATABASE" + "\" + fileName + "\" + this.name + ".woa";
}
OptionsFile setting_file = new OptionsFile(fn);
setting_file.SetKey("File Name", fn);
setting_file.SetKey("Object Name", fileName);
setting_file.SetKey("Animation Name", this.name);
setting_file.SetKey("picturebox.Width", pb.Width.ToString());
setting_file.SetKey("picturebox.Height", pb.Height.ToString());
string[] xFrames = new string[wocl.Count];
string[] yFrames = new string[wocl.Count];

string X="";
string Y="";
for (int i = 0; i < wocl.Count; i++)
{
X = string.Format("Frame_X_{0} ", i +1);
Y = string.Format("Frame_Y_{0} ", i +1);
framesNumber ++;
for (int j = 0; j < wocl.Point_X.Count; j++)
{
xFrames += string.Format("{0},", wocl.Point_X[j]);
yFrames += string.Format("{0},", wocl.Point_Y[j]);


}

string tt = xFrames.Trim(",".ToCharArray());
string yy = yFrames.Trim(",".ToCharArray());



setting_file.SetKey(X, tt);
setting_file.SetKey(Y, yy);

}

int result = framesNumber;
setting_file.SetKey("Number Of Frames", result.ToString());







}


public void Load(string path,string fileName)
{
fnExt = true;
int numberofframes = 0;
string t = path + "\" + fileName;
OptionsFile setting_file = new OptionsFile(t);
string objectName = setting_file.GetKey("Object Name");
string XX = setting_file.GetKey("Number Of Frames");
numberofframes = Convert.ToInt32(XX);
wo1 = new WireObject(objectName);
wo1.Load(path);
wocl.Clear();
for (int i = 1; i < numberofframes; i++)
{
string Xkey = string.Format("Frame_X_{0} ", i);
string Ykey = string.Format("Frame_Y_{0} ", i);
WireObjectCoordinates woc = new WireObjectCoordinates();
woc.Point_X = setting_file.GetListFloatKey(Xkey);
woc.Point_Y = setting_file.GetListFloatKey(Ykey);
wocl.Add(woc);
}
}


public void SetFrame(int frameNumber, WireObjectCoordinates woc)
{
wocl[frameNumber].Set(woc);
}

public WireObjectCoordinates GetFrame(int frameNumber)//used to
{
if (frameNumber > wocl.Count)
{
return null;
}
else
{
return wocl[wocl.Count - 1];
}
}

public void AddFrame()
{
WireObjectCoordinates woc;
woc = new WireObjectCoordinates(wocl[wocl.Count - 1]);
wocl.Add(woc);
}

public WireObject wob
{
get
{
return wo1;
}
}

}
}

And this is the wireobject code:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using DannyGeneral;
using System.IO;

namespace AnimationEditor
{
class WireObject
{
private string an;
private bool fnExt;
public string lockObject;
private int idx;
public WireObjectCoordinates woc;
private List<int> connectionStart = new List<int>();
private List<int> connectionEnd = new List<int>();

private const string version = "01.00";

string wo_name;


public WireObject( string name )
{
wo_name = name;
woc = new WireObjectCoordinates();
/*
_frm = frm;
if (_frm != null)
{
tb = (TextBox)_frm.Controls["textBox3"];
}
saveOrLoad = false;
* */
fnExt = false;
}


public void Load( string path)
{
string fn;
fnExt = true;

if (File.Exists(path + "\" + wo_name + ".woo"))
{
fn = path + "\" + wo_name + ".woo";
}
else
{
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");
an = setting_file.GetKey("Animation Name");
lockObject = setting_file.GetKey("Button Lock");
}

And the wireobjectcoordinates:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AnimationEditor
{
class WireObjectCoordinates
{
public List<float> Point_X = new List<float>();
public List<float> Point_Y = new List<float>();

public WireObjectCoordinates()
{
}

public WireObjectCoordinates(WireObjectCoordinates w)
{
Point_X.AddRange(w.Point_X);
Point_Y.AddRange(w.Point_Y);
}

public void Set(WireObjectCoordinates w)
{
if (w == null)
{
}
else
{
for (int i = 0; i < Point_X.Count; i++)
{
Point_X = w.Point_X;
Point_Y = w.Point_Y;
}
}
}
}
}

The problem is when im trying to load the file im getting an exception:
On the line: wireObject1.woc.Set(wireObjectAnimation1.GetFrame(currentFrameIndex));Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: IndexSystem.ArgumentOutOfRangeException was caught
HResult=-2146233086
Message=Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Source=mscorlib
ParamName=index
StackTrace:
at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at System.Collections.Generic.List`1.get_Item(Int32 index)
at AnimationEditor.WireObjectAnimation.GetFrame(Int32 frameNumber) in d:C-SharpAnimationEditorAnimationEditorWireObjectAnimation.cs:line 144
at AnimationEditor.Form1.loadAnimationToolStripMenuItem_Click(Object sender, EventArgs e) in d:C-SharpAnimationEditorAnimationEditorForm1.cs:line 917
InnerException:

Since in the top the content of the file is as it should be what is the problem when trying to load it ? I think something with the number of frames but even if there is one frame it should load the points with coordinates to frame 0 which is like trackBar value 1.File Name=C:Usersbout0_000AppDataLocalAnimationEditorAnimationEditorDATABASEHumanWalking.woa
Object Name=Human
Animation Name=Walking
picturebox.Width=873
picturebox.Height=474
Frame_X_1 =431,436,427,431,438,435,425,435,442,432,436
Frame_Y_1 =238,232,237,240,244,233,229,244,227,243,226
Number Of Frames=1

There is only 1 frame but Frame_X_1 = 431 and Frame_Y_1= 238 so 431,238 is a point and so on there are 11 points.
So when loading the file i suppose to see 11 points in frame 0 which is trackBar value 1 or 0
But why the exception ?

View the full article
 
Back
Top