473,666 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing object with null member

..Net 2.0

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

private void CallbackProc(st ring response, Exception ex)
{
if (this.InvokeReq uired)
{
this.Invoke(new MyCallback(Call backProc), 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.Invokereq uired" 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 1776
ne**@mail.adsl4 less.com wrote:
Hi, I'm getting a nullreferenceex ception when calling a function from a
worker thread:

private void CallbackProc(st ring response, Exception ex)
{
if (this.InvokeReq uired)
{
this.Invoke(new MyCallback(Call backProc), 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.adsl 4less.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.co m>
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.adsl4 less.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 NullReferenceEx ception()` line, I do get a
NullReferenceEx ception on the Invoke.

using System;
using System.Windows. Forms;
using System.Threadin g;

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

private void Form1_Click(obj ect sender, EventArgs e)
{
ThreadPool.Queu eUserWorkItem(d elegate
{
CallbackProc("I n thread", null);
});
}

delegate void MyCallback(stri ng response, Exception ex);

private void CallbackProc(st ring response, Exception ex)
{
if (this.InvokeReq uired)
{
this.Invoke(new MyCallback(Call backProc), new object[]
{
response, ex
});
}
else
{
// do stuff here on the ui thread
//throw new NullReferenceEx ception();
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.adsl4 less.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.Threadin g;

namespace WindowsApp
{
public delegate void MyCallback(Exce ption ex);

public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
}

private void Form1_Click(obj ect sender, EventArgs e)
{
Worker worker = new Worker(new MyCallback(Call backProc));
Thread thread = new Thread(new
ThreadStart(wor ker.FireItUp));
thread.Start();
}

public void CallbackProc(Ex ception ex)
{
if (this.InvokeReq uired)
{
this.Invoke(new MyCallback(Call backProc), new object[]
{ ex });
}
else
{
// The following line is the bug as ex can be null.
D'oh!
Console.WriteLi ne(ex.Message);
}
}
}

public class Worker
{
public MyCallback _callback;

public Worker(MyCallba ck 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.Threadin g;

namespace WindowsApp
{
public delegate void MyCallback(Exce ption ex);

public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
}

private void Form1_Click(obj ect sender, EventArgs e)
{
Worker worker = new Worker(new MyCallback(Call backProc));
Thread thread = new Thread(new
ThreadStart(wor ker.FireItUp));
thread.Start();
}

public void CallbackProc(Ex ception ex)
{
if (this.InvokeReq uired)
{
this.Invoke(new MyCallback(Call backProc), new object[]
{ ex });
}
else
{
// The following line is the bug as ex can be null.
D'oh!
Console.WriteLi ne(ex.Message);
}
}
}

public class Worker
{
public MyCallback _callback;

public Worker(MyCallba ck 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
1751
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 types be arbitraily big as well?
7
2487
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 effort at converting that code to an OO form looks like in the code listed below. The problem I'm running into is that the OpenGL functions that take the names of other functions as arguments don't like the way I'm passing the member functions of my...
17
2804
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 free()? #include <stdlib.h> struct stype { int *foo; };
12
2678
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
4410
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 components. The application is built around a "World" object that contains a "GUI" object that is a C++ wrapper around GLUT. What I'd like to do is pass a pointer to a member function in the World class to function in the GUI
32
5003
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 not a function" ---------------------------------------------------- I have this in a .js file and in the head section.
7
3297
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 object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
3
7696
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 the My Java Application.; Therefore I used this following codes, It Consists the seperate parts for the Raede and Member Class for the purposely I craetes. 3. I have problem to Parsing the values to SQL statement, that Consists on the Run Time...
5
10282
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 may also open up new chat windows with other members (just not the same one). - Each chat page is opened with the member name as the window name. - When I log off from the web page, I would like all the chat windows to automatically close. I...
0
8784
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8556
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7387
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4371
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2774
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1777
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.