CBrauer

Hello,

My program shown below has the following run-time errror:

Object reference not set to an instance of an object.
Use the "new" keyword to create an object instance.

when I call

Net netObj = new Net(n_layers);

in InitNetwork(). The run-time error is in the constructor at the line:

Layer[k] = new Layer(n_layers, ni, nh, no);


My program is:


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

namespace Test {
class Program {
static int n_layers = 3;
static int ni = 30;
static int nh = 10;
static int no = 1;

public class Layer {
public int Units;
public double[] Output;
public double[] Error;
public Layer(int n_layers, int ni, int nh, int no) {
Units = 0;
Output = new double[n_layers];
Error = new double[n_layers];
}
};

public class Net {
public Layer[] Layer = null;
public double Alpha;
public Net(int n_layers) {
for (int k=0; k<n_layers; k++) {
Layer[k] = new Layer(n_layers, ni, nh, no);
}
Alpha = 0.0;
}
};

void InitNetwork() {
Layer[] layerObj = new Layer[n_layers];
for (int k=0; k<n_layers; k++) {
layerObj[k] = new Layer(n_layers, ni, nh, no);
}
Net netObj = new Net(n_layers);
netObj.Alpha = 0.9;
}

static void Main(string[] args) {
Program testObj = new Program();
testObj.InitNetwork();
}
}
}

Any help will be greatly appreciated.

Charles



Re: Visual C# Language Run-time error in constructor

Jerry Langley III

Hi Charles

In your Net class, I believe there are a few problems, The one that comes to mind first is when line 2 declares a public Layer[] array, but does not declare a length for the array. I believe that is why line 6 will not run.

Best Wishes,

Jerry

1: public class Net {
2: public Layer[] Layer = null;
3: public double Alpha;
4: public Net(int n_layers) {
5: for (int k=0; k<n_layers; k++) {
6: Layer[k] = new Layer(n_layers, ni, nh, no);
7: }
8: Alpha = 0.0;
9: }
10: };





Re: Visual C# Language Run-time error in constructor

CBrauer

Hmmm...

You said that line 2 declares a public Layer[] array. It's not a array of numbers, it's an array of classes (see Layer class).

If I change line 2 to: public Layer[] Layers = new Layer(n_layers, ni, nh, no);

I get a compiler error.

Charles





Re: Visual C# Language Run-time error in constructor

SP2

The first line of your Net constructor needs:

Layer = new Layer[n_layers];

You need to initialize the array before you can initialize the members of the array.





Re: Visual C# Language Run-time error in constructor

CBrauer

Don't you mean:

Layer[] Layer = new Layer[n_layers];





Re: Visual C# Language Run-time error in constructor

eradicator

CBrauer wrote:

Don't you mean:

Layer[] Layer = new Layer[n_layers];

No he doesn't mean that. You already declared the Layer variable here:

public Layer[] Layer = null;

So if you (re)declare Layer's type in the constructor, you will not properly set the above member variable.




Re: Visual C# Language Run-time error in constructor

Figo Fei - MSFT

Either

Code Block
Layer[] Layer = new Layer[n_layers];

Or

Code Block

public Layer[] Layer = null;

is delaration/definition.

What you need is initialization(assign something to the array) like this:

Code Block

Layer[0]=new Layer(blabla..);

Layer[1]=new Layer(blabla...);
Layer[2]=new Layer(blabla....);

Thanks