0

I tried creating a database that would store the answers of users but I keep getting this error System.NullReferenceException: 'Object reference not set to an instance of an object.'

  public class MySQLDatabase
{
    private SQLiteConnection conn;

    public void MyDatabase()
    {
        conn = new SQLiteConnection("mydatabase.db3");
        conn.CreateTable<MyTable>();
    }

    public void InsertData(int gameid, string preorder,string rating,string standards,string overpriced,string agerating,string gameplay,string recommend,string impact)
    {
        MyTable myNewTable = new MyTable
        {
            GameId = gameid,
            PreOrder = preorder,
            Rating = rating,
            Standards = standards,
            OverPriced = overpriced,
            AgeRating = agerating,
            Gameplay = gameplay,
            Recommend = recommend,
            Impact = impact
        };
        conn.Insert(myNewTable);
    }

    public void CloseConnection()
    {
        conn.Close();
    }
}

}

This is where i initialize it

 private void Button_Pressed(object sender, EventArgs e)
    {
        MySQLDatabase db = new MySQLDatabase();
        string q1 = Question1.ToString();
        string q2 = Question2.ToString();
        string q3 = Question3.ToString();
        string q4 = Question4.ToString();
        string q5 = Question5.ToString();
        string q6 = Question6.ToString();
        string q7 = Question7.ToString();
        string q8 = Question8.ToString();

        db.InsertData(5, q1, q8, q4, q3, q5, q6, q7, q2);
        db.CloseConnection();
    }

It seems that the table that i am trying to add to the database isn't getting populated or that's what i am getting from it.

sunboy
  • 42
  • 8
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Connor Stoop Feb 27 '23 at 12:02
  • @ConnorStoop so my error is that instead of populating my table I am just creating a new instance of it? But how do I fix it? – sunboy Feb 27 '23 at 12:16
  • Where is conn initialized? Why are you using MySQL instead of SQLite, which is the standard for on-device dbs? – Jason Feb 27 '23 at 12:30
  • @Jason I am using SQLite but i gave my class a bad name sorry about that. I also edited my question and added the whole MySQLDatabase class. – sunboy Feb 27 '23 at 12:39
  • is `public void MyDatabase()` called before `InsertData` if not `conn` will be `null` – Connor Stoop Feb 27 '23 at 12:44
  • @sunboy not in your code sample of `Button_Pressed` , `public void MyDatabase()` is not the constructor – Connor Stoop Feb 27 '23 at 12:49
  • @ConnorStoop You were right I wasn't calling MyDatabase() but now i am getting a new error that says SQLite.SQLiteException: 'Could not open database file: mydatabase.db3 – sunboy Feb 27 '23 at 12:53
  • `SQLiteConnection` does not create a db file by default look at https://stackoverflow.com/a/24179042/2111137 – Connor Stoop Feb 27 '23 at 12:58
  • @ConnorStoop How do I create one? – sunboy Feb 27 '23 at 13:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/252157/discussion-between-connor-stoop-and-sunboy). – Connor Stoop Feb 27 '23 at 13:25

1 Answers1

1

You open de db connection in public void MyDatabase() but this function is not called in Button_Pressed. You might want to change the function to be a contructor like so:

public MySQLDatabase()
{
    conn = new SQLiteConnection("mydatabase.db3");
    conn.CreateTable<MyTable>();
}

That way it is always called before you can call InsertData.

Connor Stoop
  • 1,574
  • 1
  • 12
  • 26