'Object reference not set to an instance of an object.' error.

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 4832 Views - Last Post: 06 July 2011 - 08:45 AM Rate Topic: -----

#1 dwinn86  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-June 11

'Object reference not set to an instance of an object.' error.

Posted 05 July 2011 - 07:27 AM

I keep getting an 'Object reference not set to an instance of an object.' error.

I am trying to program my application so that a user can go on to a page where a listbox displays a list of files from an FTP site.

The user should then be able to select one of the files and download it to their computer.

I assume this error is relating to the listbox I am using, but I have already decalred it.

Please help....!!

Here is my code:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="OutDirectory.aspx.vb" Inherits="FTPUploadSite.OutDirectory" %>

<%@ Import Namespace="FTPUploadSite" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    </head>
<body style="height: 769px; width: 1630px; background-color: #99CCFF; margin-top: 0px;" 
    bgcolor="#99ffcc">
    <form id="form1" runat="server">
    <div style="text-align: center; font-family: Arial; font-size: xx-large; margin-top: 0px; height: 1072px;">
    
        <br />
    
        FTP Out Directory<br />
        <br />
        <br />
        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="ftp://10.9.16.80/dwinn/In" 
            style="font-size: small; font-weight: 700; color: #000000;">View FTP Files</asp:HyperLink>
        <br />
        <br />
        <asp:ListBox ID="ListBox1" runat="server" Height="513px" Width="1111px" 
            BackColor="#99CCFF"></asp:ListBox>
        <br />
        <asp:Button ID="Button5" runat="server" Text="Download File" />
        
        &nbsp;<asp:Button ID="Button6" runat="server" Text="Delete File" />
&nbsp;&nbsp;<br />
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Back to FTP Directories" 
            Width="167px" />
&nbsp;<br />
        <br />
        <asp:Button ID="Button2" runat="server" Text="Exit the system" Width="165px" />
        
        <script runat="server" >
            
            Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

                Dim ftp As FtpWebRequest = DirectCast(WebRequest.Create("ftp://xx.xx.x.xx/In/"), FtpWebRequest)
                ftp.Method = WebRequestMethods.Ftp.ListDirectory
                Dim ftpFiles As New ArrayList()
                ftp.Credentials = New NetworkCredential("user", "password")
                Dim Response As FtpWebResponse = ftp.GetResponse()
                Dim responseStream As Stream = Response.GetResponseStream()
                Dim reader = New StreamReader(responseStream)
                While Not (reader.EndOfStream)
                    ftpFiles.Add(reader.ReadLine())
                    'ListBox1.Items.Add(ftpFiles.ToString())
                End While
                     
                ListBox1.Items.Clear()
                
                For Each file In ftpFiles
                    ListBox1.Items.Add(file)
                Next
                
                reader.Close()
                responseStream.Close()
                Response.Close()
                
            End Sub

            Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

                Response.Redirect("FTPDirectories.aspx")
                
            End Sub
            
            Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click

                Response.Redirect("UserLogin.aspx")
                
            End Sub
            
            Protected Sub Button6_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button6.Click
                
                Dim ftp As FtpWebRequest = DirectCast(FtpWebRequest.Create("ftp://xx.xx.x.xx/In/" & ListBox1.Text), FtpWebRequest)
                
                    MsgBox("STARTING...")
                    
                'If ListBox1.SelectedItem.Text = "" Then
                MsgBox("A file needs to be selected...!")
                'End If
                
                'If ListBox1.SelectedItem.Text <> "" Then
                Try
                    ftp.Credentials = New System.Net.NetworkCredential("user", "password")
                        
                    ftp.Method = WebRequestMethods.Ftp.DeleteFile
                        
                    Dim ftpResponse As FtpWebResponse = CType(ftp.GetResponse(), FtpWebResponse)
                        
                    MsgBox("GETTING THERE...")
                            
                    ftpResponse = ftp.GetResponse()
                        
                    MsgBox(ftpResponse.StatusCode, ftpResponse.StatusDescription)
                        
                    ftpResponse.Close()
                        
                    MsgBox("FINISHED...")
                Catch ex As Exception

                End Try
                'End If
                                
            End Sub
            
            Protected Sub Button5_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button5.Click

                MsgBox("STARTING...")
                
                'If Not IsPostBack Then
                
                Dim ftp As FtpWebRequest = DirectCast(FtpWebRequest.Create("ftp://xx.xx.x.xx/In/" & ListBox1.SelectedItem.Text), FtpWebRequest)
                MsgBox("STARTED...")
                If ListBox1.SelectedItem.Text = "" Then
                    MsgBox("A file needs to be selected...!")
                End If
                
                If ListBox1.SelectedItem.Text <> "" Then
                    Try
                        ftp.Credentials = New System.Net.NetworkCredential("user", "password")
                        Dim ftpResponse As FtpWebResponse = ftp.GetResponse()
                        Dim ftpStream As Stream = ftpResponse.GetResponseStream()
                        Dim ftpFile As String = ListBox1.Text
                        Dim ftpFileOutputStream As New FileStream("C:\" & ListBox1.SelectedItem.Text, FileMode.Create)
                        MsgBox("GETTING THERE...")
                        Dim ftpContentLength As Long = ftpResponse.ContentLength()
                        Dim ftpBufferSize As Integer = 2048
                        Dim ftpRead As Integer
                        Dim ftpBuffer(ftpBufferSize) As Byte
                        ftpRead = ftpStream.Read(ftpBuffer, 0, ftpBufferSize)
                        
                        While (ftpRead > 0)
                            ftpFileOutputStream.Write(ftpBuffer, 0, ftpRead)
                            ftpRead = ftpStream.Read(ftpBuffer, 0, ftpBufferSize)
                        End While
                        MsgBox("FINISHED...")
                        ftpResponse.Close()
                        ftpStream.Close()
                        ftpFileOutputStream.Close()
                        
                    Catch ex As Exception

                    End Try
                End If
                              
                'End If
            End Sub
            
        </script>

    &nbsp;&nbsp;</div>
    </form>
</body>
</html>




All help and advice will be greatly appreciated.

Thanks,

Dan

Is This A Good Question/Topic? 0
  • +

Replies To: 'Object reference not set to an instance of an object.' error.

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,293
  • Joined: 12-June 08

Re: 'Object reference not set to an instance of an object.' error.

Posted 05 July 2011 - 08:00 AM

It means some object you are creating isn't being initialized. I would suggest taking all your 'script' code and dumping that in the code behind. Then put a break point at the start of each method and step through each line to see which object is not being created correctly.
Was This Post Helpful? 0
  • +
  • -

#3 dwinn86  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-June 11

Re: 'Object reference not set to an instance of an object.' error.

Posted 05 July 2011 - 08:04 AM

When the exception is thrown, it points to this line:

Dim ftp As FtpWebRequest = DirectCast(FtpWebRequest.Create("ftp://xx.xx.x.xx/In/" & ListBox1.SelectedItem.Text), FtpWebRequest)



Dan
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,293
  • Joined: 12-June 08

Re: 'Object reference not set to an instance of an object.' error.

Posted 05 July 2011 - 08:16 AM

... and you are sure the listbox1 has a selected item?
Was This Post Helpful? 0
  • +
  • -

#5 dwinn86  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-June 11

Re: 'Object reference not set to an instance of an object.' error.

Posted 05 July 2011 - 08:18 AM

Yep, I select the file from the list and click download, that is when I get the exception thrown.

Any ideas at all?

Dan
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,293
  • Joined: 12-June 08

Re: 'Object reference not set to an instance of an object.' error.

Posted 05 July 2011 - 08:21 AM

Slap a break point and see what isn't being created.

Are you sure "ftp://xx.xx.x.xx/In/" & ListBox1.SelectedItem.Text" is an item that actually exists?
Was This Post Helpful? 0
  • +
  • -

#7 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,511
  • Joined: 16-March 11

Re: 'Object reference not set to an instance of an object.' error.

Posted 05 July 2011 - 08:21 AM

Sometimes the compiler isn't successful at finding the correct location of the error.
Was This Post Helpful? 0
  • +
  • -

#8 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: 'Object reference not set to an instance of an object.' error.

Posted 05 July 2011 - 08:27 AM

View Postdwinn86, on 05 July 2011 - 08:04 AM, said:

When the exception is thrown, it points to this line:

Dim ftp As FtpWebRequest = DirectCast(FtpWebRequest.Create("ftp://xx.xx.x.xx/In/" & ListBox1.SelectedItem.Text), FtpWebRequest)



Dan

Have you tried:
Dim ftp As FtpWebRequest = new ftpwebrequest
ftp = DirectCast(FtpWebRequest.Create("ftp://xx.xx.x.xx/In/" & ListBox1.SelectedItem.Text), FtpWebRequest)

?
Usually when this exception is thrown you just need to instantiate the object.
Was This Post Helpful? 0
  • +
  • -

#9 dwinn86  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-June 11

Re: 'Object reference not set to an instance of an object.' error.

Posted 05 July 2011 - 08:34 AM

I have tried all of the above and I just get:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

modi123_1 - I have realised I dont need the & ListBox1.SelectedItem.Text in that part of the script.

I am now getting it here:

If ListBox1.SelectedItem.Text = "" Then


Dan
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,293
  • Joined: 12-June 08

Re: 'Object reference not set to an instance of an object.' error.

Posted 05 July 2011 - 08:40 AM

Well then it looks like "ListBox1.SelectedItem" isn't a valid object.. ie there is nothing selected.
Was This Post Helpful? 0
  • +
  • -

#11 dwinn86  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-June 11

Re: 'Object reference not set to an instance of an object.' error.

Posted 05 July 2011 - 08:46 AM

So when I think I am making a selection of a file to download...in actual fact, I am not selecting anything at all?

That is why I am getting this null exception?

Dan
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,293
  • Joined: 12-June 08

Re: 'Object reference not set to an instance of an object.' error.

Posted 05 July 2011 - 08:48 AM

That's what it sounds like.

If ListBox1.SelectedItem.Text = "" Then


It's not the string.empty on the right side of the if statement... so it pretty much has to be that selecteditem, right? Again I would debug through this and put a break point to evaluate your VB code.
Was This Post Helpful? 0
  • +
  • -

#13 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: 'Object reference not set to an instance of an object.' error.

Posted 05 July 2011 - 08:57 AM

to check, I would use:
If ListBox1.Selectedindex = -1 ' -1 indicates nothing is selected



instead of:

If ListBox1.SelectedItem.Text = "" Then



Was This Post Helpful? 1
  • +
  • -

#14 dwinn86  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-June 11

Re: 'Object reference not set to an instance of an object.' error.

Posted 05 July 2011 - 09:01 AM

Btu,

I just used your code and got that message.

I assume therefore, that nothing is being selected.

Is there a way around this at all?

Dan
Was This Post Helpful? 0
  • +
  • -

#15 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: 'Object reference not set to an instance of an object.' error.

Posted 05 July 2011 - 09:48 AM

if nothing is selected, throw up a messagebox.show that tells the user to choose something in the box? Else, something is selected, so proceed.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2