如何在列表框中修复此nullreferenceexception?

时间:2014-09-13 11:11:57

标签: c# nullreferenceexception

很抱歉再次提出这个问题,但我真的不知道如何调试这个问题 调试器在这一行给我nullreferenceexception:         if(listBox1.SelectedItem.ToString()==“Chicken $ 15”) 我认为它给我nullreferenceexception的原因是因为listbox1为null所以我认为我必须初始化它。但是怎么样?我不知道如何初始化列表框。

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();
    }
}

}

1 个答案:

答案 0 :(得分:0)

您似乎认为listBox1.SelectedItem永远不会null,尝试做类似

的事情
if (listBox1.SelectedItem != null)
{
    // code here
}