Click here to Skip to main content
16,004,782 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to write code for multiple client server and it is working . But I want to display data conversation on form textbox, for that I have following method in form1.cs which if calling from same class this runs but when calling from another class handleClient.cs this doesnt works and giving "object reference not set to an instance : null reference exception" error but when I putted breakpoint there it shows data in loop but break just after the loop and doesn't displays data.
The error is at the bold line in "AppendTxtdata" method.

here is the code for better understanding,

form1.cs.

C#
public void AppendTxtdata(string val)
{
try
{
if (InvokeRequired)
{
this.Invoke(new Action<string>(AppendTxtdata), new object[] { val });
return;
}
txtdata.Text += val;
}
catch { }
}


handleClient.cs

C#
public void GetData() // to run server //
{
Form1 f = new Form1();

try
{
NetworkStream networkStream = clientSocket.GetStream();


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

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

f.AppendTxtdata("The Data from Client ( " + clNo + " ) Recieved..." + Environment.NewLine);

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

f.AppendTxtdata(Environment.NewLine);

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

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

    string res = System.Text.Encoding.ASCII.GetString(temp);

}
Posted
Updated 9-Jul-15 0:41am
v3
Comments
Philippe Mori 9-Jul-15 20:59pm    
As already mentionned, if txtdata is null, then you have to do a search (or find reference) on it and see where it is set and also that it is initialized. Thus put a breakpoint on each line that set or modify it. If the breakpoint it hit before the crash, then ensure that it is set to something not null. If the debugger don't stop, then it was probably never set.

1 solution

Start by running your app in the debugger, and when the exception occurs, it will break execution at the line that showed the problem.
Then use the debugger to examine all the variables in the line, and find out which one contains the null value. You can then use the stack trace and the rest of your code to find out why it is null.

We can't do that for you: we can't run your code under the exact same conditions as you do.
So start looking and find out exactly which line, and which variable. When you know that, it should become clearer why it's null. But until you have that info, it's just guess work.
 
Share this answer
 
Comments
Member 11543226 9-Jul-15 6:40am    
see my edited question
[no name] 9-Jul-15 6:53am    
See the unchanged answer.
OriginalGriff 9-Jul-15 6:55am    
That still tells us nothing.

Use the debugger. Find how it got there, find what is null.
Put a breakpoint at the start of the method and follow it through.
Until then? We can't help.
Member 11543226 9-Jul-15 7:29am    
I said the error is in the line "txtdata.Text += myval". myval getting but txtdata shows null.
OriginalGriff 9-Jul-15 7:52am    
Yes? I guessed that.
Now why is it null? That's up to the rest of your code - which you don't show - so start looking for wherever you put a value into it. Then work out why that isn;t being executed.

Seriously, we can't do that for you - we can't run your app and that's the only way to find out what actually happens when you run it. So stop feeding us dribs and drabs of information, and start looking at your code! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900