0

I am working on an assignment with my friend. I asked for help on how to do the loop and he gave me the code for that part. SO I copied and pasted it into vb. It works for him but everytime I try to debug it I keep getting the sign "Null exception was unhandled". But its not just one one line. Fist it starts on the LstInvoice.items.clear() but if I delete that, it goes through all the lines. What is going on? I used the LstInvoice.items.clear() before on other assignments and never had this problem before. Here is my code:

Private Sub btnStraight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStraight.Click
        Dim Cost As Double

        Cost = txtCost.Text
        Dim Salvage_Value As Double
        Salvage_Value = 0
        Dim Life As Double
        Life = txtLife.Text
        Dim Depreciation As Double
        Depreciation = (Cost / Life)
        Dim c As Integer, i As Integer, x As Integer, y As Integer, z As Integer
        c = CInt(CDbl(txtYear.Text))
        i = CInt(txtLife.Text)
        x = CInt(txtCost.Text)
        y = CInt(CDbl(x) / i)
        z = x - y
        LstInvoice.items.clear()
        LstInvoice.Items.Add("Description: " & "" & txtDescription.Text)
        LstInvoice.Items.Add("Year of purchase: " & txtYear.Text)
        LstInvoice.Items.Add("Cost: " & FormatCurrency(txtCost.Text))
        LstInvoice.Items.Add("Estimated life:" & txtLife.Text)
        LstInvoice.Items.Add("Method of Depresciation: straight-line method")
        LstInvoice.Items.Add("")
        LstInvoice.Items.Add("Value beginning of " & c & ": " & FormatCurrency(CInt(txtCost.Text)))
        LstInvoice.Items.Add("Amount of depreciation accruing: " & c & ": " & FormatCurrency(y))
        LstInvoice.Items.Add("Total depreaciation at end of " & c & ": " & FormatCurrency(z))
        LstInvoice.Items.Add("")
        c = c + 1
        Do While (x > 0)
            y = CInt(CDbl(x) / i)
            z = x - y
            x = z
            LstInvoice.Items.Add("Value beginning of " & c & ": " & FormatCurrency(x))
            LstInvoice.Items.Add("Amount of depreciation accruing: " & c & ": " & FormatCurrency(y))
            LstInvoice.Items.Add("Total depreaciation at end of " & c & ": " & FormatCurrency(z))
            LstInvoice.Items.Add("")
            i = i - 1
            c = c + 1
        Loop
Julie Haag
  • 23
  • 5
  • 1
    I assume that `LstInvoice` is supposed to a listbox control that displays invoices. Do you have such a control with that name added to your form? Otherwise, it will be null because no such object exists. – Cody Gray - on strike Apr 08 '13 at 22:28
  • There's this at the bottome except it added a throw line to it which the debugging system had me get rid of Private Function LstInvoice() As Object End Function End Class – Julie Haag Apr 08 '13 at 22:30
  • What is going on is that you are deleting code you need. Don't do that. If you get a null reference exception you have to debug the code; read [this](http://stackoverflow.com/questions/4660142/). – Dour High Arch Apr 08 '13 at 22:35
  • In addition to the other responses, put Option Strict On at the top of your code or go into the project properties and set Option Strict On. And make that the default for all of your new projects. It will save you lots of hassle. – Chris Dunaway Apr 09 '13 at 14:44

2 Answers2

0

You haven't capitalized everything properly, it should look like this:

LstInvoice.Items.Clear()

notice the capital I and C.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • Capitalize does not change the problem, but please do not add semicolons in VB.Net. Those are real errors – Amegon Apr 08 '13 at 23:10
  • @Amegon, yes as you can see by my edit before your comment I've since removed the semicolon. My day to day language is C# but every now and then I have to crack out my VB.NET gloves even at work. But you've got to be kidding me that capitalization doesn't matter! – Mike Perrenoud Apr 08 '13 at 23:13
  • What I meant was that you were right, that the capitalization was not 100% perfect, but this is not connected with any solution here. In VB.Net the case does not matter! Actually it can even be used as an additional control by writing deklarations with correct capitalization, but all other lines by using small letters only. If objects are recognized correctly, then the names will be auto-capitalized correctly. – Amegon Apr 08 '13 at 23:35
0

I tested the exact same code with a project.

As already mentioned by Cody Gray, you may have added the wrong control as lstInvoice. Use a ListBox.

What you did was to choose a code fix after you pasted the code, and generated a code stub for lstInvoice, which creates following method at the bottom of your code:

Private Function LstInvoice() As Object
    Throw New NotImplementedException
End Function

Those corrections offer some working fixes if you forgot a namespace reference, but usually it cannot guess for which control the code was meant.

So please remove this Function, go to the Form (I suppose it is not a console application), open the Toolbox, and drag'n drop a ListBox to your Form. Finally rename that ListBox name from ListBox1 to lstInvoice and check if your code is working now.

Amegon
  • 629
  • 6
  • 15