Run-time error in constructor
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

