简体   繁体   中英

Nullreference Exception code -1 when using object in another class

I apologize if my title-problem text is not correct... My problem: The first class posted under works fine and I can send data to a control processor through Crestron.ActiveCNX. The line shown containing "Hello world" sends data to the control processor, and here it works fine. In this same class I start this ActiveCNX communication and there is also event handling(Not shown here). But I have a lot of code and need to send data trough this same ActiveCNX connection from another class. This class is shown with the necessary code to explain my problem. When I try to send data from this second class i get the Nullreference exception code -1 error.

What am I doing wrong?

Sorry if this is a stupid question. I am an all around programmer, this time I needed to use the C# language.

Thanks for any help! Erik.

using Crestron.ActiveCNX; 
namespace Server
{
public partial class Form1 : Form
{
    public ActiveCNX cnx;

    public Form1()
    {
        InitializeComponent();   
    }

    public void Form1_Load(object sender, EventArgs e)
    {
        //Initialize ActiveCNX
        cnx = new ActiveCNX();                                                          
        cnx.Connect("10.0.0.32", 3);

        MethodInvoker simpleDelegate = new MethodInvoker(AsynchronousSocketListener.StartListening);
        simpleDelegate.BeginInvoke(null, null);
    }   //Form1_Load

    private void button2_Click(object sender, EventArgs e)
    {
        cnx.SendSerial(1, 1, "Hello World!");  //This works fine from this location.(sending text to a control processor).
    }


}   //Class : Form1
}   //Namespace

//////////////////////////////////////////////////////////////////////////////////////////////

namespace Server
{
class SendCNXUpdate
{
    public void Update()
    {
        Form1 form1 = new Form1();
        //Here it usually is code to receive data from another server, parsing it and then this class is supposed to send the parsed data to the processor.
        form1.cnx.SendSerial(1, 1, "Hello World!");  //I am using the exact same code as in the form1 class, but get the nullexception error..
    }
} 
}

You initialize cnx in FormLoad method, but you need to do that in constructor.

public Form1()
    {
        InitializeComponent();   
        cnx = new ActiveCNX();                                                          
        cnx.Connect("10.0.0.32", 3);

    }

In Update() method you do not show the form, so Form_Load() method is not called. You only ever initialise cnx in Form_Load() . You should initialise it in Update() as well:

public void Update()
{
    Form1 form1 = new Form1();
    form1.cnx = new ActiveCNX();                                                          
    form1.cnx.Connect("10.0.0.32", 3);
    form1.cnx.SendSerial(1, 1, "Hello World!");
}

Better still, you can extract the all the logic around CNX it into a separate class to decouple it from Form1 .

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