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 ?

7
  • 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.) Commented Mar 12, 2014 at 20:30
  • Show the full stack trace here please. Commented Mar 12, 2014 at 20:30
  • 1
    and trainingMethod? where is initialized? Commented Mar 12, 2014 at 20:31
  • 1
    We don't see where aNeuron (and therefore perceptron), trainingMethod, trainNetwork and trainingTemplate are created. They all could be null. Commented Mar 12, 2014 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). Commented Mar 12, 2014 at 20:38

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

4 Comments

Ummm... passing a null parameter could result in a NullReferenceException.
Yes but from the method that uses it, not from the method that passes the parameter.
This was it, sorry for making such an obvious error (never code when you're tired from work)
@GeorgeBora Happens to the best of us :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.