Object reference not set as an instance of an object. VB.NET

-2

I'm trying to launch a command from this class:

Imports System.Data.OleDb
Imports System.IO

Public Class GestionSql
    Private sReturn As String = ""
    Private sConection As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" +Directory.GetCurrentDirectory() + "\TPV.mdb;"
    Private oConection As OleDb Connection
    Public oCommand As New OleDbCommand

    Public Function Launch(ByVal value As String) As String
        Try
            oConection.Open()
            oConection = New OleDbConnection(sConection)
            oCommand.CommandText = Value
            sReturn = oCommand.ExecuteNonQuery()
            oConection.Close()
            MessageBox.Show("La contraseña es: " + sReturn.ToString)
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return sReturn
    End Function
End Class

In this class:

Public Class IDVendedor
    Private GestionSql As New GestionSql
    Private sCmd As String
    Private sPass As String

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Montamos y lanzamos el comando
        sCmd = "SELECT Password FROM Vendedores WHERE Id=" + id.Text
        sPass = GestionSql.Launch(sCmd)
    End Sub
End Class

I miss the error:

  

Reference to object not established as an instance of an object. It tells me that the error is in ExecuteNonQuery in the sCmd parameter.

    
asked by Adrian Hernando Solanas 10.04.2018 в 01:44
source

3 answers

1

The error Referencia a objeto no establecida como instancia de un objeto will accompany you for many years in the world of programming, but before long you will get used to know immediately where is the error:)

What does that message mean?

You are reading a property or executing a function of an object variable ("object reference") that has not been initialized ("not set as an instance of an object"). That is, you declared a variable and used it without assigning it a value before.

The error appears on line 15 of your capture, because you have inverted the initialization of variable oConexion (line 16) and the first use (line 15):

oConection.open()
oConection = New OleDbConnection(sConection) 

To solve, you just have to initialize the variable oConexion before opening the connection:

oConection = New OleDbConnection(sConection) 
oConection.open()
    
answered by 10.04.2018 / 03:28
source
1

In the code to what I see you are trying to use oConection before initializing it, in theory your problem could be solved by changing this:

    oConection.Open()
    oConection = New OleDbConnection(sConection) 

for this:

    oConection = New OleDbConnection(sConection)//primero creas el objeto
    oConection.Open()//despues lo utilizas

Explanation: oConection is equal to null when being null or not being initialized, you can not access its methods or attributes, therefore before calling .open() you need to assign a value when using =new create a new instance of this object and that is how you avoid the error of Uninitialized object .

What I see you need to initialize oConection, but you would have to validate it, check the image to see if it can help you in something.

    
answered by 10.04.2018 в 03:57
0

In the end I solved it in the following way:

Imports System.Data.OleDb
Imports System.IO

Public Class GestionSql
Private sReturn As String = ""
Private sConection As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Directory.GetCurrentDirectory() + "\TPV.mdb;"
Private oConection As New OleDbConnection(sConection)
Public oCommand As New OleDbCommand
'Protected daAdapter As New SqlDataAdapter(sCmd, oConection)
'Protected dsDataSet As New DataSet

'Lanzar comandos con respuesta (SELECT)
Public Function Launch(ByVal value As String) As String
    Try
        oConection.Open()
        oCommand.CommandText = value
        oCommand.CommandType = CommandType.Text
        oCommand.Connection = oConection
        sReturn = oCommand.ExecuteScalar()
        oConection.Close()
        MessageBox.Show("La contraseña es: " + sReturn.ToString)
    Catch ex As Exception
        MessageBox.Show(ex.ToString)
    End Try
    Return sReturn
End Function
End Class
    
answered by 10.04.2018 в 04:36