从方法返回布尔值
我正在程序中创建一个保存选项,将更改保存到文件中。我正在使用此代码保存并获取 MessageBox
来显示该过程的结果,我在这一行收到错误“对象引用未设置为对象的实例”。
SaveFileCheck = StockHandler.SaveChangesToFile();
这是我的代码
private void Save_Click(object sender, EventArgs e)
{
bool SaveFileCheck = false;
var result = MessageBox.Show("Are you sure you want to Save the changes ?", "My Application",
MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
if (result == DialogResult.Yes)
{
SaveFileCheck = StockHandler.SaveChangesToFile();
if (SaveFileCheck)
{
MessageBox.Show("The process was a success");
}
else
{
MessageBox.Show("The process failed please make sure that the file is not been used and try again");
}
//Save the file back
}
}
}
}
public bool SaveChangesToFile()
{
try
{
if (FileName != null)
{
using (StreamWriter Write = new StreamWriter(FileName, false))
{
foreach (Stock s in FileStockList)
{
Write.Write(s.ToString() + "\r\n");
}
}
}
else {
return false;
}
}
catch(IOException ex)
{
return false;
throw new ArgumentException("something went wrong an error" + ex + "is been cought");
}
return true;
}
I am making a save option in my program that saves the changes to a file. I am using this code to save and get a MessageBox
to show the result of the process I am getting an error on this line "Object reference not set to an instance of an object."
SaveFileCheck = StockHandler.SaveChangesToFile();
this is my code
private void Save_Click(object sender, EventArgs e)
{
bool SaveFileCheck = false;
var result = MessageBox.Show("Are you sure you want to Save the changes ?", "My Application",
MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
if (result == DialogResult.Yes)
{
SaveFileCheck = StockHandler.SaveChangesToFile();
if (SaveFileCheck)
{
MessageBox.Show("The process was a success");
}
else
{
MessageBox.Show("The process failed please make sure that the file is not been used and try again");
}
//Save the file back
}
}
}
}
public bool SaveChangesToFile()
{
try
{
if (FileName != null)
{
using (StreamWriter Write = new StreamWriter(FileName, false))
{
foreach (Stock s in FileStockList)
{
Write.Write(s.ToString() + "\r\n");
}
}
}
else {
return false;
}
}
catch(IOException ex)
{
return false;
throw new ArgumentException("something went wrong an error" + ex + "is been cought");
}
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
StockHandler
为空。如果
StockHandler
不是static
类,则需要先创建它的实例,然后才能调用它的方法:或者,如果
StockHandler
是一个成员变量:StockHandler
is null.If
StockHandler
is not astatic
class, you need to create an instance of it before you can call methods on it:Or, if
StockHandler
is a member variable:您还没有显示
StockHandler
是什么,或者您从哪里获取它 - 但它看起来像是 null。您需要它作为对有效对象的引用。仅从您提供的代码中我们无法得知更多内容。请注意,这与返回
bool
的方法无关。You haven't shown what
StockHandler
is, or where you're getting it from - but it looks like it's null. You'll need it to be a reference to a valid object. There's not a lot more we can say just from the code you've given.Note that this has nothing to do with a method returning a
bool
.可能是
StockHandler
为 null,或者SaveChangesToFile
方法中的某些内容为 null 或无效。编辑
请参阅此处:
您需要初始化
StockHelper
实例:我假设此代码无法编译,这可能意味着
StockHandler
为空。否则,错误可能会指向SaveChangesToFile
方法。其次,您要么需要在 SaveChangesToFile() 方法中吞掉异常(不建议),要么需要删除 return 语句并引发异常。如果您确实决定抛出异常,那么它绝对不应该是ArgumentException,因为它与提供给方法的参数(或缺少参数)无关。
It could be that
StockHandler
is null, or something in theSaveChangesToFile
method is null or invalid.EDIT
See here:
You need to initialize the
StockHelper
instance:I'm assuming that this code doesn't compile, which probably means that
StockHandler
is null. Otherwise, the error would likely be pointing to theSaveChangesToFile
method.Secondly, you either need to swallow exceptions in the
SaveChangesToFile()
method (not advisable), or you need to remove the return statement and throw the exception. If you do decide to throw an exception, it should definitely not be anArgumentException
, as it has nothing to do with arguments supplied to the method (or lack thereof).什么是stockhandler——您的SaveChangesToFile方法是一个实例方法——那么您是否已将变量“StockHandler”实例化为包含方法SaveChangesToFile()的任何类的实例?
What is stockhandler -- your SaveChangesToFile method is an instance method -- so have you instantiated a variable 'StockHandler' to an instance of whatever class contains the method SaveChangesToFile();