Link to home
Start Free TrialLog in
Avatar of mrestuccia
mrestuccia

asked on

System.NullReferenceException: Object reference not set to an instance of an object.

All queries are performed using a class to access the DB (Access).

This is the code:

Option Strict On
Imports System.Data
Imports System.Data.OleDb
'---------------------------------------
' Class for all database activities
' Last Update: 10/30/03
'---------------------------------------
Public Class clsData
    Private mUdl_path As String

    Private m_cn As OleDbConnection
    Private m_ds As DataSet
    Private m_dr As OleDbDataReader
    Private m_da As OleDbDataAdapter
    Dim m_cmdBld As OleDbCommandBuilder

    Private WithEvents m_cmd As OleDbCommand

    Private m_msg As String
    Private mErrorState As Int32
    Private mErrorText As String
    Private mErrorEX As String
    ' Get a DataSet
    Public Overloads Function getDataSet(ByVal sql As String, ByVal updatable As Boolean, ByVal tablename As String, ByRef da As OleDbDataAdapter) As DataSet
        ' Sql - to generate the Dataset
        ' Updatable - If true, use a command builder so Datset can get updates
        Try
            Call OpenCn()
            If mErrorState <> 0 Then
                getDataSet = Nothing
                Exit Function
            End If
            m_cmd = New OleDbCommand()
            m_da = New OleDbDataAdapter(sql, m_cn)
            m_da.SelectCommand.CommandText = sql

            m_ds = New DataSet()
            If updatable Then
                m_da.FillSchema(m_ds, SchemaType.Mapped, tablename)
            End If
            m_da.Fill(m_ds, tablename)
            ' Build Update/Inser/Delete sql commands
            If updatable Then
                m_cmdBld = New OleDbCommandBuilder(m_da)
            End If
            da = m_da   ' Return the DataAdapter
            Return m_ds ' Return the DataSet
        Catch
            Throw
        Finally
            m_cmd.Dispose()
            m_cmd = Nothing
            If Not m_cn Is Nothing Then
                m_cn.Close()    ' Dont dispose as connection will be opened on save
            End If
        End Try
    End Function

    Public Overloads Function getDataSet(ByVal sql As String) As DataSet
        ' Sql - to generate the Dataset , read only
        Try
            Call OpenCn()
            If mErrorState <> 0 Then
                getDataSet = Nothing
                Exit Function
            End If
            m_cmd = New OleDbCommand()
            m_da = New OleDbDataAdapter(sql, m_cn)
            m_da.SelectCommand.CommandText = sql

            m_ds = New DataSet()
            m_da.Fill(m_ds)
            '            m_cmd = Nothing
            Return m_ds ' Return the DataSet
        Catch
            Throw
        Finally
            m_cmd.Dispose()
            m_da.Dispose()
            If Not m_cn Is Nothing Then
                m_cn.Close()
                m_cn.Dispose()
                m_cn = Nothing
            End If
        End Try

    End Function

    ' Get a DataReader
    Public Function getDataReader(ByVal sql As String) As OleDbDataReader
        Try
            Call OpenCn()
            If mErrorState <> 0 Then Exit Function
            m_cmd = New OleDbCommand()
            With m_cmd
                .Connection = m_cn
                .CommandType = CommandType.Text
                .CommandText = sql
            End With
            m_dr = m_cmd.ExecuteReader(CommandBehavior.CloseConnection)
            getDataReader = m_dr
            Exit Function
        Catch
            Throw
        Finally
            m_cmd.Dispose()
        End Try
    End Function

    ' Execute Update/Insert/Delete Commands
    Public Function ExecuteSQL(ByVal sql As String, ByVal getValue As Boolean) As Int32
        ' Sql - to generate the Dataset
        ' getvalue - Only 1 value required ( e.g. Count(*) )
        Dim nupd As Int32
        Try
            Call OpenCn()
            If mErrorState <> 0 Then Exit Function
            m_cmd = New OleDbCommand()
            With m_cmd
                .Connection = m_cn
                .CommandType = CommandType.Text
                .CommandText = sql
            End With
            If getValue Then    ' get a Value (max , min etc.)
                nupd = 0
                If Not (m_cmd.ExecuteScalar Is DBNull.Value) Then
                    nupd = CInt(m_cmd.ExecuteScalar)
                End If
            Else    ' get no of records affected (Delete/update etc.)
                nupd = m_cmd.ExecuteNonQuery()
            End If
            Return nupd
        Catch
            Throw
        Finally
            m_cmd.Dispose()
            If Not m_cn Is Nothing Then
                m_cn.Close()
                m_cn.Dispose()
                m_cn = Nothing
            End If
        End Try

    End Function
    ' Update a dataset
    Public Function save_DS(ByRef ds As DataSet, ByRef da As OleDbDataAdapter, ByVal tablename As String) As Int32
        Dim n_upd As Int32
        Dim b1 As Boolean, sqlins As String
        Dim e_msg As String
        Try
            n_upd = CInt(da.Update(ds, tablename))
        Catch
            Throw
        Finally
        End Try
    End Function

    ' Open a connection
    Private Sub OpenCn()
        Try
            mErrorState = 0
            m_cn = New OleDbConnection()
            ' get the Application Bin folder path
            'm_cn.ConnectionString = "File Name=" & mUdl_path
            m_cn.ConnectionString = mUdl_path
            m_cn.Open()
        Catch
            Throw
        Finally
        End Try
    End Sub

    Public Sub CloseConnection()
        m_cn.Close()
        m_cn.Dispose()
        Exit Sub
    End Sub
    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub

    Public Sub New(ByVal Udl_Path As String)
        mUdl_path = Udl_Path
    End Sub
End Class

The error that happens some times (not all the time):
Ex1:

System.NullReferenceException: Object reference not set to an instance of an object.
   at FDRC_Site.clsData.getDataSet(String sql, Boolean updatable, String tablename, OleDbDataAdapter& da)
   at FDRC_Site.Standard_Question.Load_Question()

Ex2:

System.NullReferenceException: Object reference not set to an instance of an object.
   at FDRC_Site.Standard_Question.Fill_Controls()


This happen sometimes, and occurs from any call to the class in any other page.

Any advice about how to fix this or at least how to install code to debug this properly will be more than welcome!

Thanks in advace,

mauro.-
Avatar of mmarinov
mmarinov

you should provide the line where the exceptions occurs
otherwise it is not possible to find the problem

B..M
Avatar of mrestuccia

ASKER

This errors happens almost all the time in the production server. I haven't manage to get line where the error occurs in production.
Can you tell me how to compile to obtain the error line in production, or it's a IIS setting?

This is one example in the local server:
System.NullReferenceException: Object reference not set to an instance of an object.
   at FDRC_Site.clsData.getDataSet(String sql, Boolean updatable, String tablename, OleDbDataAdapter& da) in C:\TSGC_DEV\FDRC_Site\clsData.vb:line 49
   at FDRC_Site.Standard_Question.Load_Question() in C:\TSGC_DEV\FDRC_Site\q_standard.aspx.vb:line 571


But I have another in a diferent line (maybe another issue?):
System.NullReferenceException: Object reference not set to an instance of an object.
   at TSGC_Site.clsData.getDataReader(String sql) in C:\TSGC_DEV\TSGC_Site\clsData.vb:line 106
   at TSGC_Site.Final.Load_Responder() in C:\TSGC_DEV\TSGC_Site\final.aspx.vb:line 163

Thanks,

mauro.-
mauro, thanks a lot, but i don't have any idea what this lines contain :-(
coudl you tell us ?

B..M
Sorry about that:

Focusing the the 1st error lines:

System.NullReferenceException: Object reference not set to an instance of an object.
   at FDRC_Site.clsData.getDataSet(String sql, Boolean updatable, String tablename, OleDbDataAdapter& da) in C:\TSGC_DEV\FDRC_Site\clsData.vb:line 49
   at FDRC_Site.Standard_Question.Load_Question() in C:\TSGC_DEV\FDRC_Site\q_standard.aspx.vb:line 571

The main page calling is:

Private Function Load_Question() As Short
        ' loads Question data into the class
        ' Load question data into the form
        Dim filter_text As String
        Dim rows As DataRow(), col As DataColumn
        Dim cls_data As clsData
        Dim strSQL As String, temp As Int32
        Load_Question = 0
        ' initial value
        gResponded = False
        Session("Responded") = gResponded

        Try
            strSQL = "SELECT Answers.* FROM Answers " & _
            " WHERE [SurveyID]=" & gSurvey & " And [ResponderID]= " & gResponder & " And [SequenceNo] = " & Q_Now

            cls_data = New clsData(Udl_Path)
            ' get a Questions DataSet and save the DataAdpter as global
            dsQstn = cls_data.getDataSet(strSQL, True, "Answers", ad_Qstn) '<----THIS IS THE LINE THAT CALLED THE CLSDATA

            cls_data = Nothing

...........

This is the function on the cls_data:

Public Overloads Function getDataSet(ByVal sql As String, ByVal updatable As Boolean, ByVal tablename As String, ByRef da As OleDbDataAdapter) As DataSet
        ' Sql - to generate the Dataset
        ' Updatable - If true, use a command builder so Datset can get updates
        Try
            Call OpenCn()
            If mErrorState <> 0 Then
                getDataSet = Nothing
                Exit Function
            End If
            m_cmd = New OleDbCommand()
            m_da = New OleDbDataAdapter(sql, m_cn)
            m_da.SelectCommand.CommandText = sql

            m_ds = New DataSet()
            If updatable Then
                m_da.FillSchema(m_ds, SchemaType.Mapped, tablename)
            End If
            m_da.Fill(m_ds, tablename)
            ' Build Update/Inser/Delete sql commands
            If updatable Then
                m_cmdBld = New OleDbCommandBuilder(m_da)
            End If
            da = m_da   ' Return the DataAdapter
            Return m_ds ' Return the DataSet
        Catch
            Throw   '<--HERE IS LINE THAT IS REPORTED IN THE ERROR
        Finally
            m_cmd.Dispose()
            m_cmd = Nothing
            If Not m_cn Is Nothing Then
                m_cn.Close()    ' Dont dispose as connection will be opened on save
            End If
        End Try
End Function

Let me know if it's easier to give you the individual lines.

The part that make this very dificult to follow, is that only happens sometimes.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of mmarinov
mmarinov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial