0

I am very interested in ai and wanted to create my own, so I watched some videos and found a good library. For my first ai, it is very simple. I did this to get a grip on the library and how it works for more complex things in the future. All this ai is supposed to do is get an input of four small numbers, and the output is only 1 if all four inputs are 1. (dont worry about the AI and AIType classes) here is the code, please let me know if i am doing something wrong because i've looked in a lot of places and couldn't find anything:

    AI aitest = new AI(AIType.FIRST_AI);
    DataSet ds = new DataSet(4,1);
    ds.add(new double[]{0,0,0,0},new double[]{0});
    ds.add(new double[]{0,0,0,1},new double[]{0});
    ds.add(new double[]{0,0,1,0},new double[]{0});
    ds.add(new double[]{1,1,0,0},new double[]{0});
    ds.add(new double[]{1,0.1,1,3},new double[]{0});
    ds.add(new double[]{1,1,1,1},new double[]{1});
    ds.add(new double[]{1,1,1,1},new double[]{1});
    ds.add(new double[]{0,1,2,5},new double[]{0});
    
    Layer l = new Layer();
    InputLayer il = new InputLayer(4);
    aitest.getAI().getNN().setInputNeurons(il.getNeurons());
    for(int ignored :new int[]{1,2,3,4}){
        Neuron n = new Neuron();
        for(Neuron all: aitest.getAI().getNN().getInputNeurons()) {
            n.addInputConnection(all);
        }
        l.addNeuron(n);
    }

    aitest.getAI().getNN().addLayer(l);
    aitest.getAI().train(ds);

It throws a NPE on the last line with this error:

Exception in thread "main" java.lang.NullPointerException
at org.neuroph.core.NeuralNetwork.learn(NeuralNetwork.java:297)
at me.ai.aicore.SuperAI.train(SuperAI.java:31) // these last two lines just call the .learn method
at me.ai.Core.main(Core.java:38)

(there are more error lines but those aren't relevant)

The line in neuroph (NeuralNetwork class) which this error originates is marked:

      public void setWeights(double[] weights) {
    int i = 0;
    Iterator var3 = this.layers.iterator();

    while(var3.hasNext()) {
        Layer layer = (Layer)var3.next();
        Iterator var5 = layer.getNeurons().iterator();

        while(var5.hasNext()) {
            Neuron neuron = (Neuron)var5.next();
            // the line below is where the error comes from
            for(Iterator var7 = neuron.getInputConnections().iterator(); var7.hasNext(); ++i) {
                Connection conn = (Connection)var7.next();
                conn.getWeight().setValue(weights[i]);
            }
        }
    }

}
        

All help is appreciated.

EDIT: I added the full stacktrace, but everything besides the first like is useless as its just the ones calling methods. by the way, the bottom codeblock is from the library. I updated it to show the full method

zachary cohen
  • 55
  • 2
  • 9
  • give us a bit more code. What is `i` and where have you declared and initialized it? – Boris Strandjev Apr 05 '21 at 13:46
  • Show the full stack trace please – Yassin Hajaj Apr 05 '21 at 13:51
  • I updated the post to show more info – zachary cohen Apr 05 '21 at 14:18
  • `NullPoitnerException` is still too vauge, and the stacktrace only tells us what line it's on. is `neuron` null? is `neuron.getInputConnections()` null? is `var7` null? I'd recommend printing out as many variables as you can to figure out what the values are in there to get an idea of what's not behaving as you expect it to. – Frank Bryce Apr 05 '21 at 18:10

0 Answers0