C# 如何修复列表框中的nullreferenceexception?
很抱歉再次提出这个问题,但我真的不知道如何调试它。 调试器在此行中为我提供nullreferenceexception: 如果listBox1.SelectedItem.ToString==鸡肉$15 我想它之所以给我nullreferenceexception是因为listbox1为null,所以我想我必须初始化它。但是怎么做呢?我不知道如何初始化列表框C# 如何修复列表框中的nullreferenceexception?,c#,nullreferenceexception,C#,Nullreferenceexception,很抱歉再次提出这个问题,但我真的不知道如何调试它。 调试器在此行中为我提供nullreferenceexception: 如果listBox1.SelectedItem.ToString==鸡肉$15 我想它之所以给我nullreferenceexception是因为listbox1为null,所以我想我必须初始化它。但是怎么做呢?我不知道如何初始化列表框 using System; using System.Collections.Generic; using System.Component
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace lab6
{
public partial class Form1 : Form
{
double total = 0;
int x = 0;
string ord = "";
public Form1()
{
InitializeComponent();
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void editToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void placeToolStripMenuItem_Click(object sender, EventArgs e)
{
checkBox1.Checked = false;
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
radioButton4.Checked = false;
switch (checkBox1.Checked)
{
case true:
total += 1;
ord += "Water\n";
break;
}
if (comboBox1.Text == "Extra Meat")
{
total += 1;
ord += ord + "Extra Meat\n";
}
if (comboBox1.Text == "Extra Rice")
{
total += 1;
ord += "Extra Rice\n";
}
if (comboBox1.Text == "Extra Veggies")
{
total += 1;
ord += "Extra Veggies\n";
}
if (listBox1.SelectedItem.ToString() == "Chicken $15")
{
total += 15;
ord += " Chicken\n";
}
else { }
if (listBox1.SelectedItem.ToString() == "Pizza $8") //< my pathetic attempt to figure it out with intelisense
{
total += 8;
ord += "Pizza\n";
}
else
{
}
if (listBox1.SelectedItem.ToString() == "Spaghetti $12")//< my pathetic attempt to figure it out with intelisense
{
total += 12;
ord += " Spaghetti\n";
}
else { }
if (listBox1.SelectedItem.ToString() == "Fries $8")
{
total += 8;
ord += " Fries\n";
}
else { }
if (listBox1.SelectedItem.ToString() == "Burger $10")
{
total += 10;
ord += " Burger\n";
}
else { }
//radiobutton
if (radioButton1.Checked)
{
total+=5;
ord += "Pineapple Juice\n";
}
if (radioButton2.Checked)
{
total+=6;
ord += "Mango Juice\n";
}
if (radioButton3.Checked)
{
total+=7;
ord += "Apple Juice\n";
}
if (radioButton4.Checked)
{
total+=8;
ord += "Orange Juice\n";
}
MessageBox.Show("Order Done");
listBox1.SelectedItems.Clear();
}
private void clearToolStripMenuItem_Click(object sender, EventArgs e)
{
ord = "";
total = 0;
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void displayToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Order: " + ord+"\nTotal: "+total);
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
}
}看起来您假设listBox1.SelectedItem从不为空,请尝试执行以下操作
if (listBox1.SelectedItem != null)
{
// code here
}
对于初学者来说,NullReferenceException是一种常见的情况。提供的链接应有助于您了解问题。然后使用调试器查找什么/何处/何时变量为null。是否确认listBox1为null?或者listBox1.SelectedItem是否为空?您可以使用调试器进行调试,在代码行上放置断点,并在运行时检查调试器中的值。谢谢。我试过这个:如果listBox1!=null{if listBox1.SelectedItem.ToString==Chicken$15{total+=15;ord+=Chicken\n;}否则{}它不起作用,我的示例中有一个错误,我现在已经更正了。我实际上是想检查所选项是否为null,而不是listboxIt是否不再抛出异常,但问题是它完全忽略了其中的语句。