473,756 Members | 4,863 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

simple question about ref with object?? PLEASE HELP

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.c s????
-private System.Windows. Forms.DataGridV iew dataGridView2;

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

Thanks
Gideon

Nov 22 '06 #1
14 1631
It will be initialised in InitialiseCompo nent 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.c s????
-private System.Windows. Forms.DataGridV iew dataGridView2;

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

Thanks
Gideon

Nov 22 '06 #2
um.. .. yea i thought about that , i call the method during a button
click event

private void button1_Click(o bject sender, EventArgs e)
{
FillColumns(ref dataGridView2);
//CreateDocumentP ropertyTable();
}
so yes, the exception (NullRefrenceEx ception) 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.mi crosoft.comwrot e:
It will be initialised in InitialiseCompo nent 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://wannabedevelope r.spaces.live.c om

"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.c s????
-private System.Windows. Forms.DataGridV iew dataGridView2;
PLEASE HELP ME OUT.. i need this in a hurry!
Thanks
Gideon
Nov 22 '06 #3
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...@gmai l.comwrote:
um.. .. yea i thought about that , i call the method during a button
click event

private void button1_Click(o bject sender, EventArgs e)
{
FillColumns(ref dataGridView2);
//CreateDocumentP ropertyTable();
}
so yes, the exception (NullRefrenceEx ception) 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.mi crosoft.comwrot e:
It will be initialised in InitialiseCompo nent 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://wannabedevelope r.spaces.live.c om
"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.c s????
-private System.Windows. Forms.DataGridV iew dataGridView2;
PLEASE HELP ME OUT.. i need this in a hurry!
Thanks
Gideon
Nov 22 '06 #4
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*******@gmai l.comwrote in message
news:11******** **************@ e3g2000cwe.goog legroups.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.c s????
-private System.Windows. Forms.DataGridV iew dataGridView2;

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

Thanks
Gideon

Nov 22 '06 #5
"giddy" <gi*******@gmai l.comwrote in message
news:11******** **************@ j44g2000cwa.goo glegroups.com.. .
um.. .. yea i thought about that , i call the method during a button
click event

private void button1_Click(o bject sender, EventArgs e)
{
FillColumns(ref dataGridView2);
//CreateDocumentP ropertyTable();
}
so yes, the exception (NullRefrenceEx ception) 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
Kevin Spencer <sp**@uce.govwr ote:
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.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 22 '06 #7
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.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
Kevin Spencer <sp**@uce.govwr ote:
>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.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 23 '06 #8

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.ColumnCo unt =
dataSet.Tables["customers"].Columns.Count; //THIS IS THE LINE.

VS says :

NullRefrenceExc eption 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.DataGridV iew dataGridView2;

and its properties are set like this :
this.dataGridVi ew2.ColumnHeade rsHeightSizeMod e =
System.Windows. Forms.DataGridV iewColumnHeader sHeightSizeMode .AutoSize;
this.dataGridVi ew2.Location = new System.Drawing. Point(5, 50);
this.dataGridVi ew2.Name = "dataGridView2" ;

so why is it null!

thanks

Gideon

On Nov 23, 5:18 am, "Kevin Spencer" <s...@uce.govwr ote:
----------------------------------------
Kevin Spencer <s...@uce.govwr ote:
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

hi ,

i'm really embarrased about this ,

theDGV.ColumnCo unt =
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...@gmai l.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.ColumnCo unt =
dataSet.Tables["customers"].Columns.Count; //THIS IS THE LINE.

VS says :

NullRefrenceExc eption 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.DataGridV iew dataGridView2;

and its properties are set like this :
this.dataGridVi ew2.ColumnHeade rsHeightSizeMod e =
System.Windows. Forms.DataGridV iewColumnHeader sHeightSizeMode .AutoSize;
this.dataGridVi ew2.Location = new System.Drawing. Point(5, 50);
this.dataGridVi ew2.Name = "dataGridView2" ;

so why is it null!

thanks

Gideon

On Nov 23, 5:18 am, "Kevin Spencer" <s...@uce.govwr ote:----------------------------------------
Kevin Spencer <s...@uce.govwr ote:
>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

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

Similar topics

38
3537
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser = serial.Serial()
1
6027
by: Randell D. | last post by:
HELP! I am determined to stick with this... I'm getting there... for those who haven't read my earlier posts, I'm createing what should be a simple function that I can call to check that required fields in a form have values. I'm writing the values to the client using document.write only so that I can confirm that the values are there to be played with - this is not the final result so please (unless it's leading to the script failure)...
5
7246
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files, two aspects which I want to incorporate into my program eventually. That aside, my most pressing problem right now is how to get rid of the newline in the input when I use fgets(). Now I have looked around on the net, not so much in this group...
3
2166
by: Peter | last post by:
Hello Thanks for reviewing my question. I would like to know how can I programmatically select a node Thanks in Advanc Peter
0
7566
by: Tal Sharfi | last post by:
Hi everyone I recently had the need for StringGrid object same as the one that Delphi has. An object that helps show lists of other objects in a simple grid. I searched the news groups and found none, so, I wrote one and decided to share it with you. It's a very simple one with few functions. I derived a DataGrid and added to it a DataTable to hold the data. The object itself is handling the synchronization between them, because...
1
1763
by: jm | last post by:
Easy probably, please read on. I know some of you have commented already about some of my socket question. I appreciate that. I have a Form1: static void Main() { Application.Run(new Form1()); clsListening.AsynchronousSocketListener.StartListening();
9
1374
by: NewInTheGame | last post by:
I'm a beginner in VB.Net. I'm actually taking a class & so far I understand everything, but I suck when it comes to arrays. I'm sure that this is simple (I'm ashamed of asking :oops: )... I have some textboxes. I'm trying to store the information in an array, but I'm lost as to how: I have textboxes for FirstName, LastName & PhoneNumber. My problem is that the number of arrays is unknown so our teacher wants us to declare it as...
73
4622
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an endless loop in a line with: if a==b: print 'OK' I mean, it would be of much help to me on my way to understanding Python to know how such prefix code leading to an endless loop can look like and if it is eventually not possible to write such...
17
5816
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /* Simple Thread Object ______________________________________________________________*/ #include <pthread.h> extern "C" void* thread_entry(void*);
0
9325
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9152
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9716
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...
0
9571
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8569
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...
0
6410
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();...
1
3676
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
3185
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2542
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.