Showing posts with label codedim. Show all posts
Showing posts with label codedim. Show all posts

Monday, March 26, 2012

Object reference not set to an instance of an object

HI,

I am getting a terrible error and I don't know how to fix it. Can anyone give me any input?

Here is my code:

Dim strFileExtension As String
Dim strFileType As String
Dim intFileLen As Integer
Dim objStream As Stream

strFileType = fileUps.PostedFile.contenttype (error is here)

<input id="fileUps" Type="File" Runat="Server">Try something like:

Dim myFile As HttpPostedFile = fileUps.PostedFile
Dim nFileLen As Integer = myFile.ContentLength

If nFileLen > 0 Then

Try
Dim myData(nFileLen) As Byte
myFile.InputStream.Read(myData, 0, nFileLen)
Dim strFilename As String = Path.GetFileName(myFile.FileName)
Dim newFile As FileStream = New FileStream(Server.MapPath("Imports\" & strFilename), FileMode.Create)
newFile.Write(Buffer, 0, Buffer.Length)
newFile.Close()
Catch ex As Ex as Exception
lblMsg.Text = "Failed to upload the file!"
End Try

End If

MajorCats
Your code was very helpful, but I still receive the same error. I received the same error with
Dim nFileLen As integer = myFile.ContentLength

Do you recommend anything else?

Thursday, March 22, 2012

Object reference not set to an instance of an object.

I get the error: Object reference not set to an instance of an object

With this code:

Dim test3 As New TextBox
test3 = CType(e.Item.FindControl("name"), TextBox)
Dim name As String = test3.Text

The specific line is:

Dim name As String = test3.Text

So, what do I need to do to fix that?

Thanks.Because e.Item.FindControl() does not find a control named as "name", hence
next line of code gives you that error.

You need to figure out why there is no TextBox, called "name" found.

"Jim in Arizona" <tiltowait@.hotmail.comwrote in message
news:ePOMLuz1GHA.4300@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

>I get the error: Object reference not set to an instance of an object
>
With this code:
>
Dim test3 As New TextBox
test3 = CType(e.Item.FindControl("name"), TextBox)
Dim name As String = test3.Text
>
The specific line is:
>
Dim name As String = test3.Text
>
So, what do I need to do to fix that?
>
Thanks.


Yep. That's it. Thanks. Now I have another question to post regarding
this whole mess, which I bet no one will have an answer for.

Thanks Norman.

Norman Yuan wrote:

Quote:

Originally Posted by

Because e.Item.FindControl() does not find a control named as "name", hence
next line of code gives you that error.
>
You need to figure out why there is no TextBox, called "name" found.
>
"Jim in Arizona" <tiltowait@.hotmail.comwrote in message
news:ePOMLuz1GHA.4300@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

>I get the error: Object reference not set to an instance of an object
>>
>With this code:
>>
> Dim test3 As New TextBox
> test3 = CType(e.Item.FindControl("name"), TextBox)
> Dim name As String = test3.Text
>>
>The specific line is:
>>
> Dim name As String = test3.Text
>>
>So, what do I need to do to fix that?
>>
>Thanks.


>
>