I am getting a nullReferenceException when using vector.push_back on a new object.
In the code segment you see I have the vector object as a pointer but I originally had it as a non-pointer, I changed it in my troubleshooting out of desperation.
I stepped through the instantiation of BasicSolver completely to make sure the were no issues in it and also separated the instantiation from the push_back to help show that push_back is where the problem is occurring.
InformedSolver1&2 are children of BasicSolver. vector is included in BasicSolver and therefore in puzzleSolver also.
#include "stdafx.h"
#include "puzzleSolver.h"
PuzzleSolver::PuzzleSolver()
{
solvers = new vector<BasicSolver*>();
}
void PuzzleSolver::createPuzzle(string input)
{ //hitting step-over
BasicSolver* temp = new BasicSolver(input);// no errors
solvers->push_back(temp); // nullReferenceException
solvers->push_back(new InformedSolver1(input));
solvers->push_back(new InformedSolver2(input));
}
That should be all the relevant information. Let me know if you have any ideas as to what is causing this/ how to fix it! Thanks.
Edit: Added BasicSolver constuctor and dependency methods by comment request also a little background: this is a Sudoku solver for an AI class
BasicSolver::BasicSolver(string input)
{
peers = new vector<Peer*>();
squares = new vector<Square*>();
unsolved = new vector<Square*>();
solved = new vector<Square*>();
createStructure(input);
original = input;
mistakes = 0;
}
void BasicSolver::createStructure(string input)
{
try
{
createEmptyStructure(peers, squares, unsolved);
//Parse the puzzle and assign input to squares
int numCharsToRead = MAX_SQUARES; //makes sure vector isn't outside its range
if (input.length() < MAX_SQUARES) //makes sure string isn't outside its range
numCharsToRead = input.length();
for (int i = 0; i < numCharsToRead; i++)
{
if(input[i] != '.')
insertValue(input[i], (*squares)[i], unsolved);
}
}
catch(exception e)
{
throw e;
}
}
void BasicSolver::createEmptyStructure(vector<Peer*> *workingPeers, vector<Square*> *workingSquares, vector<Square*> *workingUnsolved)
{
for (int i = 0; i < MAX_PEERS; i++)
{
workingPeers->push_back(new Peer());
}
for (int i = 0; i < 81; i++)
{
try
{
workingSquares->push_back(new Square('.'));
//Adding the square to its corresponding peers
(*workingPeers)[i / MAX_ROWS]->addSquare((*workingSquares)[i]); //adds the square into its appropriate row of peers
(*workingPeers)[(i % MAX_ROWS) + COL_OFFSET]->addSquare((*workingSquares)[i]); //adds the square into its appropriate column of peers
int tempBoxCol = (i % MAX_ROWS) / MAX_BOX_COLS; //returns the box column (0,1,2)
if ((i / MAX_ROWS) < BOX_ROW_WIDTH) //if its box is in the first row
{
(*workingPeers)[tempBoxCol + BOX_OFFSET]->addSquare((*workingSquares)[i]);
}
else if ((i / MAX_ROWS) < (2 * BOX_ROW_WIDTH)) //if its box is in the second row
{
(*workingPeers)[BOX_ROW_WIDTH + tempBoxCol + BOX_OFFSET]->addSquare((*workingSquares)[i]);
}
else //if the box is in the third row
{
(*workingPeers)[2 * BOX_ROW_WIDTH + tempBoxCol + BOX_OFFSET]->addSquare((*workingSquares)[i]);
}
}
catch(exception e)
{
throw e;
}
}
*workingUnsolved = *workingSquares;
}
Edit2: Personal tests add these lines:
vector<BasicSolver*>* test = new vector<BasicSolver*>();
test->push_back(temp);
before
solvers->push_back(temp);
and they execute fine, I also notice that during runtime solvers is listed as outside of scope even though it is a protected variable of BasicSolver.
The problem was that the calling source did not have PuzzleSolver properly instantiated. Thanks to @TheDark for suggesting I check that!