user34537
user34537

Reputation:

SQLite and `Object reference not set` exception

edit2: solution SQLite and `Object reference not set` exception

I am having a hard time understanding my error.

edit1: I set my define to make my code single threaded. The problem went away. So it seems like a race condition.

I get the error below. But not always, i notice that if i do not set a break or i step through them quickly i get no exception. When set a breakpoint at var o = command.ExecuteScalar(); or the line before it and wait 10+ seconds (i used the system clock to check, not counting) it will ALLWAYS get an exception (i tried it twice however based on what i notice the exception happens only when i break for more then a few seconds).

I dont understand WHY i am getting the error. I printed out both the sql statement and the param values i fed it and i can see its correct values. Whats going on!?! and what bothers me is the COUNT(*) works but the insert doesnt.

Here is my code

        else
        {
            command.CommandText = "SELECT COUNT(*) FROM link_list;";
            var o = command.ExecuteScalar();
            int status = (int)r.status;
            command.CommandText = "UPDATE link_list SET status=@status WHERE id=@id;";
            command.Parameters.Add("@status", System.Data.DbType.Byte).Value = status;
            command.Parameters.Add("@id", System.Data.DbType.Int32).Value = info.linkId;
            Console.WriteLine("CommandText {0} {1} {2}", command.CommandText, status, info.linkId);
            command.ExecuteNonQuery();
            Console.WriteLine("CommandText no exception");
        }

elsewhere

        catch(Exception e)
        {
            Console.WriteLine(e.Message);
            Console.WriteLine(e.StackTrace);

My output

CommandText UPDATE link_list SET status=@status WHERE id=@id; 5 108
The thread '<No Name>' (0xbc8) has exited with code 0 (0x0).
A first chance exception of type 'System.NullReferenceException' occurred in System.Data.SQLite.dll
Object reference not set to an instance of an object.
   at System.Data.SQLite.SQLiteDataReader.NextResult()
   at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
   at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()
   at WebDLManager.DB.updateStatus(DLInfo info, ReturnVal r) in C:\dev\source\prvTrunk\WebDLManager\WebDLManager\db.cs:line 134
   at WebDLManager.SiteBase.threadStart() in C:\dev\source\prvTrunk\WebDLManager\WebDLManager\SiteType.cs:line 241
The program '[1360] WebDLManager.vshost.exe: Managed' has exited with code 0 (0x0).

As requested

        //this is called through form_load
        connection = new SQLiteConnection("Data Source=mydb.sqlite3;Version=3");
        command = new SQLiteCommand(connection);
        connection.Open();

Upvotes: 0

Views: 5452

Answers (2)

user34537
user34537

Reputation:

If its multithreaded and command is being shared with other threads there is going to be a problem. Even if the output shows the sql being correct there still may be a chance two threads using the same SQLiteCommand at one time.

Upvotes: 0

Adam Ralph
Adam Ralph

Reputation: 29956

Look at your stack trace. The first call in the SQLite assembly is a call to System.Data.SQLite.SQLiteCommand.ExecuteNonQuery() so the exception is not occurring on the line which says var o = command.ExecuteScalar(). Are you sure you don't have another thread running which is calling SQLiteCommand.ExecuteNonQuery()?

UPDATE (with reference to comment)

One of your other threads must be causing the exception. You need to examine the exact state of your objects prior to that call and it should become apparent which reference is erronously set to null.

Upvotes: 2

Related Questions