0
votes

I have a problem with using a method from form2 to form1. The error is "Object reference not set to an instance of an object." and I can't figure out what I'm doing wrong. I'm still beginner in form programming and I'm having a hard time.

Here is my code in form1:

    // showing form2 and pass the value of the _handle
    private void sendMessageToolStripMenuItem_Click(object sender, EventArgs e)
    {
        foreach (ListViewItem item in listView1.SelectedItems)
        {
            int _handle = (int)item.Tag;
            sf = new SendForm(_handle);
            sf.Show();
        } 
    }

    // sending message using socket
    public void sendT(int _handle, string msg)
    {
        byte[] sdata = Encoding.ASCII.GetBytes(msg);
        serverSocket[_handle].Send(sdata, 0, sdata.Length, 0);
    }

and here is my form2 code:

    Main m = new Main();
    int handle;

    public SendForm(int handle)
    {
        InitializeComponent();
        this.handle = handle;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        m.sendT(handle, "msgbox||test message||warning");
    }

I can't figure out how to fix this.

3
Which line throws the error? That error means that something has a null value at runtime and your code assumes that it would have a valid value. - David
I think you dont set handle any value - apomene
SendForm needs some knowledge of your main form, at the moment, it doesnt. - Sayse

3 Answers

0
votes

what I can see is you are attempting to execute method sendT on new instance of Main so probably you need to revise like this

ode in form1:

// showing form2 and pass the value of the _handle
private void sendMessageToolStripMenuItem_Click(object sender, EventArgs e)
{
    foreach (ListViewItem item in listView1.SelectedItems)
    {
        int _handle = (int)item.Tag;
        sf = new SendForm(_handle, this);
        sf.Show();
    } 
}

// sending message using socket
public void sendT(int _handle, string msg)
{
    byte[] sdata = Encoding.ASCII.GetBytes(msg);
    serverSocket[_handle].Send(sdata, 0, sdata.Length, 0);
}

form2 code:

Main m;
int handle;

public SendForm(int handle, Main mainForm)
{
    InitializeComponent();
    this.handle = handle;
    this.m=mainForm;
}

private void button1_Click(object sender, EventArgs e)
{
    m.sendT(handle, "msgbox||test message||warning");
}

this will solve the problem what i can see

0
votes

serverSocket does not seem to initialized. IMHO, I would recommend using a separate class to perform operations that are common to screens in your application.

I do not see a real need of interaction between the forms given the code sample so there is no need to be sending out form instances.

0
votes

So here is the issue: Main m = new Main(); This m, is just a new instance of Main class and has nothing to do with the first form that had displayed the SendForm instances. later you are calling sendT method of this instance that probably is using some thing that is still null. fix thie issue and I think it would work fine.

private Main m;
private int handle;

public SendForm(Main mform, int handle)
{
    InitializeComponent();
    m = mform;
    handle = handle;
}

private void button1_Click(object sender, EventArgs e)
{
    _form.sendT(handle, "msgbox||test message||warning");
}