NullReferenceException makes me want to shoot myself
Posted
by
rockinthesixstring
on Stack Overflow
See other posts from Stack Overflow
or by rockinthesixstring
Published on 2010-12-29T23:47:32Z
Indexed on
2010/12/29
23:54 UTC
Read the original article
Hit count: 243
Ok, so once upon a time, my code worked. Since then I did some refactoring (basic stuff so I thought) but now I'm getting a null reference exception, and I can't bloody well figure out why.
Here's where it starts. note, this is the logout method, but all of my ActivityLogs sections are throwing this error
Function LogOut(ByVal go As String) As ActionResult
ActivityLogService.AddActivity(AuthenticationHelper.RetrieveAuthUser.ID, _
IActivityLogService.LogType.UserLogout, _
HttpContext.Request.UserHostAddress)
ActivityLogService.SubmitChanges()
''# more stuff happens after this
End Function
The service is pretty straight forward (notice the ERROR THROWN HERE)
Public Sub AddActivity(ByVal userid As Integer, ByVal activity As Integer, ByVal ip As String) Implements IActivityLogService.AddActivity
Dim _activity As ActivityLog = New ActivityLog With {.Activity = activity,
.UserID = userid,
.UserIP = ip.IPAddressToNumber,
.ActivityDate = DateTime.UtcNow}
ActivityLogRepository.Create(_activity) ''#ERROR THROWN HERE
End Sub
And the Interface that the Service uses looks like this
Public Interface IActivityLogService
Sub AddActivity(ByVal userid As Integer, ByVal activity As Integer, ByVal ip As String)
Function GetUsersLastActivity(ByVal UserID As Integer) As ActivityLog
Sub SubmitChanges()
''' <summary>
''' The type of activity done by the user
''' </summary>
''' <remarks>Each int refers to an activity.
''' There can be no duplicates or modifications
''' after this application goes live</remarks>
Enum LogType As Integer
''' <summary>
''' A new session started
''' </summary>
SessionStarted = 1
''' <summary>
''' A new user is Added/Created
''' </summary>
UserAdded = 2
''' <summary>
''' User has updated their profile
''' </summary>
UserUpdated = 3
''' <summary>
''' User has logged into they system
''' </summary>
UserLogin = 4
''' <summary>
''' User has logged out of the system
''' </summary>
UserLogout = 5
''' <summary>
''' A new event has been added
''' </summary>
EventAdded = 6
''' <summary>
''' An event has been updated
''' </summary>
EventUpdated = 7
''' <summary>
''' An event has been deleted
''' </summary>
EventDeleted = 8
''' <summary>
''' An event has received a Up/Down vote
''' </summary>
EventVoted = 9
''' <summary>
''' An event has been closed
''' </summary>
EventCloseVoted = 10
''' <summary>
''' A comment has been added to an event
''' </summary>
CommentAdded = 11
''' <summary>
''' A comment has been updated
''' </summary>
CommentUpdated = 12
''' <summary>
''' A comment has been deleted
''' </summary>
CommentDeleted = 13
''' <summary>
''' An event or comment has been reported as spam
''' </summary>
SpamReported = 14
End Enum
End Interface
And the repository is equally straight forward
Public Sub Create(ByVal activity As ActivityLog) Implements IActivityLogRepository.Create
dc.ActivityLogs.InsertOnSubmit(activity)
End Sub
The stack trace is as follows
[NullReferenceException: Object reference not set to an instance of an object.] MyApp.Core.Domain.ActivityLogService.AddActivity(Int32 userid, Int32 activity, String ip) in E:\Projects\MyApp\MyApp.Core\Domain\Services\ActivityLogService.vb:49 MyApp.Core.Controllers.UsersController.Authenticate(String go) in E:\Projects\MyApp\MyApp.Core\Controllers\UsersController.vb:258 lambda_method(Closure , ControllerBase , Object[] ) +163 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +51 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary
2 parameters) +409 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2 parameters) +52 System.Web.Mvc.<>c_DisplayClass15.b_12() +127 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func1 continuation) +436 System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +61 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList
1 filters, ActionDescriptor actionDescriptor, IDictionary2 parameters) +305 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +830 System.Web.Mvc.Controller.ExecuteCore() +136 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +232 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +39 System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +68 System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +44 System.Web.Mvc.Async.<>c__DisplayClass8
1.b__7(IAsyncResult ) +42 System.Web.Mvc.Async.WrappedAsyncResult`1.End() +141 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +54 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 System.Web.Mvc.<>c_DisplayClasse.b_d() +61 System.Web.Mvc.SecurityUtil.b_0(Action f) +31 System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +56 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +110 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +690 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +194
here's an image of the error when attached to the debugger.
And here's an image of the DB schema in question
Can anyone shed some light on what I might be missing here?
© Stack Overflow or respective owner