0

I have defined the following class:

public class PerceptronNetwork : NetworkBase
{
    Neuron perceptron { get; set; }

    public PerceptronTrainer trainingMethod;

    public PerceptronNetwork(Neuron aNeuron)
    {
        this.perceptron = aNeuron;
    }

    public double train(TrainingTemplate trainingTemplate, int extMaxGenerations)
    {
        // This is simple but the ideea is suposed to be that in larger networks here
        // I do a foreach over the neurons
        double error =  this.trainingMethod.trainNetwork(trainingTemplate, perceptron,
                                                         extMaxGenerations);
        return error;
    }
}

Whenever I try to use the train method from my main function I get the error

Object reference not set to an instance of an object.

pointing to the perceptron object.

Despite that when I hover over every object in the function call trainingTemplate, perceptron and extMaxGenerations they all seem to be pointing to proper values.

Did I declare or instantiate them wrong in some way ?

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
George Bora
  • 1,618
  • 6
  • 26
  • 45
  • 1
    What *exactly* do you mean by "pointing to the perceptron object"? Is the exception definitely in `train`, or is it in `trainNetwork`? (As an aside, it's a good idea to get into the habit of following .NET naming conventions... and keeping your fields private.) – Jon Skeet Mar 12 '14 at 20:30
  • Show the full stack trace here please. – Crono Mar 12 '14 at 20:30
  • 1
    and trainingMethod? where is initialized? – Steve Mar 12 '14 at 20:31
  • 1
    We don't see where `aNeuron` (and therefore `perceptron`), `trainingMethod`, `trainNetwork` and `trainingTemplate` are created. They all could be `null`. – Olivier Jacot-Descombes Mar 12 '14 at 20:36
  • 1
    @OlivierJacot-Descombes that would not generate an exception until they were _used_ - and in that case the stack trace would point to `trainNetwork` (or a lower method). – D Stanley Mar 12 '14 at 20:38
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Steve Mar 12 '14 at 20:39
  • ... or `trainingMethod`. The other objects could throw an excpetion within the method `trainNetwork`. – Olivier Jacot-Descombes Mar 12 '14 at 20:44

2 Answers2

2

Ensure that this.trainingMethod is instantiated. From your code it doesn't seem to be.

If it is then you will have to show the full stack trace.

Crono
  • 10,211
  • 6
  • 43
  • 75
1

A NullReferenceException is not thrown when you pass a null parameter, it is thrown when you try and access a member property/method/field on a null reference. In your case it means that this.trainingMethod is null.

If trainNetwork had validation code to verify that the incoming parameters were not null, you would most likely get an ArgumentNullException with the name of the parameter that was null indicated.

If trainNetwork tried to reference an instance member on a null value that you passed in, the stack trace would originate from that method, not from train.

D Stanley
  • 149,601
  • 11
  • 178
  • 240