Solved

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

Posted on 2004-04-19
5
560 Views
Last Modified: 2008-03-03
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.-
0
Comment
Question by:mrestuccia
  • 3
  • 2
5 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 10861520
you should provide the line where the exceptions occurs
otherwise it is not possible to find the problem

B..M
0
 

Author Comment

by:mrestuccia
ID: 10861940
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.-
0
 
LVL 28

Expert Comment

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

B..M
0
 

Author Comment

by:mrestuccia
ID: 10862651
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!
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 250 total points
ID: 10863034
so the problem is in the constructor of  clsData
is Udl_Path dependant on something and you have to check which are the scenarious where the clsData (Udl_Path) returns Nothing

B..M
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now