locked
A null reference exception could result at run time RRS feed

  • Question

  • User-152935344 posted

    I get a warning message that reads " Variable 'cust' is used before it has been assigned a value. A null reference exception could result at run time.", but it occurs in my vb file "Northwind.vb with the following code:

    Public Class Northwind
    
        Private cust As BizObjects1.Customer
    
        Private Sub frmCustomer_Load( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles MyBase.Load
    
            'Load the list of customers
            Dim custList As New BizObjects1.CustomerList
            cbCustomers.DataSource = _
             custList.GetCustomerList.Tables(0)
            cbCustomers.DisplayMember = "CompanyName"
            cbCustomers.ValueMember = "CustomerID"
    
            cbCustomers.SelectedIndex = -1
    
            UpdateView()
            Status("Select a customer from list.")
        End Sub
    
        Private Sub Status(ByVal sMessage As String)
            sbStatus.Text = sMessage
        End Sub
    
        Private Sub ReadCustomer(ByVal bReload As Boolean)
            Dim cust As BizObjects1.Customer
    
            If bReload Or cust Is Nothing Then
                cust = New BizObjects1.Customer( _
                cbCustomers.SelectedItem("CustomerID"))
            End If
    
            lblCustomerID.Text = cust.CustomerID
            txtCustomerName.Text = cust.CompanyName
            txtContactName.Text = cust.ContactName
            txtContactTitle.Text = cust.ContactTitle
            txtAddress.Text = cust.Address
            txtCity.Text = cust.City
            txtRegion.Text = cust.Region
            txtPostalCode.Text = cust.PostalCode
            txtCountry.Text = cust.Country
            txtPhone.Text = cust.Phone
            txtFax.Text = cust.Fax
    
            Status("")
    
        End Sub
        Private Sub UpdateView()
            If cbCustomers.SelectedIndex > -1 _
            AndAlso Len(cbCustomers.SelectedItem("CustomerID")) _
            > 0 Then
                cbCustomers.Visible = True
            Else
                cbCustomers.Visible = False
    
            End If
    
        End Sub
    
    
    
    
        Private Sub cmdSaveChanges_click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles cmdSaveChanges.Click
            Dim cust As New BizObjects1.Customer(lblCustomerID.Text)
    
            cust.CompanyName = txtCustomerName.Text
            cust.ContactName = txtContactName.Text
            cust.ContactTitle = txtContactTitle.Text
            cust.Address = txtAddress.Text
            cust.City = txtCity.Text
            cust.Region = txtRegion.Text
            cust.PostalCode = txtPostalCode.Text
            cust.Country = txtCountry.Text
            cust.Phone = txtPhone.Text
            cust.Fax = txtFax.Text
    
            Dim iRows As Int32 = cust.UpdateCustomer
            Select Case iRows
                Case -1
                    Status("No customers updated. There were no data " _
                    & "changes to make.")
                Case 0
                    Status("No customers updated. An error may have " _
                    & "occurred.")
                Case 1
                    Status("No customers updated one customer record.")
                Case Else
                    Status("updated " & iRows.ToString _
                    & " customers. This may indicate data " _
                    & "corruption since only one customer was " _
                    & "targeted for update.")
            End Select
    
        End Sub
    
        Private Sub cmdDiscardChanges_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles cmdDiscardChanges.Click
            If Len(cbCustomers.SelectedItem("CustomerID")) > 0 Then
                'Ignore this if there is no customer selected
                ReadCustomer(False)
            End If
        End Sub
    
        Private Sub cbCustomers_selectedindexchanged( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles cbCustomers.SelectedIndexChanged
            If cbCustomers.SelectedIndex > -1 _
            AndAlso Len(cbCustomers.SelectedItem("CustomerID")) _
            > 0 Then
                ReadCustomer(True)
                Status("Loaded the customer: " _
                & cbCustomers.SelectedItem("CompanyName") & ".")
            Else
                cust = Nothing
                Status("Select a customer from list.")
            End If
    
            UpdateView()
    
        End Sub
    
    End Class
    Friday, August 5, 2011 4:40 PM

Answers

  • User-573138384 posted

    I see duplicate variables in your code. You already defined a private variable cust at class level.

    Private cust As BizObjects1.Customer

    You need not declare cust again in methods. You can use same class level variable here.

    Remove this

    Dim cust As BizObjects1.Customer

    Change this

    change
    
    Dim cust As New BizObjects1.Customer(lblCustomerID.Text)
    
    to
    
    cust = New BizObjects1.Customer(lblCustomerID.Text)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 5, 2011 9:42 PM

All replies

  • User-573138384 posted

    I see duplicate variables in your code. You already defined a private variable cust at class level.

    Private cust As BizObjects1.Customer

    You need not declare cust again in methods. You can use same class level variable here.

    Remove this

    Dim cust As BizObjects1.Customer

    Change this

    change
    
    Dim cust As New BizObjects1.Customer(lblCustomerID.Text)
    
    to
    
    cust = New BizObjects1.Customer(lblCustomerID.Text)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 5, 2011 9:42 PM
  • User-152935344 posted

    Thank you fayaz_3e!!  The error is now removed. Laughing

    Saturday, August 6, 2011 11:00 PM