Can you have a look at my code please, where am i going wrong?

Compiles fine but i get a nullPointerException

in the setLayerOne() method and the setInputs() method

the error seems to point to the Neuron[][] network

but i thought it should be ok as i initilised the pointer as static

the Neuron class is compiled and contains both these methods.

also, is this bad design to make all my varibles static like this

thanks for the help

Tim

import java.io.*;

publicclass Guesser{

staticchar[] user =newchar[100];

staticint guess_no = 1;

staticint noOfLayers;

static BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

static Neuron[][] network;

staticint[] layers;

publicstaticvoid main(String args[])

throws IOException

{

guesserNetwork();

///now for the guessing bit

int guess_no = 0;

char guess;

//char dummy;

//BufferedReader br = new BufferedReader( new InputStreamReader(System.in));

char[] user =newchar[100];

while (guess_no<10){

System.out.println("Please think of Heads (H) or Tails (T) then press enter.");

if (guess_no == 1)

{

guess ='H';

}

elseif ( user[guess_no] =='H' )

{

guess ='T';

}

else

{

guess ='H';

}

// now we tell the user what our guess was,

char dummy;

dummy = (char) br.read();// wait for the return key pressed

System.out.println(guess);

// Find out what the user was thinking of

// and force the user to H or T

user[guess_no] = (char) br.read();

if (user[guess_no] =='h')

{user[guess_no] ='H';}//this does not work i dont know why

elseif (user[guess_no] =='t')

{user[guess_no] ='T';}

dummy = (char) br.read();// clear the stream for the return key press

while ( user[guess_no] !='H' && user[guess_no] !='T')

{

System.out.println("You must enter H or T");

user[guess_no] = (char) br.read();

}

setLayerOne();//////HERES WHERE I GET NULLPOINTER EXCEPTION

guess_no++;

}

while (guess_no<100){

//call update method

//print output of last node

network[0][guess_no].inputGuess(guess_no);

guess_no++;

}

}

staticvoid setLayerOne()////I GET NULLPOINTER EXCEPTION

{

network[0][guess_no].inputGuess(guess_no);// sets the first layer neurons

}

staticvoid setInputs ()

{

for (int i = 1; i < noOfLayers; i++){

for(int j=0; j < layers[i]; j++){

network[i][j].updateInput(i, network[i-1], network[i-1].length);//sets the other neurons

}

}

}

staticvoid guesserNetwork()

{

int noOfNeurons[] ={10,1,1};

int nodes = 0;

noOfLayers = 3;

int[] layers =newint[noOfLayers];

for (int i =0; i < noOfLayers; i++)

{

layers[i] = noOfNeurons[i];

System.out.println("layer " + (i + 1) +" has " + noOfNeurons[i] +" neurons");

nodes += noOfNeurons[i];

}

System.out.println("total no of nodes = " + nodes);

System.out.println("building network...");

//make (noOfLayers)arrays of arrays

Neuron[][] network =new Neuron[noOfLayers][];

for (int i=0; i < noOfLayers; i++)

{

//for each layer make the desired number of rows(of neurons)

int rows = layers[i];

network[i] =new Neuron[rows];

//instinate neuron objects one by one

for (int j=0; j < layers[i]; j++)

{

if (i==0)

{

network[i][j] =new Neuron(noOfLayers, i, j, rows, 1, user[j]);

}

else

{

boolean isFirstLayer =false;

network[i][j] =new Neuron(noOfLayers, i, j, rows, layers[i-1], network[i-1]);

}

}

}

}

}

[8359 byte] By [Pingua] at [2007-11-26 21:09:59]
# 1
Paste in the exact, complete error message and indicate clearly which line it's talking about.
jverda at 2007-7-10 2:46:11 > top of Java-index,Java Essentials,Java Programming...
# 2
static Neuron[][] network;This is never initialized.P.S. Why is everything static?
sabre150a at 2007-7-10 2:46:11 > top of Java-index,Java Essentials,Java Programming...
# 3

See the comments:public class Guesser {

static Neuron[][] network; // gets never initialized!

// ...

static void guesserNetwork() {

// ...

Neuron[][] network = new Neuron[noOfLayers][]; // 'network' now is a local variable. Remove 'Neuron[][]' from it.

// ...

}

}

Message was edited by:

prometheuzz

Darn, sabre beat me to it!

prometheuzza at 2007-7-10 2:46:11 > top of Java-index,Java Essentials,Java Programming...
# 4

As far as I can see you never initialize this array

static Neuron[][] network;

because in guesserNetwork() you declare a local variable

//make (noOfLayers)arrays of arrays

Neuron[][] network = new Neuron[noOfLayers][];

#

*edit* ****** everybody beat me to it! =)

duckbilla at 2007-7-10 2:46:11 > top of Java-index,Java Essentials,Java Programming...
# 5

Main calls guesserNetwork() which initilises the array:-

Neuron[][] network = new Neuron[noOfLayers][];

for (int i=0; i < noOfLayers; i++)

{

//for each layer make the desired number of rows(of neurons)

int rows = layers[i];

network[i] = new Neuron[rows];

//instinate neuron objects one by one

for (int j=0; j < layers[i]; j++)

{

if (i==0)

{

network[i][j] = new Neuron(noOfLayers, i, j, rows, 1, user[j]);

}

else

{

boolean isFirstLayer = false;

network[i][j] = new Neuron(noOfLayers, i, j, rows, layers[i-1], network[i-1]);

}

}

}

later main calls setLayerOne():-

static void setLayerOne()

{

network[0][guess_no].inputGuess(guess_no); // sets the first layer neurons

}

and gives this error:-

NullPointerException:

at Guesser.setLayerOne(Guesser.java:77)

at Guesser.main(Guesser.java:62)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

I nave set network[][] to static so that it can be acessed from the main method and the other methods in this class.

thanks again

Pingua at 2007-7-10 2:46:11 > top of Java-index,Java Essentials,Java Programming...
# 6
> Main calls guesserNetwork() which initilises the> array:-> ...See reply 3, 4 and 5.
prometheuzza at 2007-7-10 2:46:12 > top of Java-index,Java Essentials,Java Programming...
# 7
Thanks very much promethim on my way again:)))
Pingua at 2007-7-10 2:46:12 > top of Java-index,Java Essentials,Java Programming...