0

I am getting a Nullpointer on this code:

public void InsertImportantMsg (string msg, DateTime toDay, int visible)
    {

            connection.Open ();
            string query = "UPDATE IMPORTANTMESSAGE SET MESSAGES=':msg', DATETIME=':toDay', " +
                "LABELVISIBILITY=':visible'";
            connection.CreateCommand ();
            cmd.CommandText = query;
            cmd.Parameters.Add (":msg", msg);
            cmd.Parameters.Add (":toDay", toDay);
            cmd.Parameters.Add(":visible", visible);

            cmd.ExecuteNonQuery ();

    }

Specifically at this line:

cmd.CommandText = query;

I am passing the parameters right. Is there something wrong with my update statement, that I don't realize?

(I know the code should be in try catch, but it's on purpose for now, so don't look at that)

Thanks

States
  • 141
  • 11
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Dirk Oct 28 '14 at 11:36
  • 3
    are you missing: cmd = connection.CreateCommand(); line above? – Aurimas Neverauskas Oct 28 '14 at 11:36
  • 1
    Are you *really* getting a `NullpointerException`? or are you getting a `NullReferenceException` ? – Marc Gravell Oct 28 '14 at 11:38
  • A quick use of debug mode would show which object is null and likely why. – Allan S. Hansen Oct 28 '14 at 11:38
  • dirk I know what a nullreference means, I just couldn't find it. aurimas-neverauskas thanks, that was it. Seemed so simple, but I was unable to locate it after looking at it for a long time. I don't know how I ended up deleting that – States Oct 28 '14 at 11:39

1 Answers1

1

The immediate reason of the exception is that you haven't assigned cmd value. The implementtation could be like that

public void InsertImportantMsg (string msg, DateTime toDay, int visible) {
  connection.Open();

  // Put IDisposable into using...
  using (var cmd = connection.CreateCommand()) {
    // Format query to be readble
    cmd.CommandText = 
      @"UPDATE IMPORTANTMESSAGE
           SET MESSAGES = :msg, -- <- you don't need apostrophes here...
               DATETIME = :toDay,
               LABELVISIBILITY = :visible";

    cmd.Parameters.Add(":msg", msg);
    cmd.Parameters.Add(":toDay", toDay);

    //TODO: Check this - Oracle doesn't support boolean is SQL 
    cmd.Parameters.Add(":visible", visible);  

    cmd.ExecuteNonQuery(); 
  }
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 1
    Yeah I know Oracle doesn't support boolean (unfortunately), but notice that visible is not a boolean value, but rather an int :D – States Oct 28 '14 at 11:46