Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I know that this has been addressed before, but I just can't seem to flush out the correct way to handle the error checking for this. Any help or guidance would be greatly appreciated! Everything works as it should, except if you do not enter an item from both listboxes, I receive

"An unhandled exception of type 'System.NullReferenceException' occurred in Workshop Selector.exe
Additional information: Object reference not set to an instance of an object."

rather than the message box that I would like to show. Obviously I am missing a very important step along the way. I've tried so many different methods to fix it that I've simply become more confused.

Per CHill60's suggestion, this is the updated code with the If...Then added below the variables (it still shows the exception above when debugging):

VB
Public Class FormWorkshopSelector

   

    Private Sub btnAddWorkshop_Click(sender As Object, e As EventArgs) Handles btnAddWorkshop.Click

        'Declare variables and assign values for both listboxes
        Dim intFee As Integer
        Dim intTotal As Integer
        Dim intDay As Integer
        Dim intStay As Integer
        Dim Workshop As String = lstWorkshop.SelectedItem.ToString
        Dim Location As String = lstLocation.SelectedItem.ToString

        If lstWorkshop.SelectedItem Is Nothing And lstLocation.SelectedItem Is Nothing Then
            MessageBox.Show("Select both a workshop and a location")
            Return
        End If

            ' Select workshop
            Select Case Workshop
                Case CStr("Handling Stress")
                    intDay = 3
                    intFee = 595
                Case CStr("Time Management")
                    intDay = 3
                    intFee = 695
                Case CStr("Supervision Skills")
                    intDay = 3
                    intFee = 995
                Case CStr("Negotiation")
                    intDay = 5
                    intFee = 1295
                Case CStr("How to Interview")
                intDay = 1
                    intFee = 395

        End Select

            ' Select location.
            Select Case Location
                Case CStr("Austin")
                    intStay = intDay * 95
                Case CStr("Chicago")
                    intStay = intDay * 125
                Case CStr("Dallas")
                    intStay = intDay * 110
                Case CStr("Orlando")
                    intStay = intDay * 100
                Case CStr("Phoenix")
                    intStay = intDay * 92
                Case CStr("Raleigh")
                    intStay = intDay * 90
           
        End Select
      

            'Cost of each selected workshop and location
            intTotal = intStay + intFee

            'Display cost of workshop and location in "List Cost" list box
        lstCost.Items.Add(intTotal.ToString("c"))
       

    End Sub

    Private Sub btnCalcTotal_Click(sender As Object, e As EventArgs) Handles btnCalcTotal.Click
       
        Dim dblTotal As Double = 0.0   'Accumulator
        Dim sum As Double           ' 

        For x As Integer = 0 To lstCost.Items.Count - 1
            sum += CDbl(lstCost.Items(x))
        Next
        lblTotalCost.Text = sum.ToString("c")
       

    End Sub

    Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
        'Reset the Workshop and Location list boxes.
        lstWorkshop.SelectedIndex = -1
        lstLocation.SelectedIndex = -1

        'Reset the List of Costs list box.
        lstCost.Items.Clear()

        'Reset the Total Cost label
        lblTotalCost.Text = String.Empty

        ' Return the focus to Pick a Workshop.
        lstWorkshop.Focus()
    End Sub
    Private Sub FormWorkshopSelector_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        ' Verify closing of form.
        If MessageBox.Show("Are you sure you want to exit?", "Confirm",
                           MessageBoxButtons.YesNo) = DialogResult.Yes Then
            ' Continue to close the form.
            e.Cancel = False
        Else
            ' Do not close the form.
            e.Cancel = True
        End If
    End Sub
    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        ' Close the form.
        Me.Close()
    End Sub

    
End Class
Posted
Updated 12-Oct-14 5:04am
v2
Comments
[no name] 10-Oct-14 10:01am    
Simply add a check to see if lstWorkshop.SelectedItem is actually valid before trying to use the value.
Member 11139267 10-Oct-14 10:30am    
Hi Wes,

I really appreciate your input,
I tried adding
If lstWorkshop.SelectedItem = -1 Then
' Display a message to select a workshop and a location.
MessageBox.Show("Select both a workshop and a location")

End If to no avail. I have stared at this for so long that it's just becoming more muddled for me.
Can you give me a better idea of what to add and where to add it? If I can see it working, then I can dissect it and "read backwards" to understand how it works. I hope that made sense!
[no name] 10-Oct-14 11:15am    
What does "to no avail" mean? SelectedItem is not a number so it would never be -1 anyway. SelecgtedIndex would be -1. Try If lstWorkshop.SelectedItem is nothing then ...
Member 11139267 10-Oct-14 11:44am    
Hello Wes,
I'm sorry, I should have written the above as code, I meant that I tried <pre lang="vb">lstWorkshop.SelectedIndex = -1 Then
' Display a message to select a workshop and a location.
MessageBox.Show("Select both a workshop and a location")
End If </pre>
You're right and I did add it as index, not item, still not working.

Based upon your suggestion above, I put in:
<pre lang="vb">
Case Else
If lstWorkshop.SelectedItem Is Nothing Then
' Display a message to select a workshop and a location.
MessageBox.Show("Select both a workshop and a location")
End If
End Select
</pre>

Can I add an If...Then to a Select Case/Case Else?
If not where?
I also tried to add it directly beneath the declared variables, same results.

Also, apparently I can't format code in the replies, either. Sigh.
[no name] 10-Oct-14 21:39pm    
Why does
Dim Workshop As String
Dim Location As String

If lstWorkshop.SelectedItem Is Nothing And lstLocation.SelectedItem Is Nothing Then
MessageBox.Show("Select both a workshop and a location")
Return
End If
not work for you?

The lines
VB
Dim Workshop As String = lstWorkshop.SelectedItem.ToString
Dim Location As String = lstLocation.SelectedItem.ToString
will cause a problem if either or both of the listboxes have not been selected.
Instead use
VB
Dim Workshop As String
 Dim Location As String

Your next problem is with
SQL
If lstWorkshop.SelectedItem Is Nothing And lstLocation.SelectedItem Is Nothing Then
    MessageBox.Show("Select both a workshop and a location")
    Return
End If
It should be
SQL
If lstWorkshop.SelectedItem Is Nothing Or lstLocation.SelectedItem Is Nothing Then
    MessageBox.Show("Select both a workshop and a location")
    Return
End If


[EDIT - a bit more handholding]

Because you have removed the initialisation of Workshop and Location you need to add two lines after the tests for Nothing
SQL
Workshop = lstWorkshop.SelectedItem.ToString()
Location = lstLocation.SelectedItem.ToString()
 
Share this answer
 
v2
You all are wonderful! I used Wes's suggestion of
VB
If lstWorkshop.SelectedItem Is Nothing And lstLocation.SelectedItem Is Nothing Then
    MessageBox.Show("Select both a workshop and a location")
    Return
End If


but changed it to CHill60's
VB
If lstWorkshop.SelectedItem Is Nothing Or lstLocation.SelectedItem Is Nothing Then
    MessageBox.Show("Select both a workshop and a location")
    Return
End If

And removed the .ToString per Nosey Parker (so glad that you are nosy!) and it alll works now!

If anyone is interested, this is the final code (before I add in extra comments, etc.)
VB
'Option Strict On

Public Class FormWorkshopSelector

   

    Private Sub btnAddWorkshop_Click(sender As Object, e As EventArgs) Handles btnAddWorkshop.Click

        'Declare variables and assign values for both listboxes
        Dim intFee As Integer
        Dim intTotal As Integer
        Dim intDay As Integer
        Dim intStay As Integer
        Dim Workshop As String = lstWorkshop.SelectedItem
        Dim Location As String = lstLocation.SelectedItem

        If lstWorkshop.SelectedItem Is Nothing Or lstLocation.SelectedItem Is Nothing Then
            MessageBox.Show("Select both a workshop and a location")
            Return
        End If

            ' Select workshop
            Select Case Workshop
            Case CStr("Handling Stress")
                intDay = 3
                intFee = 595
            Case CStr("Time Management")
                intDay = 3
                intFee = 695
            Case CStr("Supervision Skills")
                intDay = 3
                intFee = 995
            Case CStr("Negotiation")
                intDay = 5
                intFee = 1295
            Case CStr("How to Interview")
                intDay = 1
                intFee = 395

        End Select

            ' Select location.
            Select Case Location
                Case CStr("Austin")
                    intStay = intDay * 95
                Case CStr("Chicago")
                    intStay = intDay * 125
                Case CStr("Dallas")
                    intStay = intDay * 110
                Case CStr("Orlando")
                    intStay = intDay * 100
                Case CStr("Phoenix")
                    intStay = intDay * 92
                Case CStr("Raleigh")
                    intStay = intDay * 90
           
        End Select
      

            'Cost of each selected workshop and location
            intTotal = intStay + intFee

            'Display cost of workshop and location in "List Cost" list box
        lstCost.Items.Add(intTotal.ToString("c"))
       

    End Sub

    Private Sub btnCalcTotal_Click(sender As Object, e As EventArgs) Handles btnCalcTotal.Click
       
        Dim dblTotal As Double = 0.0   'Accumulator
        Dim sum As Double           ' 

        For x As Integer = 0 To lstCost.Items.Count - 1
            sum += CDbl(lstCost.Items(x))
        Next
        lblTotalCost.Text = sum.ToString("c")
       

    End Sub

    Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
        'Reset the Workshop and Location list boxes.
        lstWorkshop.SelectedIndex = -1
        lstLocation.SelectedIndex = -1

        'Reset the List of Costs list box.
        lstCost.Items.Clear()

        'Reset the Total Cost label
        lblTotalCost.Text = String.Empty

        ' Return the focus to Pick a Workshop.
        lstWorkshop.Focus()
    End Sub
    Private Sub FormWorkshopSelector_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        ' Verify closing of form.
        If MessageBox.Show("Are you sure you want to exit?", "Confirm",
                           MessageBoxButtons.YesNo) = DialogResult.Yes Then
            ' Continue to close the form.
            e.Cancel = False
        Else
            ' Do not close the form.
            e.Cancel = True
        End If
    End Sub
    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        ' Close the form.
        Me.Close()
    End Sub

    
End Class


Again, thank you all so very much, I will use all of your suggestions (checking spelling, no need to add .ToString to a string (duh), how to properly place If..Then statements, to pay more attention to details, etc.) in the future.

Have a wonderful rest of your weekend!
 
Share this answer
 
Hi!
I think the exception occures because you just declared some variables but didn't define them. If "Select ... End Select" construction didn't find matching value, it will not define variables and you will get exception right here:
VB
intTotal = intStay(warn) + intFee(warn)

So, first of all, while declaring numeric variables, set its values to 0.

Well, why Select can't find matching value? Maybe you are using different cases in Select-construction and ListBox , or something else... In any way I recommend you add "CaseElse" before "End Select" and replace string values with an indexes (ListBox.SelectedIndex).

By the way, items containing in standard ListBox are already Strings. So, fix it:
VB
Dim Workshop As String = lstWorkshop.SelectedItem.ToString
Dim Location As String = lstLocation.SelectedItem.ToString
 
Share this answer
 
Comments
CHill60 12-Oct-14 12:16pm    
Actually SelectedItem in standard ListBox is an Object not a String. Your suggestion still works however.
Your overall solution is not correct though.
... and finally you don't need to declare/define 'workshop' and 'location' just use:


VB
Select Case lstWorkshop.SelectedIndex
    Case -1
        MsgBox("Select 2 values, man!")
        Exit Sub
    Case 0
...
...
...
...
End Select

Select Case lstLocation.SelectedIndex
    Case -1
        MsgBox("Select 2 values, man!")
        Exit Sub
    Case 0
...
...
...
...
End Select
 
Share this answer
 
Comments
CHill60 12-Oct-14 12:14pm    
Use the "Improve Solution" link next to your solution if you want to add extra detail, don't post multiple solutions to the same question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900