title: C++ Exception thrown: read access violation. this was nullptr
body: <p>For one of my programming classes, we are required to design a program that can be ran through the provided "stress tests" that our instructor wrote himself.</p>
  
  <p>We are working with nodes and linked lists but in a way that is different than any of the YouTube videos I have looked at on the subject.</p>
  
  <p>I've been tearing my hair out for the past couple of days trying to figure out what is wrong with my program but I'm having no luck.</p>
  
  <p>Here is the code for my Node.cpp file (didn't include Node.h)</p>
  
  <pre><code>#include "Node.h"
  
  Node::Node() {
      m_value = 0;
      m_next = nullptr;
  }
  
  void Node::setValue(int val) {
      m_value = val;
  }
  
  int Node::getValue() const {
      return m_value;
  }
  
  void Node::setNext(Node* prev) {
      m_next = prev;
  }
  
  Node* Node::getNext() const {
      return m_next;
  }
  </code></pre>
  
  <p>Here is my LinkedList.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include "LinkedList.h"
  
  LinkedList::LinkedList() {
      m_front = nullptr;
      m_size = 0;
  }
  
  LinkedList::~LinkedList() {
      // Deconstructor
      m_size = 0;
      Node* a = m_front;
      Node* b = a-&gt;getNext();
      while (a-&gt;getNext() != NULL) {
          delete a;
          a = b;
          b = b-&gt;getNext();
      }
      delete a;
      a = NULL;
  }
  
  bool LinkedList::isEmpty() const{
      if (m_size == 0) {
          return true;
      }
      else {
          return false;
      }
  }
  
  int LinkedList::size() const {
      return m_size;
  }
  
  bool LinkedList::search(int value) const {
      if (m_size == 0) {
          return false;
      }
      else if (m_size == 1) {
          if (m_front-&gt;getValue() == value) {
              return true;
          }
          else {
              return false;
          }
      }
      else {
          Node* a = m_front;
  
          for (int i = 0; i &lt; m_size; i++) {
              if (a-&gt;getValue() == value) {
                  return true;
              }
              else {
                  a = a-&gt;getNext();
              }
          }
          return false;
      }
  }
  
  void LinkedList::printList() const {
      std::cout &lt;&lt; "List: ";
      if (m_size == 0) {
          // Print Nothing
      }
      else if (m_size == 1) {
          std::cout &lt;&lt; m_front-&gt;getValue();
      } 
      else {
          Node* a = new Node();
          a = m_front;
          int b = m_front-&gt;getValue();
          std::cout &lt;&lt; b &lt;&lt; ", ";
  
          while (a-&gt;getNext() != NULL) {
              a = a-&gt;getNext();
  
              if (a-&gt;getNext() == NULL) {
                  std::cout &lt;&lt; a-&gt;getValue();
              }
              else {
                  std::cout &lt;&lt; a-&gt;getValue() &lt;&lt; ", ";
              }
          }
      }
      std::cout &lt;&lt; std::endl;
  
  }
  
  void LinkedList::addBack(int value) {
      Node* a = new Node();
      a-&gt;setValue(value);
      if (m_size == 0) {
          m_front = a;
      }
      else {
          Node* b = new Node();
          b = m_front;
          while (b-&gt;getNext() != NULL) {
              b = b-&gt;getNext();
          }
          b-&gt;setNext(a);
      }
      m_size++;
  }
  
  void LinkedList::addFront(int value) {
      Node* a = new Node(); // Check later
  
      a-&gt;setNext(m_front);
      a-&gt;setValue(value);
      m_front = a;
      m_size++;
  
  }
  
  bool LinkedList::removeBack() {
      if (m_size == 0) {
          return false;
      }
      else {
          Node* a = new Node();
          Node* b = new Node();
          a = m_front;
          while (a-&gt;getNext() != NULL) {
              b = a;
              a = a-&gt;getNext();
          }
          b-&gt;setNext(nullptr);
          delete a;
          a = NULL;
          m_size--;
          return true;
      }
  }
  
  bool LinkedList::removeFront() {
      if (m_size == 0) {
          return false;
      }
      else {
          Node* a = new Node();
          a = m_front;
          m_front = m_front-&gt;getNext();
          delete a;
          a = NULL;
          m_size--;
          return true;
      }
  }
  std::vector&lt;int&gt; LinkedList::toVector() const {
      if (m_size == 0) {
          std::vector&lt;int&gt; b;
          return b;
      }
      else {
          std::vector&lt;int&gt; a(m_size);
          Node* b = new Node();
          b = m_front;
          for (int i = 0; i &lt; m_size; i++) {
              a[i] = b-&gt;getValue();
              b = b-&gt;getNext();
          }
          return a;
      }
  }
  </code></pre>
  
  <p>Basically, I've tested my program on my own and I've been able to make a linked list and run all my add and remove functions and print out the lists just fine. My problem is I run the test that our instructor gave us and it looks like this at the point where I'm having problems (Those print messages are in another file but all they seem to do is print the string arguments that are passed)</p>
  
  <pre><code>int score = 0;
  const int MAX_SCORE = 90;
  
  std::cerr &lt;&lt; "\n\n=========================\n";
  std::cerr &lt;&lt; "   RUNNING TEST SUITE    \n";
  std::cerr &lt;&lt; "=========================\n\n";
  
  //Run test and award points where appropriate
  score += test1() ? 2 : 0; 
  score += test2() ? 2 : 0; 
  score += test3() ? 3 : 0; 
  </code></pre>
  
  <p>This goes on for 18 tests, but my program never "makes" it past the first one. It passes the first test then all of a sudden throws an error. </p>
  
  <pre><code>bool Test_LinkedList::test1()
  {
      LinkedList list;
      bool isPassed = false;
  
      printTestMessage("size of empty list is zero"); 
  
      isPassed = list.size() == 0;
      printPassFail(isPassed);
  
      return (isPassed);
  }
  </code></pre>
  
  <p>I actually get this output before it crashes</p>
  
  <pre><code>=========================
     RUNNING TEST SUITE
  =========================
  
  Test 1: size of empty list is zero: PASSED
  </code></pre>
  
  <p>So it passes the first test but never makes it out of there. What I mean is that I have tried throwing in a cout message around</p>
  
  <pre><code>score += test1() ? 2 : 0; 
  std::cout &lt;&lt; "Done with test 1"
  score += test2() ? 2 : 0; 
  score += test3() ? 3 : 0; 
  </code></pre>
  
  <p>But that is never outputted. Instead my program breaks and Visual Studio pops up with a message saying </p>
  
  <pre><code>Exception thrown: read access violation.
  
  this was nullptr.
  
  If there is a handler for this exception, the program may be safely continued.
  </code></pre>
  
  <p>Then it points me to my method in Node.cpp that is</p>
  
  <pre><code>Node* Node::getNext() const {
      return m_next;
  }
  </code></pre>
  
  <p>Sorry, I know this is a lot of text to read through but right now I'm beyond stumped and there is no time for me to go into office hours as it is due early tomorrow morning.</p>
  
  <p>edit: i tried omitting the first test and running it. It gets through the next 6 tests but then fails on the 7th (8th) with the same exact error.</p>
  
  <pre><code>bool Test_LinkedList::test8()
  {
      LinkedList list;
      bool isPassed = false;
  
      printTestMessage("search returns false on empty list");
  
      isPassed = !list.search(42);
      printPassFail(isPassed);
      return (isPassed); 
  }
  </code></pre>