The type initializer for '... ' threw an exception
سلام. برنامه يك منو داره با دو گزينه كه هركدوم بناست يك فرم جديد رو باز كنن.وقتي اجرا ميكنم ، اولين گزينه كه انتخاب ميشه(هركدوم رو كه انتخاب كنم فرق نميكنه) فرم مربوطش باز ميشه و مشكلي نيست.اما وقتي فرمش رو ببندم و بخوام از منو اون يكي گزينه رو انتخاب كنم،اين ارور رو ميده:The type initializer for 'p1.find' threw an exception
كد:
namespace p1
{
publicpartialclassMain : Form
{
public Main()
{
InitializeComponent();
}
privatevoid ToolStripMenuItem_Click(object sender, EventArgs e)
{
add add = newadd();
add.Show();
add.SetDesktopLocation(50, 50);
add.Activate();
}
privatevoid ToolStripMenuItem1_Click(object sender, EventArgs e)
{
find fnd = newfind();
fnd.Show();
fnd.SetDesktopLocation(50, 50);
fnd.Activate();
}
}
}
لطفا راهنمايي كنيد.ممنونم.
نقل قول: The type initializer for '... ' threw an exception
سايت هاي خارجي رو زياد گشتم براي اين ارور.ولي چيزي كه به دردم بخوره و بفهممش پيدا نكردم.كسي تا بحال با اين مشكل برخورد نكرده؟
نقل قول: The type initializer for '... ' threw an exception
كد سازنده كلاس find خطا دارد!
نقل قول: The type initializer for '... ' threw an exception
سعی کنید از اسامی بهتری استفاده کنید.
هر دو فرم شما از کلماتی بعنوان اسم استفاده میکنند که ممکن است مشکل ساز شوند.
در شرایطی که مجبور به استفاده از این اسامی هستید ،برای مثال میتونید از یک "frm" بعنوان پیشوند یا پسوند در اون استفاده کنید.
در مورد مشکلتون هم ، ظاهراً متد find در p1 وجود نداره که این خطا رو دریافت میکنید.
اگر بتونید همه(ویا قسمت بیشتری از) کدتون رو اینجا بگذارید راحت تر میشه این مشکل رو حل کرد.
نقل قول: The type initializer for '... ' threw an exception
حق باشماست.هنوز برنامه خيلي كار داره و اين ريزه كارياشو درست ميكنم.وقتي براي ارور قسمت view detail رو نگاه كردم مقدار InnerException رو خوندم مربوط ميشد به اينكه نميتونه فايلي رو كه در برنامه باز ميكنم رو باز كنه.وقتي فايل رو آخر هر متدي كه با فايل استريم كار كرده بستم، ظاهرا مشكل حل شد.از اين مطلب هم استفاده كردم:
http://www.xtremevbtalk.com/showthread.php?t=305884
نقل قول: The type initializer for '... ' threw an exception
publicpartialclassadd : Form
{
String text;
staticFileStream fs1 = newFileStream(@"c:\std.txt", FileMode.Append, FileAccess.Write);
StreamWriter tw = newStreamWriter(fs1);
public add()
{
InitializeComponent();
}
privatevoid Form1_Load(object sender, EventArgs e)
{
}
publicvoid button1_Click(object sender, EventArgs e)
{
tw.WriteLine(merge);
label1.Text = merge;
tw.Flush();
}
publicString merge
{
get
{
return
text =
"fname= " + nametxt.Text + "," + "lname= " + familytxt.Text + "," + "id= " + idtxt.Text + "," + "average= " + avgtxt.Text + "#";
}
}
protectedoverridevoid OnFormClosing(FormClosingEventArgs e)
{
MessageBox.Show("Closing", "Closing", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
fs1.Close();
}
}
public
partialclassfind : Form
{
staticFileStream fs2 = newFileStream(@"c:\std.txt", FileMode.OpenOrCreate, FileAccess.Read);
StreamReader tr = newStreamReader(fs2);
String regmatch;
Boolean found;
string text;
public find()
{
InitializeComponent();
}
privatevoid find_Load(object senarrayder, EventArgs e)
{
fs2.Position = 0;
}
privatevoid findbtn_Click(object sender, EventArgs e)
{
if (findtxt.Text.Length < 6)
MessageBox.Show("ID must be at least 6 number!");
else
{
regmatch = "";
regmatch = findtxt.Text;
findtxt.Text = "";
fs2.Position = 0;
text = (tr.ReadLine());
while ((text = tr.ReadLine()) != null)
{
// findlbl.Text = "w";
text =
"";
text = (tr.ReadLine());
if (text.Contains(regmatch))
{
found = true;
listBox1.Items.Add(text);
}
}
if (found == false)
MessageBox.Show("not found");
}
}
protectedoverridevoid OnFormClosing(FormClosingEventArgs e)
{
MessageBox.Show("Closing", "Closing", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
fs2.Close();
}
}
public
partialclassMain : Form
{
public Main()
{
InitializeComponent();
}
privatevoid ToolStripMenuItem_Click(object sender, EventArgs e)
{
add add;
add = newadd();
add.Show();
add.SetDesktopLocation(50, 50);
add.Activate();
//this.Close();
}
privatevoid ToolStripMenuItem1_Click(object sender, EventArgs e)
{ find fnd;
fnd= newfind();
fnd.Show();
fnd.SetDesktopLocation(50, 50);
fnd.Activate();
}
}
با تغييراتي كه دادم، وقتي فرم ها رو باز ميكنم بار اول مشكلي نداره و هر دوشو باز ميكنه. ولي اگه براي بار دوم بخوام از منو يكيشو باز كنم ارور(stream was not readable(writable رو ميده
نقل قول: The type initializer for '... ' threw an exception
به نظر میرسه مشکل مربوط میشه به نوع کار با FileStream.
قاعده کلی استفاده از این کلاس به این شکل هست که اگر شما کدتون رو به این شکل تغییر بدین ، درصورتی که مشکل از FileStream باشه حتماً حل خواهد شد.
using System.IO;
FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.Open);
try
{
// read from file or write to file
}
finally
{
fileStream.Close();
}
نقل قول: The type initializer for '... ' threw an exception
راستش ارور رو ازاين خط مي گيره
StreamReader tr = newStreamReader(fs2);
نقل قول: The type initializer for '... ' threw an exception
shahab_ss ، دقيقا وقتي از از ساختار try استفاده كردم مشكلم حل شد.واقعا ممنونم.