473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Passing object with null member

..Net 2.0

Hi, I'm getting a nullreferenceexception when calling a function from a
worker thread:

private void CallbackProc(string response, Exception ex)
{
if (this.InvokeRequired)
{
this.Invoke(new MyCallback(CallbackProc), new object[] {
response, ex });
}
else
{
// do stuff here on the ui thread
}
}

It is true that ex is indeed null in some calls of CallbackProc, but
not always. The exception is thrown whenever ex is indeed null.
However, the documentation doesn't say that I can't pass null in an
Invoke (indeed it seems to say the opposite). So, I tried it out with a
fresh project but this time from one thread. (I.e. no worker thread.)
Ok, it was a hack because I replaced the "this.Invokerequired" with a
flag to ensure it only looped once. Anyway, the point is that it
worked: Invoke was happy to have a null argument in the object array.

My final test was to enclose the Invoke in a try catch. Surprisingly,
although the exception is still thrown, the Invoke call is still made
and works exactly as I want it to! Weird!

I could leave it with the try catch but that's horrible. So, can
someone point me in the right direction please?

TIA

Nov 30 '06 #1
7 1758
ne**@mail.adsl4less.com wrote:
Hi, I'm getting a nullreferenceexception when calling a function from a
worker thread:

private void CallbackProc(string response, Exception ex)
{
if (this.InvokeRequired)
{
this.Invoke(new MyCallback(CallbackProc), new object[] {
response, ex });
}
else
{
// do stuff here on the ui thread
}
}
This should be valid. Is it the Invoke that's throwing the exception,
or is it simply relaying an exception thrown by the "do stuff here on
the ui thread" code?

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.
Nov 30 '06 #2
Jon Shemitz wrote:
>
This should be valid. Is it the Invoke that's throwing the exception,
or is it simply relaying an exception thrown by the "do stuff here on
the ui thread" code?
It is the Invoke that's complaining, and in particular the fact that ex
is null. If I force populate ex with a new Exception object (the
problem, of course, occurs whatever object type I choose) just before
the Invoke, it works without complaining. Very odd indeed.

Nov 30 '06 #3
<ne**@mail.adsl4less.comwrote:
This should be valid. Is it the Invoke that's throwing the exception,
or is it simply relaying an exception thrown by the "do stuff here on
the ui thread" code?

It is the Invoke that's complaining, and in particular the fact that ex
is null. If I force populate ex with a new Exception object (the
problem, of course, occurs whatever object type I choose) just before
the Invoke, it works without complaining. Very odd indeed.
Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 30 '06 #4
ne**@mail.adsl4less.com wrote:
This should be valid. Is it the Invoke that's throwing the exception,
or is it simply relaying an exception thrown by the "do stuff here on
the ui thread" code?

It is the Invoke that's complaining, and in particular the fact that ex
is null. If I force populate ex with a new Exception object (the
problem, of course, occurs whatever object type I choose) just before
the Invoke, it works without complaining. Very odd indeed.
Have you tried commenting out the "do stuff here on the ui thread"
code? The code below works just fine, for me. If I uncomment the
`throw new NullReferenceException()` line, I do get a
NullReferenceException on the Invoke.

using System;
using System.Windows.Forms;
using System.Threading;

namespace InvokeIssue
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Click(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(delegate
{
CallbackProc("In thread", null);
});
}

delegate void MyCallback(string response, Exception ex);

private void CallbackProc(string response, Exception ex)
{
if (this.InvokeRequired)
{
this.Invoke(new MyCallback(CallbackProc), new object[]
{
response, ex
});
}
else
{
// do stuff here on the ui thread
//throw new NullReferenceException();
MessageBox.Show(
String.Format("response = {0}\nex is {1}null",
response, ex == null ? "" : "not "),
"CallbackProc");
}
}
}
}

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.
Nov 30 '06 #5

ne**@mail.adsl4less.com wrote:
Jon Shemitz wrote:

This should be valid. Is it the Invoke that's throwing the exception,
or is it simply relaying an exception thrown by the "do stuff here on
the ui thread" code?

It is the Invoke that's complaining, and in particular the fact that ex
is null. If I force populate ex with a new Exception object (the
problem, of course, occurs whatever object type I choose) just before
the Invoke, it works without complaining. Very odd indeed.
Don't forget that if your callback method fails when you pass it an ex
== null, then the final exception you get will be from the Invoke, with
the true exception that caused the problem as its InnerException.

I've run across this many times. I look at the exception and think,
"Oh, I've screwed up the Invoke," when in fact I haven't: the error is
in the code that was Invoked, and it's just the way that exceptions are
handled across an invocation that makes it look otherwise.

Nov 30 '06 #6
Bruce Wood wrote:
>
Don't forget that if your callback method fails when you pass it an ex
== null, then the final exception you get will be from the Invoke, with
the true exception that caused the problem as its InnerException.

I've run across this many times. I look at the exception and think,
"Oh, I've screwed up the Invoke," when in fact I haven't: the error is
in the code that was Invoked, and it's just the way that exceptions are
handled across an invocation that makes it look otherwise.
Bingo - problem solved! InnerException was null, but the Invoke was
indeed complaining about an a null exception in the _else_ statement.
(The debugger doesn't help because I can't "step into" the invoke even
with F11 - it's always the Invoke that throws the exception in the IDE.
Maybe I'm not using it right?) I pared the code down until I had the
bare essentials throwing the exception. My schoolboy mistake was then
obvious. :) For anyone else following this, here's the bug. Thanks to
Jon, Jon and Bruce for your help.

using System;
using System.Windows.Forms;
using System.Threading;

namespace WindowsApp
{
public delegate void MyCallback(Exception ex);

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Click(object sender, EventArgs e)
{
Worker worker = new Worker(new MyCallback(CallbackProc));
Thread thread = new Thread(new
ThreadStart(worker.FireItUp));
thread.Start();
}

public void CallbackProc(Exception ex)
{
if (this.InvokeRequired)
{
this.Invoke(new MyCallback(CallbackProc), new object[]
{ ex });
}
else
{
// The following line is the bug as ex can be null.
D'oh!
Console.WriteLine(ex.Message);
}
}
}

public class Worker
{
public MyCallback _callback;

public Worker(MyCallback callback)
{
_callback = callback;
}

public void FireItUp()
{
_callback(null);
}
}
}

Dec 4 '06 #7
Bruce Wood wrote:
>
Don't forget that if your callback method fails when you pass it an ex
== null, then the final exception you get will be from the Invoke, with
the true exception that caused the problem as its InnerException.

I've run across this many times. I look at the exception and think,
"Oh, I've screwed up the Invoke," when in fact I haven't: the error is
in the code that was Invoked, and it's just the way that exceptions are
handled across an invocation that makes it look otherwise.
Bingo - problem solved! InnerException was null, but the Invoke was
indeed complaining about an a null exception in the _else_ statement.
(The debugger doesn't help because I can't "step into" the invoke even
with F11 - it's always the Invoke that throws the exception in the IDE.
Maybe I'm not using it right?) I pared the code down until I had the
bare essentials throwing the exception. My schoolboy mistake was then
obvious. :) For anyone else following this, here's the bug. Thanks to
Jon, Jon and Bruce for your help.

using System;
using System.Windows.Forms;
using System.Threading;

namespace WindowsApp
{
public delegate void MyCallback(Exception ex);

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Click(object sender, EventArgs e)
{
Worker worker = new Worker(new MyCallback(CallbackProc));
Thread thread = new Thread(new
ThreadStart(worker.FireItUp));
thread.Start();
}

public void CallbackProc(Exception ex)
{
if (this.InvokeRequired)
{
this.Invoke(new MyCallback(CallbackProc), new object[]
{ ex });
}
else
{
// The following line is the bug as ex can be null.
D'oh!
Console.WriteLine(ex.Message);
}
}
}

public class Worker
{
public MyCallback _callback;

public Worker(MyCallback callback)
{
_callback = callback;
}

public void FireItUp()
{
_callback(null);
}
}
}

Dec 4 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Method Man | last post by:
I understand that arrays and structs can't be passed by value into and out of functions since they can be arbitrarily big. My question is: Why are types allowed to be passed by value? Couldn't my...
7
by: Steven T. Hatton | last post by:
I am trying to convert some basic OpenGL code to an OO form. This is the C version of the program: http://www.opengl.org/resources/code/basics/redbook/double.c You can see what my current...
17
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
11
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI...
32
by: paul | last post by:
HI! I keep on getting this error and I have tried different things but I am not sure how to send the expiring date. The error that I am getting in Firefox 1.5 is "Error: expires.toGMTString is...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
3
by: Joshepmichel | last post by:
Please to help me to following problem I want to do this 1. create Table Name MEMBER on the Database Name "mytestdb", 2. Add the Values to the Table through the Key board Inputs during running...
5
by: aelred | last post by:
I have a web page where a member can open up a chat window (child window) with another member. - From there the member can also navigate to other web pages. - From other pages in the site, they...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.