title: Debug Assertion Failed: Invalid Null Pointer
body: <p>I am attempting to make a test program to see if an idea I had for getting and storing data from an SQLite3 database in a struct would work but I am running into some major problems with the code.  While debugging I keep running into the error in the title.  Here is the full text of the error window:</p>
  
  <blockquote>
    <p>Debug Assertion Failed!</p>
    
    <p>File: include\xstring
    Line: 929</p>
    
    <p>Expression: invalid null pointer</p>
    
    <p>For information on how your program can cause an assertion
    failure, see the Visual C++ documentation on asserts.</p>
  </blockquote>
  
  <p>And here is the code, I will indicate which line the problem is with an arrow (&lt;--; extra help see case 2 of the switch):</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "data_placeholder.h"
  #include "sqlite3.h"
  #include &lt;vector&gt;
  #include &lt;conio.h&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  
  using namespace std;
  
  void openDB(sqlite3* dBase, int iID, string table, string operation, sqlite3_stmt* statement, vector&lt;mission&gt;&amp; mission_1);
  void createStatement(int iID, string table, string operation, sqlite3_stmt* statement, sqlite3* dBase, vector&lt;mission&gt;&amp; mission_1);
  void getMResults(string sqlStr, sqlite3_stmt* statement, sqlite3* dBase, vector&lt;mission&gt;&amp; mission_1);
  void returnMResult(vector&lt;mission&gt; mResults, vector&lt;mission&gt;&amp; mission_1);
  
  int main()
  {
      //Define Variables
      vector&lt;mission&gt; mission_1;
      sqlite3 *dBase;
      sqlite3_stmt *statement;
      int pInput;
  
      mission_1.push_back(mission());
  
      cout &lt;&lt; "Input a number between 1 and 3" &lt;&lt; endl;
      cout &lt;&lt; "&gt;";
      cin &gt;&gt; pInput;
      cout &lt;&lt; endl;
      cout &lt;&lt; endl;
  
      openDB(dBase, pInput, "Mission_Data", "select from", statement, mission_1);
  
      cout &lt;&lt; mission_1.at(0).mName &lt;&lt; ", " &lt;&lt; mission_1.at(0).mDesc &lt;&lt; ", " &lt;&lt; mission_1.at(0).mCBELevel &lt;&lt; ", " &lt;&lt; mission_1.at(0).mSCReq &lt;&lt; ", " &lt;&lt; mission_1.at(0).mMWReq &lt;&lt; ", " &lt;&lt; mission_1.at(0).mTLimit &lt;&lt; ", " &lt;&lt; mission_1.at(0).mDifficulty &lt;&lt; ", " &lt;&lt; mission_1.at(0).mSector &lt;&lt; ", " &lt;&lt; mission_1.at(0).mSystem &lt;&lt; ", " &lt;&lt; mission_1.at(0).mTName &lt;&lt; ", " &lt;&lt; mission_1.at(0).mTSClass &lt;&lt; ", " &lt;&lt; mission_1.at(0).mBounty &lt;&lt; ", " &lt;&lt; mission_1.at(0).mXarn &lt;&lt; ", " &lt;&lt; mission_1.at(0).mRubies &lt;&lt; ", " &lt;&lt; mission_1.at(0).mDiamonds &lt;&lt; ", " &lt;&lt; mission_1.at(0).mDraconic &lt;&lt; ", " &lt;&lt; mission_1.at(0).mLithium &lt;&lt; ", " &lt;&lt; mission_1.at(0).mPlatinum &lt;&lt; ", " &lt;&lt; mission_1.at(0).mUranium &lt;&lt; ", " &lt;&lt; mission_1.at(0).mPlutonium &lt;&lt; ", " &lt;&lt; mission_1.at(0).mNWaste &lt;&lt; ", " &lt;&lt; mission_1.at(0).mCEXP &lt;&lt; ", " &lt;&lt; mission_1.at(0).mItem &lt;&lt; ", " &lt;&lt; mission_1.at(0).mType &lt;&lt; ", " &lt;&lt; endl;
  
      _getch();   
  }
  
  void openDB(sqlite3* dBase, int iID, string table, string operation, sqlite3_stmt* statement, vector&lt;mission&gt;&amp; mission_1)
  {
      sqlite3_open("scDatabase.sqlite",&amp;dBase);
  
      createStatement(iID, table, operation, statement, dBase, mission_1);
  }
  
  void createStatement(int iID, string table, string operation, sqlite3_stmt* statement, sqlite3* dBase, vector&lt;mission&gt;&amp; mission_1)
  {
      stringstream ss;
      ss &lt;&lt; iID;
  
      string sID(ss.str());
  
      string sqlStr = "Select * From " + table + " Where ID = " + sID;
  
      getMResults(sqlStr, statement, dBase, mission_1);
  }
  
  void getMResults(string sqlStr, sqlite3_stmt* statement, sqlite3* dBase, vector&lt;mission&gt;&amp; mission_1)
  {
      vector&lt;mission&gt; mResults;
  
      mResults.push_back(mission());
  
      if (sqlite3_prepare_v2(dBase, sqlStr.c_str(), sqlStr.size(), &amp;statement, 0) == SQLITE_OK)
      {   
          int cols;
          int i;
          cols = sqlite3_column_count(statement);
  
          for (i =01; i &lt;= cols; i++)
          {
              switch(i)
              {
              case 2:
                  mResults.at(0).mName = string((char*)sqlite3_column_text(statement,i)); //&lt;-- Here is the line the assert fail happens
                  break;
              /*
              case 3:
                  mResults.at(0).mDesc = string(reinterpret_cast&lt;const char*&gt;(sqlite3_column_text(statement,i)));
                  break;
  
              case 4:
                  mResults.at(0).mCBELevel = sqlite3_column_int(statement,i);
                  break;
  
              case 5:
                  mResults.at(0).mSCReq = string(reinterpret_cast&lt;const char*&gt;(sqlite3_column_text(statement,i)));
                  break;
  
              case 6:
                  mResults.at(0).mMWReq = string(reinterpret_cast&lt;const char*&gt;(sqlite3_column_text(statement,i)));
                  break;
  
              case 7:
                  mResults.at(0).mTLimit = sqlite3_column_int(statement,i);
                  break;
  
              case 8:
                  mResults.at(0).mDifficulty = string(reinterpret_cast&lt;const char*&gt;(sqlite3_column_text(statement,i)));
                  break;
  
              case 9:
                  mResults.at(0).mSector = string(reinterpret_cast&lt;const char*&gt;(sqlite3_column_text(statement,i)));
                  break;
  
              case 10:
                  mResults.at(0).mSystem = string(reinterpret_cast&lt;const char*&gt;(sqlite3_column_text(statement,i)));
                  break;
  
              case 11:
                  mResults.at(0).mTName = string(reinterpret_cast&lt;const char*&gt;(sqlite3_column_text(statement,i)));
                  break;
  
              case 12:
                  mResults.at(0).mTSClass = string(reinterpret_cast&lt;const char*&gt;(sqlite3_column_text(statement,i)));
                  break;
  
              case 13:
                  mResults.at(0).mBounty = sqlite3_column_int(statement,i);
                  break;
  
              case 14:
                  mResults.at(0).mXarn = sqlite3_column_int(statement,i);
                  break;
  
              case 15:
                  mResults.at(0).mRubies = sqlite3_column_int(statement,i);
                  break;
  
              case 16:
                  mResults.at(0).mDiamonds = sqlite3_column_int(statement,i);
                  break;
  
              case 17:
                  mResults.at(0).mDraconic = sqlite3_column_int(statement,i);
                  break;
  
              case 18:
                  mResults.at(0).mLithium = sqlite3_column_int(statement,i);
                  break;
  
              case 19:
                  mResults.at(0).mPlatinum = sqlite3_column_int(statement,i);
                  break;
  
              case 20:
                  mResults.at(0).mNWaste = sqlite3_column_int(statement,i);
                  break;
  
              case 21:
                  mResults.at(0).mCEXP = sqlite3_column_int(statement,i);
                  break;
  
              case 22:
                  mResults.at(0).mItem = sqlite3_column_int(statement,i);
                  break;
  
              case 23:
                  mResults.at(0).mType = string(reinterpret_cast&lt;const char*&gt;(sqlite3_column_text(statement,i)));
                  break;
                  */
  
              default:                
                  break;
              }
          }
      }
  
      else
      {
          cout &lt;&lt; "something is wrong" &lt;&lt; endl;
      }
  
      returnMResult(mResults, mission_1);
  }
  
  void returnMResult(vector&lt;mission&gt;mResults, vector&lt;mission&gt;&amp; mission_1)
  {
      mission_1.at(0) = mResults.at(0);
  }
  </code></pre>
  
  <p>The error occurs instantly when the code hits this line on the first iteration through the for loop.  There are no compiler errors and I have also tried collapsing the first three functions into one function in case the database and statement pointers were not being passed correctly; same problem.</p>
  
  <p>Edit 2: I have whittled down where the problem is.  It has to do with my vector of structs.  I took the database query out of the link where I set to mResults.at(0).mName and then added a cast for the const unsigned char to string but the assert failure still happens.</p>
  
  <p>Edit 3: After looking at some code I had done earlier in the the year I have figure out what was going on, at least for the SQLite query.  You have to called step in order for the query to actually be carried out.  Since I had not done so the pointer was always returning as invalid since there was no row loaded and thus no columns to query.  Nothing wrong with my vector.</p>