Instance giving a Object Reference error
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.Compl开发者_JAVA技巧ete()
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.
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!
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.
精彩评论