tags:

views:

29

answers:

2

Hi there,

I've written a new DAL and BLL, but when I try to create an instance of my class, I get an Object Reference error, is there anything particular I should be looking for? I am fairly new to this concept?

Call is such:

Protected Sub btnSignin_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnSignin.Click
    Dim l As New Log()
    l.Log = "Attempted staff login with username [" & txtUsername.Text & "] and password [" & txtPassword.Text & "]"
    l.LogId = 0
    l.StaffId = 0
    l.LogDate = Date.Now()
    l.Insert()

The 'Insert' call which is where I get the reference error, calls these functions in my BLL (BLL/Log.vb);

Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports System.Transactions
Imports System.Text
Imports Harmony.Zizz.DAL
Namespace Harmony.Zizz.BLL.Zizz
    Public Class Log
        Inherits BaseZizz

        Protected _logid As Integer = 0
        Protected _log As String = ""
        Protected _staffid As Integer = 0
        Protected _logdate As DateTime = Date.Now

        Public Sub New()

        End Sub

        Public Sub New(ByVal logid As Integer, ByVal log As String, ByVal staffid As Integer, ByVal logdate As DateTime)

        End Sub

'////property setup removed for brevity ////
        Public Property LogId() As Integer
        Public Property Log() As String
        Public Property LogDate() As DateTime

        Public Function Insert() As Integer
            Return Harmony.Zizz.BLL.Zizz.Log.InsertLog(Me.LogId, Me.Log, Me.StaffId, Me.LogDate)
        End Function

        Public Shared Function InsertLog(ByVal logid As Integer, ByVal log As String, ByVal staffid As Integer, ByVal logdate As DateTime) As Integer
            HttpContext.Current.Trace.Write("Fired InsertLog BLL (log.vb) Line 61")
            Using scope As New TransactionScope()
                HttpContext.Current.Trace.Write("Within InsertLog BLL Transaction Scope (log.vb) Line 63")
                Dim record As New LogDetails(logid, log, staffid, logdate)
                HttpContext.Current.Trace.Write("Created LogDetails Object InsertLog BLL (log.vb) Line 65")
                Dim ret As Boolean = SiteProvider.Zizz.InsertLog(record)
                HttpContext.Current.Trace.Write("Inserted LogDetails object into SiteProvider InsertLog BLL (log.vb) Line 61")
                scope.Complete()
                Return ret
            End Using
        End Function

    End Class
End Namespace

The stack trace gets to the Dim ret As Boolean = SiteProvider line. The SiteProvider calls contains this code, which is where I think the error originates, but have no idea why - or how to fix --- :

Imports Microsoft.VisualBasic
Namespace Harmony.Zizz.DAL
    Public NotInheritable Class SiteProvider
        Public Shared ReadOnly Property Zizz() As ZizzProvider
            Get
                Return ZizzProvider.Instance
            End Get
        End Property
    End Class
End Namespace

Help, if able to be given, would be hugely appreciated. Intellisense highlights no problems and if I Build the web site in VS I get a few warnings about schema information in the web.config (unrelated), otherwise no problem.

Help appreciated.

+1  A: 

If you put a breakpoint on the line

Return ZizzProvider.Instance

you will most likely see that either ZizzProvider or ZizzProvider.Instance is null, so back on the line SiteProvider.Zizz.InsertLog(record) the InsertLog cannot be called because Zizz is null.

slugster
+1  A: 

Chris,

I agree with slugster in his answer that it is probably the fact that ZizzProvider.Instance is Nothing. This is most likely due the the way it is implemented. I assume Instance is a property, if so, then check this property (in the property block) when returning the instance, check first to see if the instance is Nothing - if it is, new up an instance and store it off in the class before returning it. If you can give us the code to this ZizzProvider, we might be able to help a bit further.

I hope this helps,

Thanks!

Scott
Hi Scott, thanks for the reply - you too Slugster. This is my Instance() creation code and where I get the nullReferenceException. There is a class called Globals, which contains a Public ReadOnly Property called Settings, this in turn reads the web.config Hotel element and Providertype child.'_instance = Ctype(Activator.CreateInstance(Type.GetType(Globals.Settings.Hotel.Providertype)), ZizzProvider)' -- any clues?
Chris Laythorpe
Got it solved. Many thanks to both Slugster and you Scott. The key lesson here is; don't ever have classes, namespaces or propertys of the same name!
Chris Laythorpe