本文介绍了选择案例中的错误检查有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好, 我知道之前已经解决了这个问题,但我似乎无法清除正确的方法来处理错误检查这个。任何帮助或指导将不胜感激!一切正常,除非你没有从两个列表框中输入一个项目,我收到 Workshop中发生了'System.NullReferenceException'类型的未处理异常Selector.exe 附加信息:对象引用未设置为对象的实例。 而不是我想要的消息框喜欢表现出来。显然,我错过了一个非常重要的一步。我已经尝试了很多不同的方法来修复它,我只是变得更加困惑。 根据CHill60的建议,这是更新后的代码如果......然后在变量下方添加(调试时仍显示上述异常): 公共 类 FormWorkshopSelector 私人 Sub btnAddWorkshop_Click(发件人作为 对象,e 作为 EventArgs)句柄 btnAddWorkshop.Click ' 声明变量并为两个列表框分配值 Dim intFee As 整数 Dim intTotal 作为 整数 Dim intDay 作为 整数 Dim intStay 作为 整数 Dim 研讨会作为 String = lstWorkshop.SelectedItem.ToString Dim 位置作为 String = lstLocation.SelectedItem.ToString 如果 lstWorkshop.SelectedItem Nothing 和 lstLocation.Select edItem 没什么 然后 MessageBox.Show( 选择一个工作室和一个位置) 返回 结束 如果 ' 选择研讨会 选择 案例研讨会 案例 CStr ( 处理压力) intDay = 3 intFee = 595 案例 CStr ( 时间管理) intDay = 3 intFee = 695 Case CStr ( 监督技能) intDay = 3 intFee = 995 案例 CStr ( 谈判) intDay = 5 intFee = 1295 案例 CStr ( 如何采访) intDay = 1 intFee = 395 结束 选择 ' 选择位置。 选择 案例位置 案例 CStr ( Austin) intStay = intDay * 95 案例 CStr ( Chicago) intStay = intDay * 125 案例 CStr ( 达拉斯) intStay = intDay * 110 案例 CStr ( Orlando) intStay = intDay * 100 案例 CStr ( Phoenix) intStay = intDay * 92 案例 CStr ( Raleigh ) intStay = intDay * 90 结束 选择 ' 每个的成本选定的研讨会和地点 intTotal = intStay + intFee ' 在列表成本列表框中显示车间和位置的成本 lstCost.Items.Add(intTotal.ToString( c)) 结束 Sub 私有 Sub btnCalcTotal_Click(发件人作为 对象,e 作为 Ev entArgs)句柄 btnCalcTotal.Click Dim dblTotal As Double = 0 。 0 ' 累加器 Dim sum As Double ' 对于 x As 整数 = 0 lstCost.Items.Count - 1 sum + = CDbl (lstCost.Items(x) ) 下一步 lblTotalCost.Text = sum.ToString( c) 结束 Sub 私人 Sub btnReset_Click(发件人作为 对象,e As EventArgs)句柄 btnReset.Click ' 重置工作室和位置列表框。 lstWorkshop.SelectedIndex = -1 lstLocation.SelectedIndex = - 1 ' 重置费用清单列表框。 lstCost.Items.Clear() ' 重置总费用标签 lblTotalCost.Text = 字符串 .Empty ' 将焦点返回到选择工作室。 lstWorkshop.Focus() 结束 Sub 私有 Sub FormWorkshopSelector_FormClosing(sender 作为 对象,e 作为 FormClosingEventArgs)句柄 MyBase .FormClosing ' 验证表单的关闭。 如果 MessageBox.Show( 您确定要退出吗?, 确认, MessageBoxButtons.YesNo)= DialogResult.Yes 然后 继续关闭表单。 e.Cancel = False 否则 ' 不要关闭表单。 e.Cancel = True End 如果 结束 Sub 私人 Sub btnExit_Click(发件人正如 对象,e As EventArgs)句柄 btnExit.Click ' 关闭表单。 我 .Close() 结束 Sub 结束 类 解决方案 Dim 工作坊 As String = lstWorkshop.SelectedItem.ToString Dim 位置作为 字符串 = lstLocation.SelectedItem.ToString 将导致问题,如果未选择其中一个或两个列表框。 而是使用 Dim Workshop As 字符串 Dim 位置作为 字符串 您的下一个问题是 如果 lstWorkshop.SelectedItem 没有和 lstLocation.SelectedItem Nothing 然后 MessageBox.Show( 选择研讨会和位置) 返回 结束 如果 它应该是 如果 lstWorkshop.SelectedItem 没有 或 lstLocation.SelectedItem Nothing 然后 MessageBox.Show( 选择一个研讨会和位置) 返回 结束 如果 因为你已经删除了 Workshop 和位置的初始化,你需要添加两个无测试后的行 Workshop = lstWorkshop.SelectedItem.ToString() Location = lstLocation.SelectedItem.ToString() 你们都很棒!我使用了Wes的建议 如果 lstWorkshop.SelectedItem 是 Nothing 和 lstLocation.SelectedItem Nothing 然后 MessageBox.Show( 选择研讨会和位置) 返回 结束 如果 但将其更改为CHill60's 如果 lstWorkshop.SelectedItem 无 或 lstLocation.SelectedItem Nothin g 然后 MessageBox.Show( 选择研讨会和位置) 返回 结束 如果 并删除了每个Nosy Parker的.ToString(很高兴你多管闲事! )它现在可以工作了! 如果有人有兴趣,这是最终的代码(在我添加额外的评论之前等) ' Option Strict On 公共 类 FormWorkshopSelector 私有 Sub btnAddWorkshop_Click(发件人作为 Object ,e As EventArgs)句柄 btnAddWorkshop.Click ' 声明变量并为两个列表框分配值 Dim intFee 作为 整数 Dim intTotal 作为 整数 Dim intDay 作为 整数 Dim intStay 作为 整数 Dim 工作坊作为 字符串 = lstWorkshop.SelectedItem Dim Lo cation As String = lstLocation.SelectedItem 如果 lstWorkshop.SelectedItem 没什么 或 lstLocation.SelectedItem 没什么 那么 MessageBox.Show( 选择工作坊和位置) 返回 结束 如果 ' 选择研讨会 选择 案例工作坊 Cas e CStr ( 处理压力) intDay = 3 intFee = 595 案例 CStr ( 时间管理) intDay = 3 intFee = 695 案例 CStr ( 监督技能) intDay = 3 intFee = 995 案例 CStr ( 谈判) intDay = 5 intFee = 1295 案例 CStr ( 如何采访) intDay = 1 intFee = 395 结束 选择 ' 选择位置。 选择 案例位置 Case CStr ( 奥斯汀) intStay = intDay * 95 案例 CStr ( 芝加哥) intStay = intDay * 125 案例 CStr ( 达拉斯) intStay = intDay * 110 案例 CStr ( Orlando) intStay = intDay * 100 案例 CStr ( Ph oenix) intStay = intDay * 92 案例 CStr ( Raleigh) intStay = intDay * 90 结束 选择 ' 每个选定研讨会的成本和位置 intTotal = intStay + intFee ' 显示费用车间和位置在列表成本列表框中 lstCost.Items.Add(intTotal.ToString( c)) 结束 Sub 私有 Sub btnCalcTotal_Click(发件人作为 对象,e 作为 EventArgs)句柄 btnCalcTotal.Click Dim dblTotal As Double = 0 。 0 ' 累加器 Dim sum As Double ' 对于 x 正如 整数 = 0 lstCost.Items.Count - 1 sum + = CDbl (lstCost.Items(x)) 下一步 lblTotalCost.Text = sum.ToString( c) 结束 Sub 私有 Sub btnReset_Click(发件人作为 对象,e As EventArgs)句柄 btnReset.Click 重置工作室和位置列表框。 lstWorkshop.SelectedIndex = -1 lstLocation。 SelectedIndex = -1 ' 重置费用清单列表框。 lstCost.Items。清除() ' 重置总费用标签 lblTotalCost .Text = 字符串 .Empty ' 将焦点返回到选择工作室。 lstWorkshop.Focus() 结束 Sub 私有 Sub FormWorkshopSelector_FormClosing(sender As Object ,e As FormClosingEventArgs)句柄 MyBase .FormClosing ' 验证表单的关闭。 如果 MessageBox.Show( 您确定要退出吗?, 确认, MessageBoxButtons.YesNo)= DialogResult.Yes 然后 继续关闭表单。 e.Cancel = False 否则 ' 不要关闭表单。 e.Cancel = True End 如果 结束 Sub 私人 Sub btnExit_Click(发件人正如 对象,e As EventArgs)句柄 btnExit.Click ' 关闭表单。 Me .Close() 结束 Sub 结束 类 再次,非常感谢你们,我将使用你们所有的建议(检查拼写,不需要将.ToString添加到字符串(duh),如何正确地放置If..Then声明,以便在将来更加注重细节等。 周末休息一下! 嗨! 我认为发生异常是因为你刚刚声明了一些变量但没有定义它们。如果选择...结束选择构造没有找到匹配值,它将不会定义变量,您将在此处获得异常: intTotal = intStay (警告) + intFee (警告) 所以,首先,在声明数值变量时,将其值设置为0. 嗯,为什么选择找不到匹配的值?也许你在选择 -construction和 ListBox 中使用不同的情况,或其他...我推荐的任何方式在结束选择之前添加 CaseElse 并用索引替换字符串值( ListBox.SelectedIndex )。 顺便说一句,标准ListBox中包含的项目已经是字符串。所以,修复它: Dim Workshop As 字符串 = lstWorkshop.SelectedItem。 ToString Dim 位置作为 字符串 = lstLocation.SelectedItem。 ToString 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.exeAdditional 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):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 解决方案 The lines Dim Workshop As String = lstWorkshop.SelectedItem.ToStringDim Location As String = lstLocation.SelectedItem.ToString will cause a problem if either or both of the listboxes have not been selected.Instead use Dim Workshop As String Dim Location As StringYour next problem is with If lstWorkshop.SelectedItem Is Nothing And lstLocation.SelectedItem Is Nothing Then MessageBox.Show("Select both a workshop and a location") ReturnEnd IfIt should be If lstWorkshop.SelectedItem Is Nothing Or lstLocation.SelectedItem Is Nothing Then MessageBox.Show("Select both a workshop and a location") ReturnEnd 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 NothingWorkshop = lstWorkshop.SelectedItem.ToString()Location = lstLocation.SelectedItem.ToString()You all are wonderful! I used Wes's suggestion of If lstWorkshop.SelectedItem Is Nothing And lstLocation.SelectedItem Is Nothing Then MessageBox.Show("Select both a workshop and a location") ReturnEnd Ifbut changed it to CHill60's If lstWorkshop.SelectedItem Is Nothing Or lstLocation.SelectedItem Is Nothing Then MessageBox.Show("Select both a workshop and a location") ReturnEnd IfAnd 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.)'Option Strict OnPublic 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!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: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:Dim Workshop As String = lstWorkshop.SelectedItem.ToStringDim Location As String = lstLocation.SelectedItem.ToString 这篇关于选择案例中的错误检查有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 09:15