By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,505 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,505 IT Pros & Developers. It's quick & easy.

simple question about ref with object?? PLEASE HELP

P: n/a
ok i have a datagridview in my app and i have a function defined like
so:
public void FillColumns(ref DataGridView theDGV)
when i call it ... like this :
FillColumns(ref dataGridView2);

i get : Object reference not set to an instance of an object.

i dont get it , is'nt my datagridview defined in form.designer.cs????
-private System.Windows.Forms.DataGridView dataGridView2;

PLEASE HELP ME OUT.. i need this in a hurry!

Thanks
Gideon

Nov 22 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
It will be initialised in InitialiseComponent so you need to ensure that is
called first. It is set to call in the constructor by default so it you do
this in the ctor then you'll have to put it after that. If you do already,
then the exception must occur inside the method

Ciaran O'Donnell
http://wannabedeveloper.spaces.live.com

"giddy" wrote:
ok i have a datagridview in my app and i have a function defined like
so:
public void FillColumns(ref DataGridView theDGV)
when i call it ... like this :
FillColumns(ref dataGridView2);

i get : Object reference not set to an instance of an object.

i dont get it , is'nt my datagridview defined in form.designer.cs????
-private System.Windows.Forms.DataGridView dataGridView2;

PLEASE HELP ME OUT.. i need this in a hurry!

Thanks
Gideon

Nov 22 '06 #2

P: n/a
um.. .. yea i thought about that , i call the method during a button
click event

private void button1_Click(object sender, EventArgs e)
{
FillColumns(ref dataGridView2);
//CreateDocumentPropertyTable();
}
so yes, the exception (NullRefrenceException) occurs in the function.
So what do i do!!!!!!

and why does it say Object reference not set to an INSTANCE OF AN
OBJECT!
On Nov 22, 10:22 pm, Ciaran O''Donnell
<CiaranODonn...@discussions.microsoft.comwrote:
It will be initialised in InitialiseComponent so you need to ensure that is
called first. It is set to call in the constructor by default so it you do
this in the ctor then you'll have to put it after that. If you do already,
then the exception must occur inside the method

Ciaran O'Donnellhttp://wannabedeveloper.spaces.live.com

"giddy" wrote:
ok i have a datagridview in my app and i have a function defined like
so:
public void FillColumns(ref DataGridView theDGV)
when i call it ... like this :
FillColumns(ref dataGridView2);
i get : Object reference not set to an instance of an object.
i dont get it , is'nt my datagridview defined in form.designer.cs????
-private System.Windows.Forms.DataGridView dataGridView2;
PLEASE HELP ME OUT.. i need this in a hurry!
Thanks
Gideon
Nov 22 '06 #3

P: n/a
is the dataGridView2 in the same scope as the function?

In other words, are you calling that function from a class that doesn't
know where the dgv is?

On Nov 22, 1:22 pm, "giddy" <gidisr...@gmail.comwrote:
um.. .. yea i thought about that , i call the method during a button
click event

private void button1_Click(object sender, EventArgs e)
{
FillColumns(ref dataGridView2);
//CreateDocumentPropertyTable();
}
so yes, the exception (NullRefrenceException) occurs in the function.
So what do i do!!!!!!

and why does it say Object reference not set to an INSTANCE OF AN
OBJECT!

On Nov 22, 10:22 pm, Ciaran O''Donnell

<CiaranODonn...@discussions.microsoft.comwrote:
It will be initialised in InitialiseComponent so you need to ensure that is
called first. It is set to call in the constructor by default so it you do
this in the ctor then you'll have to put it after that. If you do already,
then the exception must occur inside the method
Ciaran O'Donnellhttp://wannabedeveloper.spaces.live.com
"giddy" wrote:
ok i have a datagridview in my app and i have a function defined like
so:
public void FillColumns(ref DataGridView theDGV)
when i call it ... like this :
FillColumns(ref dataGridView2);
i get : Object reference not set to an instance of an object.
i dont get it , is'nt my datagridview defined in form.designer.cs????
-private System.Windows.Forms.DataGridView dataGridView2;
PLEASE HELP ME OUT.. i need this in a hurry!
Thanks
Gideon
Nov 22 '06 #4

P: n/a
A DataGridView is a reference type. There is no need to pass it by ref.

--
HTH,

Kevin Spencer
Microsoft MVP
Ministry of Software Development
http://unclechutney.blogspot.com

Never trust a dunderhead with a blunderbuss.
"giddy" <gi*******@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
ok i have a datagridview in my app and i have a function defined like
so:
public void FillColumns(ref DataGridView theDGV)
when i call it ... like this :
FillColumns(ref dataGridView2);

i get : Object reference not set to an instance of an object.

i dont get it , is'nt my datagridview defined in form.designer.cs????
-private System.Windows.Forms.DataGridView dataGridView2;

PLEASE HELP ME OUT.. i need this in a hurry!

Thanks
Gideon

Nov 22 '06 #5

P: n/a
"giddy" <gi*******@gmail.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
um.. .. yea i thought about that , i call the method during a button
click event

private void button1_Click(object sender, EventArgs e)
{
FillColumns(ref dataGridView2);
//CreateDocumentPropertyTable();
}
so yes, the exception (NullRefrenceException) occurs in the function.
So what do i do!!!!!!

and why does it say Object reference not set to an INSTANCE OF AN
OBJECT!
First of all, calm down. Whatever emergency you think you have, if you are
posting here it is obviously not serious enough to justify your tone.
Conversely, if you think your panic is justified, then forget about this
newsgroup and go get some paid support straight from Microsoft.

Anyway, as far as your question goes...

You haven't given enough information for anyone to even know what line of
code is generating the null reference exception. What line of code does the
debugger say has the null reference? What variable in that line of code is
the one that is null? And why is it that you expect that variable to NOT be
null?

Until you answer those three questions, it is assured you cannot get any
help here. Even with the answers, it could take some time, but I guarantee
you that without that information, nothing can be done to help you.

I will also submit my agreement with Kevin's point, which is that you
probably don't need for the parameter to FillColumns() to be a "ref"
parameter. I don't see why that would be causing the null reference
exception (unless, of course, the FillColumns() method at some point sets
that parameter to null, in which case that's your problem right there), but
it is not a good idea to pass parameters by reference unless you actually
need to do so.

Pete
Nov 22 '06 #6

P: n/a
Kevin Spencer <sp**@uce.govwrote:
A DataGridView is a reference type. There is no need to pass it by ref.
You make it sound like it will behave the same with and without ref -
and that's completely untrue. It's the difference between passing the
reference by reference and passing the reference by value.

Here's a sample method which certainly won't work if you take out the
"ref" parts:

static void SplitName (string fullName,
ref string firstName,
ref string lastName)
{
string[] bits = fullName.Spliut();
firstName = bits[0];
lastName = bits[1];
}

Now, often people *do* use ref when they don't need to, but without
seeing the OP's method, we can't tell whether that's the case or not.

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information.

--
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 22 '06 #7

P: n/a
Hi Jon,

Now, don't put words in my mouth. I pointed out that it wasn't necessary to
pass the object by reference because I don't know much else about the
problem, but one important aspect to problem-solving is to simplify the
problem. In this case, passing by reference was unnecessary, and, depending
on other factors, *may* have contributed to the problem in some indirect
way. By eliminating that one step, the problem is at least simplified.

If you will re-read my post, you will see that the remark about not needing
to pass the DataGridView by reference was the only remark I made.

--
HTH,

Kevin Spencer
Microsoft MVP
Ministry of Software Development
http://unclechutney.blogspot.com

Never trust a dunderhead with a blunderbuss.

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Kevin Spencer <sp**@uce.govwrote:
>A DataGridView is a reference type. There is no need to pass it by ref.

You make it sound like it will behave the same with and without ref -
and that's completely untrue. It's the difference between passing the
reference by reference and passing the reference by value.

Here's a sample method which certainly won't work if you take out the
"ref" parts:

static void SplitName (string fullName,
ref string firstName,
ref string lastName)
{
string[] bits = fullName.Spliut();
firstName = bits[0];
lastName = bits[1];
}

Now, often people *do* use ref when they don't need to, but without
seeing the OP's method, we can't tell whether that's the case or not.

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information.

--
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 23 '06 #8

P: n/a

ok im sorry if i did'nt give enough detail and i'm really sorry if i'm
being rude by saying its urgent , i do have to to finish this project
by the end of this month! and a few more setbacks like this could
jeopardize a lot more than my job , also i cannnot! afford paid
support!
i have good programming background but i started C# just a while ago.
I made quite a few fuctions with ref, and naturally started to panic
when one of them did'nt work!

Firstly , i need to use ref because i have a 2 datagrid views that have
to be filled more or less the same way , and i could have a 3rd one
soon. Whatever is common between the dgvs goes into one function and
then obviously has to change the datagridview passed to it.

I just tried the whole thing without the ref keyword both in the
function and when i call it. and i GET THE SAME EXCEPTION.

the exception occurs inside the function on the first line :
public void FillColumns(ref DataGridView theDGV)
{
theDGV.ColumnCount =
dataSet.Tables["customers"].Columns.Count; //THIS IS THE LINE.

VS says :

NullRefrenceException was not handled
Object reference not set to an instance of an object.]
Troubleshooting tips :
use the 'new' keyword to create an object of the instance (????
check to determine if the object is null before calling the method

i have Form1 , the datagridview is ON Form1 and FillColumns() is in
Form1.cs

In Form1.Designer.cs :
the dgv is declared like this : private
System.Windows.Forms.DataGridView dataGridView2;

and its properties are set like this :
this.dataGridView2.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeig htSizeMode.AutoSize;
this.dataGridView2.Location = new System.Drawing.Point(5, 50);
this.dataGridView2.Name = "dataGridView2";

so why is it null!

thanks

Gideon

On Nov 23, 5:18 am, "Kevin Spencer" <s...@uce.govwrote:
----------------------------------------
Kevin Spencer <s...@uce.govwrote:
A DataGridView is a reference type. There is no need to pass it by ref.
Now, often people *do* use ref when they don't need to, but without
seeing the OP's method, we can't tell whether that's the case or not.
--------------------------------------

Peter Duniho wrote:
What line of code does the
debugger say has the null reference? What variable in that line of
code is
the one that is null? And why is it that you expect that variable to
NOT be
null?
--------------------------------------
cbmeeks wrote :
is the dataGridView2 in the same scope as the function?

In other words, are you calling that function from a class that doesn't
know where the dgv is?

Nov 23 '06 #9

P: n/a

hi ,

i'm really embarrased about this ,

theDGV.ColumnCount =
dataSet.Tables["customers"].Columns.Count; //THIS IS THE LINE.

after a 2 more hours of debugging i found out that ......
the DataSet was null not the dgv! =S , and this itself was a cause of
two silly mistakes

I'm really sorry to have wasted your time.

Thank you for your concern!

Gideon

On Nov 23, 9:09 am, "giddy" <gidisr...@gmail.comwrote:
ok im sorry if i did'nt give enough detail and i'm really sorry if i'm
being rude by saying its urgent , i do have to to finish this project
by the end of this month! and a few more setbacks like this could
jeopardize a lot more than my job , also i cannnot! afford paid
support!
i have good programming background but i started C# just a while ago.
I made quite a few fuctions with ref, and naturally started to panic
when one of them did'nt work!

Firstly , i need to use ref because i have a 2 datagrid views that have
to be filled more or less the same way , and i could have a 3rd one
soon. Whatever is common between the dgvs goes into one function and
then obviously has to change the datagridview passed to it.

I just tried the whole thing without the ref keyword both in the
function and when i call it. and i GET THE SAME EXCEPTION.

the exception occurs inside the function on the first line :
public void FillColumns(ref DataGridView theDGV)
{
theDGV.ColumnCount =
dataSet.Tables["customers"].Columns.Count; //THIS IS THE LINE.

VS says :

NullRefrenceException was not handled
Object reference not set to an instance of an object.]
Troubleshooting tips :
use the 'new' keyword to create an object of the instance (????
check to determine if the object is null before calling the method

i have Form1 , the datagridview is ON Form1 and FillColumns() is in
Form1.cs

In Form1.Designer.cs :
the dgv is declared like this : private
System.Windows.Forms.DataGridView dataGridView2;

and its properties are set like this :
this.dataGridView2.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeig htSizeMode.AutoSize;
this.dataGridView2.Location = new System.Drawing.Point(5, 50);
this.dataGridView2.Name = "dataGridView2";

so why is it null!

thanks

Gideon

On Nov 23, 5:18 am, "Kevin Spencer" <s...@uce.govwrote:----------------------------------------
Kevin Spencer <s...@uce.govwrote:
>A DataGridView is a reference type. There is no need to pass it by ref.
Now, often people *do* use ref when they don't need to, but without
seeing the OP's method, we can't tell whether that's the case or not.
--------------------------------------
>Peter Duniho wrote:
What line of code does thedebugger say has the null reference? What variable in that line of
code is
the one that is null? And why is it that you expect that variable to
NOT be
null? --------------------------------------
>cbmeeks wrote :is the dataGridView2 in the same scope as the function?

In other words, are you calling that function from a class that doesn't
know where the dgv is?
Nov 23 '06 #10

P: n/a
Kevin Spencer <sp**@uce.govwrote:
Now, don't put words in my mouth. I pointed out that it wasn't necessary to
pass the object by reference because I don't know much else about the
problem, but one important aspect to problem-solving is to simplify the
problem. In this case, passing by reference was unnecessary, and, depending
on other factors, *may* have contributed to the problem in some indirect
way. By eliminating that one step, the problem is at least simplified.
But we don't know whether passing by reference was unnecessary or not -
we've only seen the call, not what the method actually did. If the OP
had posted the code to FillColumns, we might well have seen that it was
unnecessary, but as he hasn't, we can't tell.
If you will re-read my post, you will see that the remark about not needing
to pass the DataGridView by reference was the only remark I made.
Indeed, and we don't know whether it was accurate or not - but I
believe it gave an inaccurate impression.

--
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 23 '06 #11

P: n/a
giddy <gi*******@gmail.comwrote:
Firstly , i need to use ref because i have a 2 datagrid views that have
to be filled more or less the same way , and i could have a 3rd one
soon. Whatever is common between the dgvs goes into one function and
then obviously has to change the datagridview passed to it.
That in itself doesn't mean you have to pass by reference.

It's very important that you understand reference types and passing by
reference. See
http://www.pobox.com/~skeet/csharp/parameters.html
I just tried the whole thing without the ref keyword both in the
function and when i call it. and i GET THE SAME EXCEPTION.

the exception occurs inside the function on the first line :
public void FillColumns(ref DataGridView theDGV)
{
theDGV.ColumnCount =
dataSet.Tables["customers"].Columns.Count; //THIS IS THE LINE.
Okay - in that case, either theDGV is null, or dataSet is null, or
dataSet.Tables["customers"] is null, or
dataSet.Tables["customers"].Columns is null.

You should be able to tell that in the debugger.
In Form1.Designer.cs :
the dgv is declared like this : private
System.Windows.Forms.DataGridView dataGridView2;

and its properties are set like this :
this.dataGridView2.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeig htSizeMode.AutoSize;
this.dataGridView2.Location = new System.Drawing.Point(5, 50);
this.dataGridView2.Name = "dataGridView2";

so why is it null!
Well, we don't know for sure that it's that that's null - see above for the options.

--
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 23 '06 #12

P: n/a
Hi Jon,
But we don't know whether passing by reference was unnecessary or not -
we've only seen the call, not what the method actually did. If the OP
had posted the code to FillColumns, we might well have seen that it was
unnecessary, but as he hasn't, we can't tell.
I'll have to admit, I made an educated guess. It was highly unlikely (IMHO),
but as I said, I made an educated guess that there was no need in this case.
Indeed, and we don't know whether it was accurate or not - but I
believe it gave an inaccurate impression.
Well, impressions are subjective, but I will certainly grant you that it at
least gave *you* an inaccurate impression, for whatever reason. Furthermore,
if it gave you an inaccurate impression, it might have given others an
inaccurate impression. :)

I have a tendancy to be concise. I often forget that human perception
involves a great deal of interpretation! While I may mean no more and no
less than what I say, I do find that I am sometimes misunderstood, simply
because I didn't clarify my remarks more. If only I had more time!

--
Best Wishes,

Kevin Spencer
Microsoft MVP
Ministry of Software Development
http://unclechutney.blogspot.com

Never trust a dunderhead with a blunderbuss.
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Kevin Spencer <sp**@uce.govwrote:
>Now, don't put words in my mouth. I pointed out that it wasn't necessary
to
<snip>
Indeed, and we don't know whether it was accurate or not - but I
believe it gave an inaccurate impression.

--
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 23 '06 #13

P: n/a
Congratulations! We've all been there before. I am embarassed on a daily
basis by things that only I know I did!

--

Kevin Spencer
Microsoft MVP
Ministry of Software Development
http://unclechutney.blogspot.com

Never trust a dunderhead with a blunderbuss.
"giddy" <gi*******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>
hi ,

i'm really embarrased about this ,

theDGV.ColumnCount =
dataSet.Tables["customers"].Columns.Count; //THIS IS THE LINE.

after a 2 more hours of debugging i found out that ......
the DataSet was null not the dgv! =S , and this itself was a cause of
two silly mistakes

I'm really sorry to have wasted your time.

Thank you for your concern!

Gideon

On Nov 23, 9:09 am, "giddy" <gidisr...@gmail.comwrote:
>ok im sorry if i did'nt give enough detail and i'm really sorry if i'm
being rude by saying its urgent , i do have to to finish this project
by the end of this month! and a few more setbacks like this could
jeopardize a lot more than my job , also i cannnot! afford paid
support!
i have good programming background but i started C# just a while ago.
I made quite a few fuctions with ref, and naturally started to panic
when one of them did'nt work!

Firstly , i need to use ref because i have a 2 datagrid views that have
to be filled more or less the same way , and i could have a 3rd one
soon. Whatever is common between the dgvs goes into one function and
then obviously has to change the datagridview passed to it.

I just tried the whole thing without the ref keyword both in the
function and when i call it. and i GET THE SAME EXCEPTION.

the exception occurs inside the function on the first line :
public void FillColumns(ref DataGridView theDGV)
{
theDGV.ColumnCount =
dataSet.Tables["customers"].Columns.Count; //THIS IS THE LINE.

VS says :

NullRefrenceException was not handled
Object reference not set to an instance of an object.]
Troubleshooting tips :
use the 'new' keyword to create an object of the instance (????
check to determine if the object is null before calling the method

i have Form1 , the datagridview is ON Form1 and FillColumns() is in
Form1.cs

In Form1.Designer.cs :
the dgv is declared like this : private
System.Windows.Forms.DataGridView dataGridView2;

and its properties are set like this :
this.dataGridView2.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHei ghtSizeMode.AutoSize;
this.dataGridView2.Location = new System.Drawing.Point(5, 50);
this.dataGridView2.Name = "dataGridView2";

so why is it null!

thanks

Gideon

On Nov 23, 5:18 am, "Kevin Spencer" <s...@uce.gov>
wrote:----------------------------------------
Kevin Spencer <s...@uce.govwrote:
A DataGridView is a reference type. There is no need to pass it by
ref.
Now, often people *do* use ref when they don't need to, but without
seeing the OP's method, we can't tell whether that's the case or not.
--------------------------------------
>>Peter Duniho wrote:
What line of code does thedebugger say has the null reference? What
variable in that line of
code is
the one that is null? And why is it that you expect that variable to
NOT be
null? --------------------------------------
>>cbmeeks wrote :is the dataGridView2 in the same scope as the function?

In other words, are you calling that function from a class that doesn't
know where the dgv is?

Nov 23 '06 #14

P: n/a
Kevin Spencer <sp**@uce.govwrote:
Congratulations! We've all been there before. I am embarassed on a daily
basis by things that only I know I did!
That's one of the interesting things about having open source projects
available on the web - *anyone* can see how awful your code is. Having
recently found some clangers in my miscutil library, I'm very red in
the face.

Still, so long as we learn lessons from it... in this case, it's a
retrospective lesson that unit tests make a huge difference :)

--
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 23 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.