Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-04-19
5
Medium Priority
?
625 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 750 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

636 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