I'm getting a NullPointerException by running the following code:
Main:
public class NeuralXOR {
public static void main(String[] args) {
double[] inputs = {1.0, 0.0};
NeuralNetwork network = new NeuralNetwork(2, 1, 3, 0.7, 0.9);
}
}
Neural Network Classes: public class NeuralNetwork {
Node[] inputNodes;
Node[] hiddenNodes;
Node[] outputNodes;
Layer[] layers;
int noOfInputNodes;
int noOfOutputNodes;
int noOfLayers = 3;
public NeuralNetwork(int noOfInputNodes, int noOfOutputNodes, int noOfHiddenNodes, double learningRate, double momentum) {
this.outputNodes = new Node[noOfOutputNodes];
for (int i = 0; i < outputNodes.length; i++) {
outputNodes[i] = new Node(i);
}
this.hiddenNodes = new Node[noOfHiddenNodes];
for (int i = 0; i < hiddenNodes.length; i++) {
hiddenNodes[i] = new Node(i);
}
this.inputNodes = new Node[noOfInputNodes];
for (int i = 0; i < hiddenNodes.length; i++) {
hiddenNodes[i] = new Node(i);
}
layers[0] = new Layer(inputNodes);
layers[1] = new Layer(hiddenNodes);
layers[2] = new Layer(outputNodes);
}
public void setInputs(double[] inputs) {
if (inputs.length == this.inputNodes.length) {
System.out.println(inputs.length);
for (int i = 0; i < inputs.length; i++) {
this.inputNodes[i].activation = inputs[i];
}
}
}
public double[] getOutputs() {
double[] outputs = new double[this.outputNodes.length];
for (Node outNode : this.outputNodes) {
outputs[outNode.nodeNumber] = outNode.activation;
}
return outputs;
}
}
class Node {
int nodeNumber;
double sum; // z
double activation; //Output
double deltaNode;
double gradient;
double[] outWeights;
public Node (int nodeNumber) {
this.nodeNumber = nodeNumber;
}
public void setOutWeights(int noOfOutNodes) {
this.outWeights = new double[noOfOutNodes];
for (int i = 0; i < outWeights.length; i++) {
this.outWeights[i] = Math.random();
}
}
public double sigmoid(double x) {
return 1 / (1 + Math.pow(Math.E, -x));
}
}
class Layer {
double layerBias;
Node[] nodes;
public Layer (Node[] nodes) {
this.nodes = nodes;
}
}
The error points at the line "layers[0] = new Layer(inputNodes);" in the NeuralNetwork class. No red is showing nor any warnings...
I assume it's an error from lack of knowledge on how the JVM works. Until now I've tried separating the classes into public classes in their separate files, which didn't help (wasn't expecting it to). I haven't tried anything else. Any thoughts? Thanks in advance!