Thursday, March 29, 2012

object ref not set to an instance of an object

I am trying to run a sub in my class, my class imports ..

Imports BinaryIntellect.ZipUtils.ZipFileHelper

and my function is....

Public Sub saveScorm(ByVal source As String, ByVal destination As String)
Try
Dim paths = Server.MapPath("/ilearnscorm/")
paths = paths & "scormCourses/"
Dim ZipFileHelper As New BinaryIntellect.ZipUtils.ZipFileHelper
ExtractZipFile(source, paths & destination) 'unzip the files into directory
Catch ex As Exception
Throw ex.InnerException
End Try
End Sub

Any ideas?
ThanksAnd where is the exception thrown from?

Simple explanation of the "Object reference not set to an instance of an object" exception: it refers to a (class) variable that has not been instantiated - i.e. = New <something>. All you really need do is find that variable - which is where the stack trace helps significantly.

Hmm, you have a variable - ZipFileHelper - that is declared/instantiated but not used; why do you have it?

What's in the "ExtractZipFile" method? Relevant to the stack and debugging.

As you are doing nothing with the exception - just remove the try/catch. You're bubbling the exception (or more specifically the inner - never mind any useful information that may be included in the parent exception ;) ) anyway. This changes when you do something with the exception, for example logging it.
Here is my stack trace..
The extractZipFile method expects two parameters, the source and the destination directories

[NullReferenceException: Object reference not set to an instance of an object.]
_Default.btnSubmit_Click(Object sender, EventArgs e) +766
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
If I create an instance I get ....
Access of shared member, constant member, enum member or nested type through an instance
If you look at the stack trace, you'll see that the first entry is "_Default.btnSubmit_Click(Object sender, EventArgs e) +766". This tells me that the exception occurred in your Default.aspx page, in the btnSubmit_Click event handler and that you're not running in debug mode.

So, change the solution build to Debug, deploy (if necessary), run the code again and check the line number that the stack trace gives you; it will be at the end of the line and will replace the "+766" part, e.g. "_Default.btnSubmit_Click(Object sender, EventArgs e) 123"
I'm willing to bet it's occurring inside the ExtractZipFile method. Step into it and watch where it fails in there.

0 comments:

Post a Comment