Guys tell me, there is a class, there are variables in it:

double[][] weightIH; double[][] weightHO; 

The default value is null, there is a method where the values ​​of these variables are initialized:

 void test() { weightIH = new double[1 + NUM_IN][NUM_HID]; weightHO = new double[1 + NUM_HID][NUM_OUT]; for (int j = 0; j < NUM_HID; j++) for (int i = 0; i < 1 + NUM_IN; i++) weightIH[i][j] = 2.0 * (randomize() - 0.5) * smallwt; for (int k = 0; k < NUM_OUT; k++) for (int j = 0; j < 1 + NUM_HID; j++) weightHO[j][k] = 2.0 * (randomize() - 0.5) * smallwt; } 

Then, there is a class where the values ​​of these variables are needed:

 class Training { void name() { Network network = new Network(); for (int j = 0; j < numHidden; j++) { sumH[j] = network.weightHO[0][j]; for (int i = 0; i < numInput; i++) sumH[j] += input[p][i] * network.weightIH[1 + i][j]; hidden[j] = 1.0 / (1.0 + Math.exp(-sumH[j])); } } } 

After launch, I catch NullPointerException, i.e. In the Training class, the weightIH and weightOH variables are null.

How to solve the problem with the transfer of variable values, without null?

  • so there is no call to the void test() method, so the variables are not initialized. In general, it is probably better to initialize the values ​​in the constructor. Those. in the Network class Network do a constructor in which to initialize the variables. - I. Smirnov
  • 2
    Possible duplicate question: What is Null Pointer Exception and how to fix it? - Alexey Shimansky
  • Update: you have the required arrays do not store any values. they were only initialized, i.e. there was a place for them. And the values ​​do not lie there, as a result, when accessing the elements of the arrays, null is returned - I. Smirnov
  • I forgot to add the code for the filling, they are initialized and filled in the method. Calling methods in main is first a method with test () and then a method from the class Training name () - D.Stifler
  • A separate Network object is created in your Training.name method. The fact that you are somewhere in the main of some other object has caused something that does not affect it. Perhaps you should change the name() method to name( Network network ) and pass the desired object as a parameter. - zRrr

2 answers 2

you create an object but do not initialize with the test () method

 class Training { void name() { Network network = new Network(); // после этого нужно вызвать метод инициализации network.test(); // дальше будут работать for (int j = 0; j < numHidden; j++) { sumH[j] = network.weightHO[0][j]; for (int i = 0; i < numInput; i++) sumH[j] += input[p][i] * network.weightIH[1 + i][j]; hidden[j] = 1.0 / (1.0 + Math.exp(-sumH[j])); } } } 

ps I do not understand why the code is not added normally

  • and those. you need to create an Object in the Training class and just call test () and disappear null? Why, then, doesn’t work in main, I call test () and then name ()? - D.Stifler
  • @ Tsovak Saakyan It is necessary to select the text and press Ctrl+K or the button on the panel - Alexey Shimansky
  • either you use a single quote to reverse (which is on the button with E) or indent. But in general, there is a help and there are buttons for formatting) - I. Smirnov
  • Thank you very much. I have disappeared menu with formatting buttons. but after updating the page all the rules turned out - Senior Pomidor
  1. you have the required arrays do not store any values. they were only themselves initialized, i.e. there was a place for them. And values ​​do not lie there, as a result at the appeal to elements of arrays null returns

  2. There is no call to the void test () method for the Network object, so the variables are not initialized. In general, it is probably better to initialize the values ​​in the constructor. Those. in the Network class, do a constructor in which to initialize the variables

  • oh sorry I forgot to add a value filling code - D.Stifler