Newbie: Error when calling a public function from other class

  • Thread starter Thread starter Tinus
  • Start date Start date
T

Tinus

Hello,

I'm trying to call a public function located in a different class, but get
an error message:
Object reference not set to an instance of an object.

I've got 2 classes: clsMain and clsData. In clsData I've a filled (private
or public) DataTable. When I instantiate clsData in clsMain and then call
from clsMain a public function that counts the number of rows in the
DataTable I get the above mentioned error. However, when I call the same
function from clsData itself then the count works....

What am I doing wrong?

Can someone help me with this?

Thanks in advance,
Tinus
 
Hi Tinus,

The NullReferenceException is the most common error in .Net, but to be able to say exactly what is wrong with your code I would need to see it.

The error basically tells you that you are trying to use the reference to perform actions on an object. And the reference does not point to a valid object, it is empty (null).

TextBox tb;

tb.Text = "Hello"; <- Object Reference not set to an instance of an object

tb is just a reference to a TextBox. Before you can use tb you need to attach a TextBox object to it.

TextBox tb;

tb = new TextBox(); <- create a TextBox object and store the reference in tb

tb.Text = "Hello"; <- ok
 
Thanks for your fast answer Morten!

The code is a bit large so I will try to explain it a bit better.
I've got a form called frmMain and a usercontrol called ctlData.
In frmMain I've designed a button with a onclick event and a panel called
panelMain. In ctlData I've designed a panel with a textbox. Also in this
ctlData I've coded to open a database and fill a table called dtData. Then I
put the data in the textbox that is located in ctlData. I also coded a
public function that counts the numbers of rows in dtData and returns it.
When I call this funtion within ctlData e.g.:
MessageBox.Show(RowsInTable().ToString()); Then it shows a messagebox with
the number of rows.

In the onclick event from the button in frmMain I placed the following code:

ctlData Data= new ctlData();
foreach(Control c in panelMain.Controls )
{
if (c.Name == "ctlData")
{
c.BringToFront();
return;
}
}
panelMain.Controls.Add(Data);
Data.BringToFront();

Now, when I press the button in frmMain it displays the textbox defined in
ctlData in frmMain with information from the datatable in it.

But if I use the following code I get the NullReference error:

ctlData Data= new ctlData();
foreach(Control c in panelMain.Controls )
{
if (c.Name == "ctlData")
{
c.BringToFront();
return;
}
}
panelMain.Controls.Add(Data);
Data.BringToFront();
MessageBox.Show(Data.RowsInTable().ToString());

When I run this code I get the NullReference error: Object reference not set
to an instance of an object.

When I look closer to the error it is generated in ctlData in the function
RowsInTable() where I call the function dtData.Rows.Count();

Hope you now see what I want and what is wrong....

Thanks for your help.

Tinus



Morten Wennevik said:
Hi Tinus,

The NullReferenceException is the most common error in .Net, but to be
able to say exactly what is wrong with your code I would need to see it.
The error basically tells you that you are trying to use the reference to
perform actions on an object. And the reference does not point to a valid
object, it is empty (null).
 
Tinus,

I would check the code in ctlData, and the method RowsInTable. I suspect dtData is a null reference.

Make sure dtData is a valid reference before you use it. If you create the object dtData points to at a later time, make sure you don't try to use it if it is null.

Something like this:

if(dtData != null)
return dtData.Rows.Count();
else
return -1; // or any other value to indicate non existing dtData

or

return dtData == null ? -1 : dtData.Rows.Count();
 
The ctlData class looks something like this:

public class ctlData
{
private System.Data.DataTable dtPersonalia;
...
private void LoadDatabase()
{
dtData = new DataTable();
...
}
public intCountInRows()
{
return dtData.Rows.Count();
}
}

The LoadDatabase() is only called once when the control is added to frmMain.

So I think that the dtData is null when I call the CountInRows from frmMain
but is filled when I call it from ctlData.

If I change the decleration op dtData to public static then it works fine.
Is that the solution..?? Seems a bit dirty to me...

Thanks,
Tinus

Morten Wennevik said:
Tinus,

I would check the code in ctlData, and the method RowsInTable. I suspect dtData is a null reference.

Make sure dtData is a valid reference before you use it. If you create
the object dtData points to at a later time, make sure you don't try to use
it if it is null.
 
Tinus,

You shouldn't need to make it static. It looks to me like you may be calling CountInRows before LoadDataBase.

You may also do something like

public int CountInRows()
{
if(dtData == nul)
LoadTable();

return dtData.Rows.Count;
}
 
Well, if I make dtTable private static, it works. However, it is not the
solution, because I have the same problem when I want to update a TextBox
with the number of rows found in the Table.

I've just posted an other question that in the end is the same problem. The
topic is called "Updating texbox from class that did not create the
textbox".

The problem is that when I call a public function that is in a class that
created a control from a different class, all the controls are null. But
when I call this public function from within the class that created the
control they are not null.... :-(

Loading the database every time is not an option, because it is a large
database and is retrieved over the internet.

Please look at my other topic, maybe then you see what I'm doing wrong...
It's driving me nuts so far :-(

As you maybe have noticed, I'm not an experienced coder.... this project I'm
doing is my first real big project in c#.

Thanks,
Tinus

Morten Wennevik said:
Tinus,

You shouldn't need to make it static. It looks to me like you may be
calling CountInRows before LoadDataBase.
 
Back
Top