从方法返回布尔值

发布于 2024-12-10 03:46:30 字数 1685 浏览 0 评论 0原文

我正在程序中创建一个保存选项,将更改保存到文件中。我正在使用此代码保存并获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我家小可爱 2024-12-17 03:46:30

StockHandler 为空。

如果 StockHandler 不是 static 类,则需要先创建它的实例,然后才能调用它的方法:

var handler = new StockHandler();
SaveFileCheck = handler.SaveChangesToFile();

或者,如果 StockHandler 是一个成员变量:

StockHandler = new // something

StockHandler is null.

If StockHandler is not a static class, you need to create an instance of it before you can call methods on it:

var handler = new StockHandler();
SaveFileCheck = handler.SaveChangesToFile();

Or, if StockHandler is a member variable:

StockHandler = new // something
念三年u 2024-12-17 03:46:30

您还没有显示 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.

那请放手 2024-12-17 03:46:30

可能是 StockHandler 为 null,或者 SaveChangesToFile 方法中的某些内容为 null 或无效。

编辑

请参阅此处:

private StockHelper StockHandler;
StockHandler.SaveChangesToFile(); // = bang :(

您需要初始化StockHelper实例:

private StockHelper StockHandler = new StockHelper();
StockHandler.SaveChangesToFile(); // = okay :)

我假设此代码无法编译,这可能意味着StockHandler为空。否则,错误可能会指向 SaveChangesToFile 方法。

其次,您要么需要在 SaveChangesToFile() 方法中吞掉异常(不建议),要么需要删除 return 语句并引发异常。如果您确实决定抛出异常,那么它绝对不应该是ArgumentException,因为它与提供给方法的参数(或缺少参数)无关。

It could be that StockHandler is null, or something in the SaveChangesToFile method is null or invalid.

EDIT

See here:

private StockHelper StockHandler;
StockHandler.SaveChangesToFile(); // = bang :(

You need to initialize the StockHelper instance:

private StockHelper StockHandler = new StockHelper();
StockHandler.SaveChangesToFile(); // = okay :)

I'm assuming that this code doesn't compile, which probably means that StockHandler is null. Otherwise, the error would likely be pointing to the SaveChangesToFile 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 an ArgumentException, as it has nothing to do with arguments supplied to the method (or lack thereof).

止于盛夏 2024-12-17 03:46:30

什么是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();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文