简体   繁体   中英

NullReferenceException being thrown despite debugger showing objects C#

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 ?

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM