Click here to Skip to main content
16,004,806 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello,
I've been dealing with a NullException problem since yesterday. where I try to add values to my database accordingly to my query, but I keep getting this error:

"The object reference was not defined as an instance of an object"

What I have tried:

VB
If CheckBox1.Checked = True Then
           Try
               con.ConnectionString = "The connection is good! I've used it on select statements and it works fine!"
           Catch ex As Exception

           End Try
           Dim da As New MySqlDataAdapter()
           Dim dt As New DataTable()
           dt.Clear()
           Dim cmd As New MySqlCommand("INSERT INTO D8_ALERTAQUALIDADE(CLIENTE, PROJETO, REFERENCIA, DESCRICAO, DATA_ENTRADA, DURACAO, DATA_SAIDA, FOTO1_OK, FOTO2_OK, FOTO3_OK, FOTO4_OK, FOTO1_NOK, FOTO2_NOK, FOTO3_NOK, FOTO4_NOK) VALUES(@D1,@D2,@D3,@D4,@D5,@D6,@D7,@D8,@D9,@D10,@D11,@D12,@D13,@D14,@D15)", con)
           cmd.Parameters.AddWithValue("@d1", cmbCliente.SelectedValue)
           cmd.Parameters.AddWithValue("@d2", cmbProjeto.SelectedValue)
           cmd.Parameters.AddWithValue("@d3", cmbReferencia.SelectedValue)
           cmd.Parameters.AddWithValue("@d4", TextBox1.Text)
           cmd.Parameters.AddWithValue("@d5", dtpEntrada.Value.ToString("yyyy-mm-dd"))
           cmd.Parameters.AddWithValue("@d6", cmbMeses.SelectedItem)
           cmd.Parameters.AddWithValue("@d7", dtpDataSaida.Value.ToString("yyyy-mm-dd"))
           cmd.Parameters.AddWithValue("@d8", PictureBox1.Image)
           cmd.Parameters.AddWithValue("@d9", PictureBox2.Image)
           cmd.Parameters.AddWithValue("@d10", PictureBox3.Image)
           cmd.Parameters.AddWithValue("@d11", PictureBox4.Image)
           cmd.Parameters.AddWithValue("@d12", PictureBox5.Image)
           cmd.Parameters.AddWithValue("@d13", PictureBox6.Image)
           cmd.Parameters.AddWithValue("@d14", PictureBox7.Image)
           cmd.Parameters.AddWithValue("@d15", PictureBox8.Image)
           con.Open()
           cmd.ExecuteNonQuery()
           con.Close()
           da.Fill(dt)
       ElseIf CheckBox1.Checked = False Then
           Try
               con.ConnectionString ="The connection is good! I've used it on select statements"
           Catch ex As Exception

           End Try
           Dim da As New MySqlDataAdapter()
           Dim dt As New DataTable()
           Dim cmd As New MySqlCommand("INSERT INTO D8_ALERTAQUALIDADE(CLIENTE, PROJETO, REFERENCIA, DESCRICAO, DATA_ENTRADA, DURACAO, DATA_SAIDA, FOTO1_OK, FOTO2_OK, FOTO3_OK, FOTO4_OK, FOTO1_NOK, FOTO2_NOK, FOTO3_NOK, FOTO4_NOK) VALUES(@D1,@D2,@D3,@D4,@D5,@D6,@D7,@D8,@D9,@D10,@D11,@D12,@D13,@D14,@D15)", con)
           cmd.Parameters.AddWithValue("@d1", cmbCliente.SelectedValue)
           cmd.Parameters.AddWithValue("@d2", cmbProjeto.SelectedValue)
           cmd.Parameters.AddWithValue("@d3", cmbReferencia.SelectedValue)
           cmd.Parameters.AddWithValue("@d4", TextBox1.Text)
           cmd.Parameters.AddWithValue("@d5", dtpEntrada.Value.ToString("yyyy-mm-dd"))
           cmd.Parameters.AddWithValue("@d6", cmbUmMes.SelectedItem)
           cmd.Parameters.AddWithValue("@d7", dtpDataSaida.Value.ToString("yyyy-MM-dd"))
           cmd.Parameters.AddWithValue("@d8", PictureBox1.Image)
           cmd.Parameters.AddWithValue("@d9", PictureBox2.Image)
           cmd.Parameters.AddWithValue("@d10", PictureBox3.Image)
           cmd.Parameters.AddWithValue("@d11", PictureBox4.Image)
           cmd.Parameters.AddWithValue("@d12", PictureBox5.Image)
           cmd.Parameters.AddWithValue("@d13", PictureBox6.Image)
           cmd.Parameters.AddWithValue("@d14", PictureBox7.Image)
           cmd.Parameters.AddWithValue("@d15", PictureBox8.Image)
           con.Open()
           cmd.ExecuteNonQuery()
           con.Close()
           da.Fill(dt)
       End If
Posted
Updated 12-May-17 4:44am
Comments
Richard MacCutchan 12-May-17 6:42am    
Which line causes the error?
Scribling Doodle 12-May-17 6:51am    
on both da.Fill(dt). Now it gives me this error: "The property SelectedCommand has not been initialized before you call method Fill." Any ideas?
Richard MacCutchan 12-May-17 7:18am    
You have not read any data into the MySqlDataAdapter, so there is nothing available to put into the DataTable.
Scribling Doodle 12-May-17 8:05am    
How's that? I simply used the code from a previous program that I made and was working fine. Now on this one, it's not working idk why. want me to update my question and set the code from the other program that actually works?
Richard MacCutchan 12-May-17 8:18am    
It does not matter what your other program does. What you have coded here is just wrong, and a simple reading of the code clearly shows it.

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Comments
Scribling Doodle 12-May-17 6:45am    
forgot to mention, it gives me this error on ds.Fill(dt). I'll try out some other variables and see if anything is out of line.
Using SQLConnection As New MySqlConnection("Server=192.168.50.57;Port=3306;Database=zestagio;Uid=tiago.j.turella;Pwd=123abc.;")
    Using cmd As New MySqlCommand()
        With cmd
            .CommandText = "INSERT INTO D8_ALERTAQUALIDADE(CLIENTE, PROJETO, REFERENCIA, DESCRICAO, DATA_ENTRADA, DURACAO, DATA_SAIDA, FOTO1_OK, FOTO2_OK, FOTO3_OK, FOTO4_OK, FOTO1_NOK, FOTO2_NOK, FOTO3_NOK, FOTO4_NOK) VALUES(@D1,@D2,@D3,@D4,@D5,@D6,@D7,@D8,@D9,@D10,@D11,@D12,@D13,@D14,@D15)"
            .Connection = SQLConnection
            .CommandType = CommandType.Text
            .Parameters.AddWithValue("@d1", cmbCliente.SelectedValue)
            .Parameters.AddWithValue("@d2", cmbProjeto.SelectedValue)
            .Parameters.AddWithValue("@d3", cmbReferencia.SelectedValue)
            .Parameters.AddWithValue("@d4", TextBox1.Text)
            .Parameters.AddWithValue("@d5", dtpEntrada.Value.ToString("yyyy-mm-dd"))
            .Parameters.AddWithValue("@d6", cmbMeses.SelectedItem)
            .Parameters.AddWithValue("@d7", dtpDataSaida.Value.ToString("yyyy-MM-dd"))
            .Parameters.AddWithValue("@d8", PictureBox1.Image)
            .Parameters.AddWithValue("@d9", PictureBox2.Image)
            .Parameters.AddWithValue("@d10", PictureBox3.Image)
            .Parameters.AddWithValue("@d11", PictureBox4.Image)
            .Parameters.AddWithValue("@d12", PictureBox5.Image)
            .Parameters.AddWithValue("@d13", PictureBox6.Image)
            .Parameters.AddWithValue("@d14", PictureBox7.Image)
            .Parameters.AddWithValue("@d15", PictureBox8.Image)
        End With
        Try
            SQLConnection.Open()
            cmd.ExecuteNonQuery()

        Catch ex As MySqlException
            MsgBox(ex.Message.ToString)
        Finally

            SQLConnection.Close()
        End Try
    End Using
End Using


Came up with this solution that works pretty well.
 
Share this answer
 
Comments
Richard MacCutchan 12-May-17 11:52am    
Well that is completely different from the code in your question.
Scribling Doodle 18-May-17 7:08am    
The basis is the same. I just used some functions instead of variables and declare it manually. Using functions are way cleaner and easier to work. Glad I found a way to use it the right way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900