0

please tell me how to fix the exception: "NullReferenceException: Object reference not set to an instance of an object". I'm trying to make a request, I wrote it like this:

 public void conn()
{
    path = Application.dataPath + "/StreamingAssets/db.bytes";
    con_db = new SqliteConnection("URI=file:" + path);
    con_db.Open();
    if (con_db.State == ConnectionState.Open)
    {

        string nameP= Input.text.ToString();
        dbcmd.Connection = con_db;
        dbcmd = new SqliteCommand("Select Name from Player where Name=='" + nameP+ "'");
        rdr = dbcmd.ExecuteReader();
        if (rdr.HasRows & rdr != null)
        {
            while (rdr.Read())
            {
                string n = rdr[0].ToString();
                if (n != "")
                {
                   Debug.Log("data received");
                }
                else
                {
                    Debug.Log("no data received");
                }
            }
        }
    }
    else
    {
        Debug.Log("Connection error");
    }
}

but in the Nick_db line.Connection = con_db; this exception occurs.

Avgust Kuk
  • 127
  • 7
  • 3
    Welcome to Stack Overflow. shouldn't ```dbcmd.Connection = con_db;``` be after the definition of ```dbcmd```? or have you defined dbcmd before and you are reusing it? – ewokx May 13 '22 at 05:21
  • 1
    put `dbcmd.Connection = con_db;` 1 line lower after `dbcmd = new SqliteCommand`. You are trying to eat the egg before the chicken has laid it... ;) – Mong Zhu May 13 '22 at 05:43

0 Answers0