简体   繁体   中英

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." error. But I had instanced the fm object in FileTransferManager fm = new FileTransferManager(); , didn't I?

SOLVED: Appears it was missing fm.XmppClient = xmppClient;

But what about your tb_file object.

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. It is possiby expecting something more from the user (something like, init,configure).

You will need to set breakpoint inside the FileTransferManager and then debug. No other choice.

Break point at the line and hover to see the value fm . Or tb_file.Text might be the one.

Other Check - Before calling 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 ?

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.

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, ""); call snd then in the catch you will be able to see the stack trace which should tell you where the exception is orginating.

It could be being generated from inside you FileTransferManager class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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