hello

I wrote this code using classes and it keeps giving me the error message :
a nullreference exception was unhandeled/Object reference not set to an instance of an object.

I am trying to add stock code and stock shares to the list of transactions using a currClient As Client (which, holds the current clients information) that I have listed as a global. I also have a method called getClientTransaction in the client class.(3 classes stock, transaction, and client). Whenever the click on the summary button it should give me all client summary, but it keeps crashing...Did some research on the error, and nothing helped...Could sure use some help getting unstuck...
here is part of my code

Private Sub btnGainLoss_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnYTD.Click
        'display transactions from current year for current client
        Dim tyear As Integer
        Dim ndx As Integer

        If txtClientName.Text = "" Then
            MessageBox.Show("no client selected")
            Exit Sub
        End If

        If Not Integer.TryParse(txtYear.Text, tyear) Then
            MessageBox.Show("enter a valid year")
            txtYear.Focus()
            txtYear.SelectAll()
            Exit Sub
        Else
            If tyear < Today.Year - 100 Or tyear > Today.Year Then
                MessageBox.Show("enter a valid year")
                txtYear.Focus()
                txtYear.SelectAll()
                Exit Sub
            End If
        End If

        If currClient.clientCount = 0 Then
            lblDisp.Text = "No transactions in target year"
        Else
            lblDisp.Text = "Date        Stock               Type   Share    Price      proceeds" & vbCrLf
            For ndx = 0 To currClient.clientCount - 1
                If currClient.getClientTransaction(ndx).TranDate.Year = CDbl(txtYear.Text) Then

                    stock1 = New stock(currClient.getClientTransaction(ndx).TranStockCode)

                    lblDisp.Text += currClient.getClientTransaction(ndx).TranDate.ToShortDateString.PadRight(12) & _
                    stock1.StockName.PadRight(3) & currClient.getClientTransaction(ndx).TranType.PadRight(7) & _
                    currClient.getClientTransaction(ndx).TranShares.ToString.PadRight(9) & FormatCurrency(currClient.getClientTransaction(ndx).TranSharePrice) & "   " & _
                    FormatCurrency(currClient.getClientTransaction(ndx).t_Shares) & vbCrLf
                End If
            Next
        End If
    End Sub

Thank you

Recommended Answers

All 3 Replies

Hi,

The error message you've got is for wich line?

sorry posted wrong part of the code.. the era is right before I add the shares and code to list

Private Sub btnSummary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSummary.Click

        'get a summary of client portfolio and value
        Dim portCodeList As New List(Of String) 'list of codes in portfolio
        Dim portSharesList As New List(Of Integer) 'shares of stock

        Dim pndx As Integer  'index for portfolio lists
        Dim dline As String 'next line of portfolio
        Dim stockvalue As Double
        Dim totValue As Double

        If txtClientName.Text = "" Then
            MessageBox.Show("no client selected")
            Exit Sub
        End If

        Do               'test each transaction

            If currClient.clientCount = 0 Then

                pndx = portCodeList.IndexOf(currClient.getClientTransaction(pndx).TranStockCode)
                If pndx = -1 Then  'add to end of list

                    'Where era is found
                    portCodeList.Add(currClient.getClientTransaction(pndx).TranStockCode)

                    portSharesList.Add(currClient.getClientTransaction(pndx).TranShares) 'assume it is a buy

                Else
                    If currClient.getClientTransaction(pndx).TranType = "B" Then
                        portSharesList.Item(pndx) += currClient.getClientTransaction(pndx).TranShares
                    Else
                        portSharesList.Item(pndx) -= currClient.getClientTransaction(pndx).TranShares

                    End If
                End If
            End If
            'MessageBox.Show(currClient.getClientTransaction(pndx).TranType.ToString)

            'Not sure how to end loop
        Loop Until currClient.getClientTransaction(pndx).TranType = ""

        'display each stock in portfolio
        If portCodeList.Count = 0 Then
            lblDisp.Text = "client has no stocks"
            Exit Sub
        End If

        totValue = 0
        lblDisp.Text = "Code  Name                 Shares   Price       Value" & vbCrLf

        For pndx = 0 To portCodeList.Count - 1

            If portSharesList.Item(pndx) = 0 Then
                stock1 = New stock(currClient.getClientTransaction(pndx).TranStockCode)
                Continue For
            End If



            stockvalue = currClient.getClientTransaction(pndx).TranSharePrice * portSharesList(pndx)
            totValue += stockvalue

            dline = portCodeList(pndx).PadRight(7) & stock1.StockName.PadRight(21) & _
                 portSharesList(pndx).ToString.PadLeft(5) & _
                 FormatCurrency(currClient.getClientTransaction(pndx).TranSharePrice).PadLeft(9) & _
                 FormatCurrency(stockvalue).PadLeft(12) & vbCrLf
            lblDisp.Text += dline & vbCrLf
        Next
        lblDisp.Text += vbCrLf & "Total portfolio value as of " & _
                Today.ToShortDateString & " = " & _
                FormatCurrency(totValue)

    End Sub

Revised my code and used different debugging methods...

Private Sub btnSummary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSummary.Click

        'get a summary of client portfolio and value
        Dim portCodeList As New List(Of String) 'list of codes in portfolio
        Dim portSharesList As New List(Of Integer) 'shares of stock

        Dim pndx As Integer  'index for portfolio lists
        Dim dline As String 'next line of portfolio
        Dim stockvalue As Double
        Dim totValue As Double

        If txtClientName.Text = "" Then
            MessageBox.Show("no client selected")
            Exit Sub
        End If

        Do               'test each transaction

            If currClient.getClientTransaction(pndx).TranType <> "X" Then

                ' MessageBox.Show(currClient.getClientTransaction(pndx).TranStockCode)

                pndx = portCodeList.IndexOf(currClient.getClientTransaction(pndx).TranStockCode)

                If pndx = -1 Then  'add to end of list

                    portCodeList.Add(currClient.getClientTransaction(pndx).TranStockCode)

                    portSharesList.Add(currClient.getClientTransaction(pndx).TranShares) 'assume it is a buy

                Else
                    If currClient.getClientTransaction(pndx).TranType = "B" Then
                        portSharesList.Item(pndx) += currClient.getClientTransaction(pndx).TranShares
                    Else
                        portSharesList.Item(pndx) -= currClient.getClientTransaction(pndx).TranShares

                    End If
                End If
            End If

        Loop Until currClient.getClientTransaction(pndx).TranType = "X"

        'display each stock in portfolio
        If portCodeList.Count = 0 Then
            lblDisp.Text = "client has no stocks"
            Exit Sub
        End If

        totValue = 0
        lblDisp.Text = "Code  Name                 Shares   Price       Value" & vbCrLf

        For pndx = 0 To portCodeList.Count - 1

            If portSharesList.Item(pndx) = 0 Then
                stock1 = New stock(currClient.getClientTransaction(pndx).TranStockCode)
                Continue For
            End If



            stockvalue = currClient.getClientTransaction(pndx).TranSharePrice * portSharesList(pndx)
            totValue += stockvalue

            dline = portCodeList(pndx).PadRight(7) & stock1.StockName.PadRight(21) & _
                 portSharesList(pndx).ToString.PadLeft(5) & _
                 FormatCurrency(currClient.getClientTransaction(pndx).TranSharePrice).PadLeft(9) & _
                 FormatCurrency(stockvalue).PadLeft(12) & vbCrLf
            lblDisp.Text += dline & vbCrLf
        Next
        lblDisp.Text += vbCrLf & "Total portfolio value as of " & _
                Today.ToShortDateString & " = " & _
                FormatCurrency(totValue)

    End Sub

I put messagebox above the pndx = portCodeList.IndexOf(currClient.getClientTransaction(pndx).TranStockCode)
and it gives me the correct Type of the client number I put in textbox..tried code...also correct code of client..but once it went through the rest of code it crashes.

gives me this error

object reference not instance of an instance of an object

in the trouble shooting tips it said

use a new keyword to create an object instance

Not sure what this means..

If I put a breakpoint in the same line of code it takes me through the code into this method written in my client class...

Public Function getClientTransaction(ByVal position As Integer) As transaction

        If position >= 0 And position <= m_tranlist.Count - 1 Then
            Return m_tranlist.Item(position)
        End If
        Return Nothing
    End Function

each time it goes over index of code it gives me same era code

I know I'm probably not explaing myself correctly...
Thank you for your understanding and help..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.