简体   繁体   English

C#,“对象引用未设置为对象的实例。”错误

[英]C#, “Object reference not set to an instance of an object.” error

I've got this code... 我有这个代码......

namespace YellowBox
{
    public partial class Form1 : Form
    {

        private string sid = "";

        FileTransferManager fm = new FileTransferManager();
        Jid _jid = new Jid();


        public Form1()
        {
            InitializeComponent();

            fm.OnError += fm_OnError;
            fm.OnEnd += fm_OnEnd;
            fm.OnStart += fm_OnStart;
            fm.OnProgress += fm_OnProgress;
        }

        private void btn_pickFile_Click(object sender, System.EventArgs e)
        {
            var of = new OpenFileDialog();
            if (of.ShowDialog() == DialogResult.OK)
            {
                tb_file.Text = of.FileName;

                var fi = new FileInfo(of.FileName);
                //lblSize.Text = Util.HumanReadableFileSize(fi.Length);

                btn_sendFile.Enabled = true;
            }
        }

        private void btn_sendFile_Click(object sender, System.EventArgs e)
        {

        _jid.Server = "xxx";
        _jid.User = "xxx";  /// EDIT, added the _jid values.
        _jid.Resource = "xxx";

            sid = fm.Send(_jid, tb_file.Text, ""); /// HERE IT SAYS "Object reference not set to an instance of an object." ???
            btn_sendFile.Enabled = false;
            btn_pickFile.Enabled = false;
        }

...

And when I hit the btn_sendFile it gives me a "Object reference not set to an instance of an object." 当我点击btn_sendFile时,它给了我一个“对象引用未设置为对象的实例”。 error. 错误。 But I had instanced the fm object in FileTransferManager fm = new FileTransferManager(); 但是我在FileTransferManager中实例化了fm对象fm = new FileTransferManager(); , didn't I? ,不是吗?

SOLVED: Appears it was missing fm.XmppClient = xmppClient; 已解决:似乎缺少fm.XmppClient = xmppClient;

But what about your tb_file object. 但是你的tb_file对象怎么样?

I dont see instantiation or definition of that object anywhere in your code. 我没有在代码中的任何地方看到该对象的实例化或定义。

Reading your comments, i don't think any of the parameters you are passing to "Send" is null. 阅读您的评论,我认为您传递给“发送”的任何参数都不为空。

I Would say, there is a usability issue in the "FileTransferManager" class. 我想说,“FileTransferManager”类中存在可用性问题。 It is possiby expecting something more from the user (something like, init,configure). 可能期望用户提供更多内容(例如,init,configure)。

You will need to set breakpoint inside the FileTransferManager and then debug. 您需要在FileTransferManager中设置断点,然后进行调试。 No other choice. 别无选择。

Break point at the line and hover to see the value fm . 在该行断点并悬停以查看值fm Or tb_file.Text might be the one. 或者tb_file.Text可能就是那个。

Other Check - Before calling sid = fm.Send(_jid, tb_file.Text, ""); 其他检查 - 在调用sid = fm.Send(_jid, tb_file.Text, "");之前sid = fm.Send(_jid, tb_file.Text, ""); , can you print all the parameters and verify the values. ,您可以打印所有参数并验证值。

Is it possible that the error is on tb_file.Text ? 错误是否可能在tb_file.Text上?

There's no definition of this variable anywhere in the code. 代码中的任何地方都没有定义此变量。 If this is the problem, you should also correct the assignation in the btn_pickFile_Click method. 如果这是问题,您还应该更正btn_pickFile_Click方法中的分配。

Can you debug the application and set a breakpoint on the line of code that is throwing the exception. 您可以调试应用程序并在抛出异常的代码行上设置断点。 Hover you mouse over each object on that line and it will show you which one is null. 将鼠标悬停在该行上的每个对象上,它将显示哪一个为空。 That will let you know where the problem is, after that it is a matter of figuring out why it is null. 这将让你知道问题出在哪里,之后就是弄清楚为什么它是空的。 I cant tell anything more from the code you have posted. 我不能从你发布的代码中说出更多信息。

Probably wise to stick a try..catch block around the sid = fm.Send(_jid, tb_file.Text, ""); 可能明智地在sid = fm.Send(_jid, tb_file.Text, "");周围粘贴try..catch块sid = fm.Send(_jid, tb_file.Text, ""); call snd then in the catch you will be able to see the stack trace which should tell you where the exception is orginating. 然后在catch中调用snd,您将能够看到堆栈跟踪,它应该告诉您异常的位置。

It could be being generated from inside you FileTransferManager class. 它可能是从FileTransferManager类中生成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM