I get this error "Object reference not set to an instance of an object"

M

Mucahit ikiz

Hello,

First of all, I created Base Form called "tureyen_frm".
Then i created a new form by inheritance using the Base Form. This name is
"tureten" form.
For each new form derived from the Base Form a , I created a class.
And than some methods in Base Windows Form use in the the derivate forms
class.
Some of the methods in the Base Form need to use classes of the derivative
forms.
Of cours, I make a reference from derivative Forms to Base Form in a CLASS
At this point I need establish a reference from the Base Form to the
classes in the derivative forms.

but, I get this error.
"Object reference not set to an instance of an object."

Please, How can i do,
Thanks in advance
Apologies for my English



This is order operation

1.Operation

I created Interface.


using System;
using System.Collections.Generic;
using System.Text;

namespace inttest.Interfaces
{
public interface Iadsoyad
{
string ad { get;}
string soyad { get;}
}
}
2.Operation
I derived a class from interface.


using System;
using System.Collections.Generic;
using System.Text;
using inttest.Interfaces;
namespace classsharp
{

public class cls_adsoyad : Iadsoyad
{
public string _ad=string.Empty;
public string _soyad=string.Empty;


public string ad
{
get { return _ad; }
}

public string soyad
{
get { return _soyad; }
}

public cls_adsoyad()
{
}
public cls_adsoyad(string adi, string soyadi)
{
_ad = adi;
_soyad = soyadi;
}

}
}

3.Operation


I created a Base Form and name is "tureyen_frm".
This form will be used as a Base Form the derivative forms that will
inherit from the base form.
The methods in the Base Form be executed in the derived form.
so, i am using The Interface in Base Form.
I use the classes of the derivative forms from the Base Form.

derivative forms

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using inttest.Interfaces;


namespace sharptest
{
public partial class tureyen_frm : Form
{
protected Iadsoyad tr_adsoyad;
string isim = string.Empty;
public tureyen_frm()
{
InitializeComponent();
}

private void tureyen_frm_Load(object sender, EventArgs
e)
{
isim = tr_adsoyad.ad;
}

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(isim.ToString());
}
}
}

4.Operation

I created derivative Form from the Base Form , this name is "turetilen"
form.
And call it;


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using classsharp;

namespace sharptest
{
public partial class turetilen : tureyen_frm
{

public turetilen()
{
InitializeComponent();
cls_adsoyad ogrenci=new cls_adsoyad("Ali","Okur");

}
private void tureyen_frm_Load(object sender, EventArgs
e)
{
;
}
}

}

5.Operation

And than i make debug step by step.

This script get error "cls_adsoyad ogrenci=new
cls_adsoyad("Ali","Okur");"

This error message is "Object reference not set to an instance of an
object."


Can you help me? in anyone.
 
M

Marc Gravell

Although the code isn't the clearest, the posted code doesn't appear
to contain an obvious "Object reference not set to an instance of an
object" error; adding a Main() that runs a "turetilen" works fine, but
the posted code doesn't acutally *do* anything. I had to remove the
InitializeComponent() code, as the designer code is missing... I
suspect that the problem is in this missing code.

If possible, can you post a short complete example that illustrates
the problem?

I must admit that I did have a little difficulty understanding your
descriptions, so I can't really advise on whether the approach is
suitable - but if you are interested there are a number of "best
practice" issues that could be improved - but nothing that would
actually cause an immediate bug.

Marc
 
M

Marc Gravell

Ah; possibly found it... your "Load" handler (which doesn't fire for
me because InitializeComponent() is missing from the posted code)
references:

isim = tr_adsoyad.ad;

but tr_adsoyad does not appear to ever get set.

If this is happening in your "designer" code, then please post where
this gets set.

However, I suspect that you could do most of this a lot easier simply
using a "virtual" method. Again, I'm struggling to understand the
descriptions to describe the best approach.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top