.NET Forum / Languages / C# / November 2006
simple question about ref with object?? PLEASE HELP
|
|
Thread rating:  |
giddy - 22 Nov 2006 16:45 GMT 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
Ciaran O''Donnell - 22 Nov 2006 17:22 GMT 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
> ok i have a datagridview in my app and i have a function defined like > so: [quoted text clipped - 12 lines] > Thanks > Gideon giddy - 22 Nov 2006 18:22 GMT 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.com> wrote:
> 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 [quoted text clipped - 19 lines] > > Thanks > > Gideon cbmeeks - 22 Nov 2006 18:46 GMT 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?
> um.. .. yea i thought about that , i call the method during a button > click event [quoted text clipped - 36 lines] > > > Thanks > > > Gideon Peter Duniho - 22 Nov 2006 19:48 GMT > um.. .. yea i thought about that , i call the method during a button > click event [quoted text clipped - 9 lines] > 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
Kevin Spencer - 22 Nov 2006 19:06 GMT A DataGridView is a reference type. There is no need to pass it by ref.
 Signature HTH,
Kevin Spencer Microsoft MVP Ministry of Software Development http://unclechutney.blogspot.com
Never trust a dunderhead with a blunderbuss.
> ok i have a datagridview in my app and i have a function defined like > so: [quoted text clipped - 12 lines] > Thanks > Gideon Jon Skeet [C# MVP] - 22 Nov 2006 20:35 GMT > 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.
 Signature Jon Skeet - <skeet@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
Kevin Spencer - 23 Nov 2006 00:18 GMT 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.
 Signature HTH,
Kevin Spencer Microsoft MVP Ministry of Software Development http://unclechutney.blogspot.com
Never trust a dunderhead with a blunderbuss.
>> A DataGridView is a reference type. There is no need to pass it by ref. > [quoted text clipped - 19 lines] > See http://www.pobox.com/~skeet/csharp/parameters.html for more > information. giddy - 23 Nov 2006 04:09 GMT 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.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView2.Location = new System.Drawing.Point(5, 50); this.dataGridView2.Name = "dataGridView2";
so why is it null!
thanks
Gideon
> ---------------------------------------- > >> A DataGridView is a reference type. There is no need to pass it by ref. [quoted text clipped - 6 lines] >>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?
giddy - 23 Nov 2006 06:00 GMT 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
> 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 [quoted text clipped - 64 lines] > In other words, are you calling that function from a class that doesn't > know where the dgv is? Kevin Spencer - 23 Nov 2006 17:20 GMT Congratulations! We've all been there before. I am embarassed on a daily basis by things that only I know I did!
 Signature Kevin Spencer Microsoft MVP Ministry of Software Development http://unclechutney.blogspot.com
Never trust a dunderhead with a blunderbuss.
> hi , > [quoted text clipped - 84 lines] >> In other words, are you calling that function from a class that doesn't >> know where the dgv is? Jon Skeet [C# MVP] - 23 Nov 2006 19:16 GMT > 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 :)
 Signature Jon Skeet - <skeet@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
Jon Skeet [C# MVP] - 23 Nov 2006 07:46 GMT > 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. [quoted text clipped - 4 lines] > 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 [quoted text clipped - 7 lines] > > so why is it null! Well, we don't know for sure that it's that that's null - see above for the options.
 Signature Jon Skeet - <skeet@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
Jon Skeet [C# MVP] - 23 Nov 2006 07:43 GMT > 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.
 Signature Jon Skeet - <skeet@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
Kevin Spencer - 23 Nov 2006 17:19 GMT 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!
 Signature Best Wishes,
Kevin Spencer Microsoft MVP Ministry of Software Development http://unclechutney.blogspot.com
Never trust a dunderhead with a blunderbuss.
>> 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.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|