views:

273

answers:

3

edit2: solution http://stackoverflow.com/questions/1710941/sqlite-and-object-reference-not-set-exception/1711481#1711481

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();
A: 

Looks like info or status might be null. What happens when you step through the code?

Randolph Potter
-1 bc i notice people have a bad habit of answering and not reading. No, neither is null as you can see in the first line of my output statement.
acidzombie24
What happens, just an exception thrown on the ExecuteNonQuery at the end of my else statement. I figure out the problem, i made new SQLiteCommand in each sql function (although a SQLiteCommand per thread should work).
acidzombie24
A: 

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.

AdamRalph
I know its not that line. That line works which proves the connection is still good. The execute non query fails but that same data works when i dont break which is just bizarre! I have other threads reading and writing to the database. Why does that matter and how does it affect this?
acidzombie24
A: 

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.

acidzombie24
As per my answer then?
AdamRalph