You must Sign In to post a response.
  • Category: .NET

    Object reference not set to the instance of object in network stream object.

    I have form 2 class to handle connected clients, and i have datagridview in that i added one button column and now i have to do is what , send data to connected client without clients request, the data which need to send to client is in another method other than dgv event.



    public class Form1()
    {
    NetworkStream networkStream;

    public void startClient(TcpClient inClientSocket, string clineNo) /// for handling clients///
    {
    this.clientSocket = inClientSocket;
    inClientSocket.NoDelay = true;
    this.clNo = clineNo;
    new Thread(GetData).Start();
    }

    public void GetData() // to run server //
    {
    networkStream = clientSocket.GetStream();

    for (int p = 0; p >= 0; p++)
    {
    b = new byte[5000];

    int k = networkStream.Read(b, 0, b.Length);

    for (int i = 0; i < k; i++)
    {
    Convert.ToChar(b[i]);
    }

    var j = b.Length - 1;
    while (b[j] == 0)
    {
    --j;
    }

    var temp = new byte[j + 1];
    Array.Copy(b, temp, j + 1);

    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
    if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
    {
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
    if (Convert.ToBoolean(row.Cells[SerialNo.Name].Value) == true)
    {
    for (int i = 0; i <= row.Cells.Count - 1; i++)
    {
    row.Cells[i].Style.BackColor = Color.LightSalmon;
    }
    }
    row.Cells[SerialNo.Name].Value = false;
    }
    isButtonClicked = true;
    ans = MessageBox.Show("Are you sure to send indent?", "Save & Print", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
    if (ans == DialogResult.Yes)
    {
    byte[] info = Encoding.ASCII.GetBytes(data);
    networkStream.Write(info, 0, info.Length);
    hash = Encoding.ASCII.GetBytes("#");
    networkStream.Write(hash, 0, hash.Length);
    }
    }
    }
    }


    in the above code i am declaring networkstream object globaly then assign values in "GetData" method and after asigning values it used in dgv event . but shows above error and object gets null value.
  • #763467
    can you elaborate your problem ? so that we can help you better in order to resolve your issue
    pl. post your code for better understanding

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #763469
    1. You declared the object. but not created the instance of that object.
    2. You are trying to assign the values of that object. So that you can getting this issue.

    Can you check whether you created the instance of objects.

    Ex.

    Class1 class1 = new Class1();
    class1.Name = "";

    The above one is correct

    The following may be your issue

    Class1 class1;
    if(A=B)
    {
    class1 = new Class1();
    }

    string aaa = class1.Name;

    In the above case. If the A=B is false. You will get issue. Can you check your code, whether you have this kind of issues.

    By Nathan
    Direction is important than speed

  • #763474
    Hello Priyanka,

    You know why this type of error occurs because when the object you have created and it contains null and you are trying to pass null value to perform some tasks which needs some value except null value. That's why this error occurs.

    So i am suggesting you to put break point in your code where you are trying to pass your data to client. Than only you will know while you are transferring data to client at that time the object you have created which contains some values or null value.

    Hope you understand. Let me know if you still facing an issues.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #763475
    I am doing it by putting break point , it gets clientSocket object value null when control outs from "getdata" method thats why i declared both objects globaly

  • #763477
    Hello Priyanka,

    It seems you have declared objects in same class only so it's value should be there when you are checking the checkbox. Check the value when you are changing the value of checkbox and then go step by step using breakpoint and check the values.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #763478
    I am doing it by putting break point , it gets clientSocket object value null when control outs from "getdata" method thats why i declared both objects globaly

  • #763496
    Hi,

    As per error message when you are trying to access null value with some instance or assign null value to object in that cases you get this type of error. I suggested you to please check the not null condition before access it.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


Sign In to post your comments