Object reference not set to an instance of an object

mkreddy

Member
Joined
Aug 19, 2010
Messages
6
Programming Experience
5-10
Hi,
I generated a class library as below.
VB.NET:
Public Class Class1
    Public connString As String
    'Public Sub New()
    'End Sub


    Public Function getDBConnection() As String
        Try
            Dim connObj As Object = ConfigurationManager.ConnectionStrings("DatabaseConnectionString")
            connString = connObj.ToString

        Catch ex As Exception

        End Try
        
        'connString = ConfigurationManager.ConnectionStrings("DatabaseC onnectionString").ConnectionString
        Return connString
    End Function
End Class
To get the DB connection, I included above DLL in my form class as below but I am getting "Object reference not set to an instance of an object" Error.
VB.NET:
Partial Class _Default
    Inherits System.Web.UI.Page
    Private classObj As New DBShareLib.Class1
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Dim con As New OracleConnection(classObj.getDBConnection().ToString
        Dim dbval As String = classObj.getDBConnection().ToString
        'con.Open()
        'Response.Write("Connection is Success")
        'con.Close()

        Response.Write(dbval)
    End Sub
End Class
I would really appreciate if you could tell me the issue ??
 
Last edited by a moderator:
Please format your code snippets for readability.

As for your code, this is diabolical:
VB.NET:
        Try
            Dim connObj As Object = ConfigurationManager.ConnectionStrings("DatabaseConnectionString")
            connString = connObj.ToString

[COLOR="red"]        Catch ex As Exception

        End Try[/COLOR]
You're catching any and every exception and just ignoring it and carrying on regardless. Exactly why do you have an exception handler there in the first place? If you expect a specific exception then you should be catching that exception specifically and reacting appropriately. If you aren't expecting a specific exception then you shouldn't have an exception handler at all. Empty exception handlers are sometimes appropriate but rarely and definitely not there.

Most likely the exception you are seeing is caused by an exception you're ignoring. Web applications should use the WebConfigurationManager class rather than the ConfigurationManager class, so your attempt to get the connection string fails with an exception thrown, which you simply ignore. The function then returns Nothing becauseno value was ever retrieved and you then get a NullReferenceExecption when you try to use it. We won't know for sure until you debug properly though. Start by getting rid of that exception handler.
 
Thanks for your response!! I did try with WebConfiguration but still I am not getting the Data Source from web.config.
As per one of the threads posted here suggested to use try/catch block so i tried that.

Public Function getDBConnection() As String
Dim connObj As Object = ConfigurationManager.ConnectionStrings("DatabaseConnectionString")
'Dim conObj As Object = WebConfigurationManager.AppSettings("DatabaseConnectionString")
connString = conObj.ToString
Return connString
End Function

Thanks,
 
below is the connection string in my app.config file
<connectionStrings>
<add name="DatabaseConnectionString" connectionString="Data Source=******;User ID=*******;Password=***;"/>
</connectionStrings>
 
So, for a Windows app that would require:
VB.NET:
Dim databaseConnectionString As String = ConfigurationManager.ConnectionStrings("DatabaseConnectionString").ConnectionString
and for a web app:
VB.NET:
Dim databaseConnectionString As String = WebConfigurationManager.ConnectionStrings("DatabaseConnectionString").ConnectionString
 
Thanks a lot for your response!! and patience!! I did try as you said but it still throwing same error "Object reference not set to an instance of an object"
do I need to create any object refernce here or am I missing any step?
 
You need to be clear. EXACTLY where is the NullReeferenceException being thrown and EXACTLY what reference on that line is null, i.e. Nothing. There's no magic to this. You work out which reference is null, work out where you expected an object to be assigned to that reference and then work out why it wasn't.
 
When I am tracing/debug the code I am getting error at below statement. I am getting same error even when I use configmanager also though the databaseconnectionstring is defined in app.config as above.
Dim connString As String = WebConfigurationManager.ConnectionStrings("DatabaseConnectionString").ConnectionString
 
You say that this is a web yet you use "app.config" rather than "web.config". Which config file is the connection string in: the web application project or the library project?
 
config file is in library project. What I am trying to do is-->1: create a class library which DB connection string (app.config) and build as dll
I am referencing above dll in web application and trying to get the DB connection.
 
Or you can create the DAL library (though not sure why you insist on this) without specifying the connection string... just create a new "Class Library" project (name it ConnectionLibrary) and add a class named e.g. MyConnection. Now add a shared function of SqlConnection datatype e.g.

VB.NET:
    Public Shared Function Connection(ByVal connectionstring As String) As SqlConnection
        Dim conn As New SqlConnection(connectionstring)
        Return conn
    End Function

Now add this library as reference in the web project. To test it just say:
PHP:
        Dim connstring as String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        Dim connection As SqlConnection = ConnectionLibrary.MyConnection.Connection(connstring)
        Try
            connection.Open()
            Response.Write(connection.State.ToString)
        Catch ex As Exception
            Response.Write(ex.Message)
        Finally
            connection.Close()
        End Try

I am telling you once again; i really dont see the benefit of this architecture. I would rather put that class inside the App_Code folder and call whenever i need a new connection object. :)
 
Back
Top