6 Replies - 801 Views - Last Post: 10 July 2013 - 10:22 AM Rate Topic: -----

#1 darthvader45  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-February 13

Adding values to array, yet get nullreferenceexception

Posted 09 July 2013 - 05:22 AM

Ok, I'm making a program for my visual basic class that manages reservations for a charter boat company. However, when I try to add values to my arraylists, I get a nullreferenceexception. I have tried searching the entire internet for various ways to add textbox contents to an arraylist, and have gotten nowhere. Anybody know of a way to make this work?



Public Class SignUp
    Public DailyTotal As ArrayList
    Public TotalPeople As ArrayList
    Public TotalTax As ArrayList
    Public DailyGrandTotal As ArrayList



    Private Sub PeopleText_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PeopleText.Leave
        If (PeopleText.Text >= 1 And PeopleText.Text >= 1 And PeopleText.Text <= 5) Then
            SubTotalText.Text = FormatCurrency("50")

        ElseIf PeopleText.Text >= "6" And PeopleText.Text <= "10" Then
            SubTotalText.Text = FormatCurrency("45")

        ElseIf PeopleText.Text > "10" Then
            SubTotalText.Text = FormatCurrency("40")

        End If

    End Sub

    Private Sub CityText_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles CityText.Leave
        Select CityText.Text
            Case "Mentor"
                TaxRateText.Text = 0.07
            Case "Painesville"
                TaxRateText.Text = 0.08
            Case "Kirtland"
                TaxRateText.Text = 0.09
            Case Else
                TaxRateText.Text = 0.1
        End Select
    End Sub

    Private Sub CheckoutButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckoutButton.Click
        


        SalesTaxText.Text = FormatCurrency(TaxRateText.Text * SubTotalText.Text)
        FinalAmountText.Text = FormatCurrency(CDec(SubTotalText.Text) + CDec(SalesTaxText.Text))
        InvoiceForm.Show()
        InvoiceForm.CustomerNameText.Text = FirstNameText.Text & LastNameText.Text
        InvoiceForm.GrandTotalText.Text = FormatCurrency(FinalAmountText.Text)
        InvoiceForm.TripDateText.Text = ReservationDateText.Text
        Dim Subtotal As String = SubTotalText.Text
        Dim FinalTotal As String = FinalAmountText.Text
        Dim People As String = PeopleText.Text
        Dim SalesTax As String = SalesTaxText.Text

        DailyTotal.Add(Subtotal)
        TotalPeople.Add(People)
        TotalTax.Add(SalesTax)
    End Sub

    Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
        Dim ctl As Control
        For Each ctl In Controls
            If TypeOf ctl Is TextBox Then
                ctl.Text = ""
            End If
        Next ctl

    End Sub

    Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
        Me.Close()
    End Sub

    
End Class


Is This A Good Question/Topic? 0
  • +

Replies To: Adding values to array, yet get nullreferenceexception

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,875
  • Joined: 12-December 12

Re: Adding values to array, yet get nullreferenceexception

Posted 09 July 2013 - 05:38 AM

A NullReferenceException is one of THE most common errors encountered in VB.NET so I am finding it very hard to believe that you have searched the entire internet :dontgetit:

You have not instantiated your ArrayList Class's:

Public DailyTotal As New ArrayList


I recommend the fundamentals tutorial linked in my signature.

This post has been edited by andrewsw: 09 July 2013 - 05:39 AM

Was This Post Helpful? 1
  • +
  • -

#3 darthvader45  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-February 13

Re: Adding values to array, yet get nullreferenceexception

Posted 09 July 2013 - 05:46 AM

Thank you, I have tried that and it works.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,875
  • Joined: 12-December 12

Re: Adding values to array, yet get nullreferenceexception

Posted 09 July 2013 - 05:47 AM

BTW The ArrayList is effectively deprecated in favour of List(of T).
Was This Post Helpful? 1
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Re: Adding values to array, yet get nullreferenceexception

Posted 09 July 2013 - 02:21 PM

Also note that you're using VB.net not vb6.
Was This Post Helpful? 0
  • +
  • -

#6 darthvader45  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-February 13

Re: Adding values to array, yet get nullreferenceexception

Posted 10 July 2013 - 07:25 AM

View PostAdamSpeight2008, on 09 July 2013 - 02:21 PM, said:

Also note that you're using VB.net not vb6.

Oh, sorry, I didn't know that Visual Basic 2010 is VB.Net. Btw, I managed to get it to work, but I'm having problems getting it to correctly add up all the elements in each array, then display them in textboxes. I'm trying a counter loop. How would I go about coding that?

Public Class InvoiceForm
    Public ReservationCount As Integer = 0
    Private Sub ExitButton_Click(sender As System.Object, e As System.EventArgs) Handles ExitButton.Click
        SignUp.DailyTotal.Add(SignUp.SubTotalText.Text)
        SignUp.TotalPeople.Add(SignUp.PeopleText.Text)
        SignUp.DailyGrandTotal.Add(SignUp.FinalAmountText.Text)
        SignUp.TotalTax.Add(SignUp.SalesTaxText.Text)
        Dim i As Integer
        For i = 0 To SignUp.DailyTotal.Count - 1
            ReservationCount = ReservationCount + 1
        Next
        Me.Close()
    End Sub

    Private Sub InvoiceForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,875
  • Joined: 12-December 12

Re: Adding values to array, yet get nullreferenceexception

Posted 10 July 2013 - 10:22 AM

I've only glanced at your code in the last post but you are not doing anything with ReservationCount. It might count successfully but I assume you then want to put this information somewhere.

Also, your loop is redundant. You could just set ReservationCount to the Count of the DailyTotal:

ReservationCount = SignUp.DailyTotal.Count

But, more significantly perhaps, you are immediately closing the form with Me.Close(). If this is what you intend then I don't see the purpose of this code in the ExitButton_Click. Unless you store your results in a database, or some settings (or elsewhere) they will just vanish. (Perhaps you have some exotic database-code somewhere that automatically saves the information on the form.)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1