So I've been working on this FTP server client using Howard Richards FTP client library. After a while the program just stopped logging me into the FTP server and is giving me the "Object reference not set to an instance of an object." error each time i click the button which is supposed to log me in or show my FTP files. The error happens in "For Each File In FTPDownload.ListDirectory" but that was never a problem. I dont know what happened. Someone PLEASE help me?
PS. I've checked the FTP Server I'm using in the browser to see if it works logged on to my account and it is so there is nothing wrong there.
Code Below:

Imports System.Net
Imports Utilities.FTP
Public Class Form1
    Private FTPDownload As New Utilities.FTP.FTPclient
    Private isFolder As Boolean = False
    Private isFolder1 As Boolean = False
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Button4.Enabled = False
        Button5.Enabled = False
        Button7.Enabled = False
        Button1.Enabled = False
        Button2.Enabled = False
        Button6.Enabled = False
        Button8.Enabled = False
        Timer1.Start()
        UpdateLabelDirectory()
        TextBox4.Enabled = False
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        FTPDownload.CurrentDirectory = "/"
        RefreshList()
        UpdateLabelDirectory()

    End Sub
    'Really Important: Adds Item in Listbox
    Private Sub RefreshList()
        ListView1.Items.Clear()
        'Try
        For Each File In FTPDownload.ListDirectory
            Dim ext As String = IO.Path.GetExtension(File)
            ListView1.Items.Add(New ListViewItem({File, ext}))
            ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
            TextBox4.Enabled = True
            Button1.Enabled = True
            Button2.Enabled = True
            Button6.Enabled = True
            Button8.Enabled = True
        Next
        'Catch ex As Exception
        '    MsgBox(ex.Message)
        'End Try
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        FTPDownload.CurrentDirectory += ListView1.FocusedItem.Text + "/"
        RefreshList()
        UpdateLabelDirectory()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If String.IsNullOrEmpty(TextBox4.Text) Then
            Button7.Enabled = False
        Else
            Button7.Enabled = True
        End If
        Try
            Label4.Text = ListView1.FocusedItem.Text.ToString
        Catch ex As Exception

        End Try
        For Each lvi In ListView1.Items
            Dim masterItem = From i In ListView1.Items.OfType(Of ListViewItem)()
                             Where i.Text = "."
            Dim masterItem1 = From i In ListView1.Items.OfType(Of ListViewItem)()
            Where i.Text = ".."

            If masterItem.Any Then
                ListView1.Items.Remove(masterItem.First)
                ListView1.Items.Remove(masterItem1.First)
            End If
        Next
    End Sub
    Private Sub UpdateLabelDirectory()
        Label7.Text = FTPDownload.CurrentDirectory
    End Sub
    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

        Dim file As String = ListView1.FocusedItem.Text
        Try
            If ListView1.FocusedItem.Index <> -1 And file.Contains(".") Then
                FTPDownload.FtpDelete(ListView1.FocusedItem.Text)
                RefreshList()
            Else
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        FTPDownload.Hostname = TextBox3.Text.Trim
        FTPDownload.Username = TextBox1.Text
        FTPDownload.Password = TextBox2.Text

        RefreshList()
    End Sub
    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click

        Try
            FTPDownload.FtpCreateDirectory(TextBox4.Text)
            RefreshList()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
    Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
        Dim Selected As String = ListView1.FocusedItem.Text
        If Not Selected.Contains(".") Then
            isFolder = True
            Button4.Enabled = True
        Else
            isFolder = False
            Button4.Enabled = False
        End If

        Dim Selected1 As String = ListView1.FocusedItem.Text
        If Selected.Contains(".") Then
            isFolder1 = True
            Button5.Enabled = True
        Else
            isFolder1 = False
            Button5.Enabled = False
        End If

    End Sub

    Private Sub TextBox4_LostFocus(sender As Object, e As EventArgs) Handles TextBox4.LostFocus
        TextBox4.Text = "Enter Folder Name Here..."
        TextBox4.ForeColor = Color.DarkGray
    End Sub

    Private Sub TextBox4_TextChanged(sender As Object, e As EventArgs) Handles TextBox4.TextChanged
        TextBox4.ForeColor = Color.Black
    End Sub

    Private Sub TextBox4_Click(sender As Object, e As EventArgs) Handles TextBox4.Click
        TextBox4.Text = ""
    End Sub
    Public ReadOnly Property FileType() As FTPfileInfo.DirectoryEntryTypes
        Get
            Return Label4.Text = ListView1.FocusedItem.Text & FileType
        End Get
    End Property

    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
        Label4.Text = FileType
    End Sub
End Class

Edited 3 Years Ago by Ahmed.C

Change the for statement to say:

For Each f As File In FTPDownload.ListDirectory

Then reference f.

The problem is that you are refrenceing a class File that has not been instantiated.

Edited 3 Years Ago by Begginnerdev

This article has been dead for over six months. Start a new discussion instead.