Results 1 to 4 of 4

Thread: error message: the object reference is not set to an instance of an object

  1. #1

    Thread Starter
    New Member grabbetje's Avatar
    Join Date
    Feb 2017
    Posts
    4

    Post error message: the object reference is not set to an instance of an object

    I try to upload Blob image files to azure and then to retrieve a list of Blobs stored in the container in vb winforms. The uploading runs perfect, but the items stored do not show up in the item list and im unable to download teh Blob. The container is set to read and write and list in Azure so that should not be the problem. As soon as i click the download button this error shows up: the object reference is not set to an instance of an object

    Here is the code. Can somebody help me with this as im totally stuck at this moment.



    Code:
    Imports System.Configuration
    Imports System.Threading
    
    
    Public Class Form1
        Private cts As CancellationTokenSource
        Private cancelToken As CancellationToken
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ToolStripStatusLabel1.Text = ""
            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
            cts = New CancellationTokenSource()
            cancelToken = cts.Token
            cancelToken.ThrowIfCancellationRequested()
    
    
            ListImagesAsync().ConfigureAwait(True)
        End Sub
    
    
        Private Async Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    
    
            Try
                ToolStripStatusLabel1.Text = "Uploading"
                Await Task.Delay(txtDelay.Text)
    
    
                Dim AZBlob As New AzureBlob(ConfigurationManager.AppSettings("StorageConnectionString"), "images")
                Await AZBlob.UploadAsync(PictureBox1.ImageLocation, cancelToken)
                MessageBox.Show("upload success")
                ToolStripStatusLabel1.Text = ""
                PictureBox1.Image = Nothing
            Catch exCancel As OperationCanceledException
                Dim message As String = "upload canceled at Form"
                MessageBox.Show(message)
                ToolStripStatusLabel1.Text = message
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                ToolStripStatusLabel1.Text = ex.Message
            End Try
        End Sub
    
    
        Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
            cts.Cancel()
        End Sub
    
    
        Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
            Dim fd As New OpenFileDialog()
            Dim dResult As DialogResult = fd.ShowDialog()
            If dResult = DialogResult.OK Then
                Dim path As String = fd.FileName
                PictureBox1.LoadAsync(path)
            End If
    
    
        End Sub
    
    
        Private Async Function ListImagesAsync() As Task
            Try
                ToolStripStatusLabel1.Text = "Retrieve Image collection"
                Await Task.Delay(txtDelay.Text)
    
    
                Dim AZBlob As New AzureBlob(ConfigurationManager.AppSettings("StorageConnectionString"), "images")
                Dim result As List(Of String) = Await AZBlob.ListFilesAsync(cancelToken)
                For Each imgName As String In result
                    ListBox1.Items.Add(imgName)
    
    
                Next
                ToolStripStatusLabel1.Text = ""
            Catch exCancel As OperationCanceledException
                Dim message As String = "ListImages canceled at Form"
                MessageBox.Show(message)
                ToolStripStatusLabel1.Text = message
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                ToolStripStatusLabel1.Text = ex.Message
            End Try
        End Function
    
    
        Private Sub BtnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
            ListImagesAsync().ConfigureAwait(False)
        End Sub
    
    
        Private Async Sub btnRet_Click(sender As Object, e As EventArgs) Handles btnRet.Click
            Try
                ToolStripStatusLabel1.Text = "Download Image"
                Await Task.Delay(txtDelay.Text)
                Dim imgName As String = ListBox1.SelectedItem.ToString()
                Dim AZBlob As New AzureBlob(ConfigurationManager.AppSettings("StorageConnectionString"), "images")
                Await AZBlob.DownloadAsync(imgName, cancelToken)
                Dim fs As IO.FileStream = IO.File.OpenRead(My.Application.Info.DirectoryPath & "\" & imgName)
    
    
                PictureBox1.Image = Image.FromStream(fs)
    
    
                ToolStripStatusLabel1.Text = ""
            Catch exCancel As OperationCanceledException
                Dim message As String = "download canceled at Form"
                MessageBox.Show(message)
                ToolStripStatusLabel1.Text = message
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                ToolStripStatusLabel1.Text = ex.Message
    
    
            End Try
    
    
    
    
        End Sub
    End Class

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,348

    Re: error message: the object reference is not set to an instance of an object

    If you thought that it would be more fun for us to guess or waste time trying to work out what line the exception was thrown on, you were wrong. Highlight the specific line of that code that throws the exception. Of course, if you know exactly what line throws, you can put a breakpoint on that line and determine which reference is Nothing. You can then go back through the code to where you expected a value to be assigned and at least try to determine why it was not. There's way more that you can do than just post a whole form's worth of code and tell us that a NullReferenceException is thrown somewhere, sometime.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    Re: error message: the object reference is not set to an instance of an object

    Whenever you get that exception, the line that threw it is the key to solving the problem. In this case, the exact line will be available, and I can kind of guess which one it is, but it takes a few assumptions. For example, you talk about a Download button, but there isn't one named that way in the code. I would guess that it is btnRet, but only because it mentions downloading, and does a download.

    Given the line, examine every object when the exception happens. One of them is Nothing. Once you know which it is, the next part is figuring out why it is Nothing, which sometimes is obvious, and other times not so obvious.
    My usual boring signature: Nothing

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,348

    Re: error message: the object reference is not set to an instance of an object

    One thing you may find is that having all those exception handlers is hindering your ability to determine exactly where the exception is being thrown. It's really not a good idea to be catching the base type Exception as you are and that is recommended against in many, many places. One reason for that is that, unlike when you catch a specific type of exception and thus know exactly the state that would cause it, you are unable to clean up after an arbitrary exception and thus you can't know that it is safe to carry on using the app.

    I would suggest that EVERY Catch block should have a line like this in it:
    vb.net Code:
    1. Debug.WriteLine(ex.ToString)
    That will write the entire contents of the exception to the Output window in the debugger and, among other things, give you the line number of the place the exception was thrown. You can then place a breakpoint on that line and, next time, examine the state before the exception is thrown and determine WHY it happens.

    Alternatively, do away with those 'Catch ex As Exception' lines altogether and, instead, handle the UnhandledException event of the application. That will allow you to exit or restart gracefully instead of crashing or carrying on in an unknown state and it also means that the debugger will break automatically when an unhandled exception is thrown, so you can examine the state at the time it happens.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width