0

Sorry for asking this question again but, I don't really know how to debug this.
The debugger is giving me nullreferenceexception in this line: if (listBox1.SelectedItem.ToString() == "Chicken $15") I figure that the reason why it is giving me nullreferenceexception is because listbox1 is null so I think I have to initialize it. But how? I dont know how to initialize a listbox.

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

}

  • `NullReferenceException` is a common situation for beginner programmers. The link provided should help you understand the problem. Then use the debugger to find what/where/when you have a variable that is `null`. – Soner Gönül Sep 13 '14 at 11:15
  • Have you confirmed that `listBox1` is `null`? Or is `listBox1.SelectedItem` what's `null`? You debug it by using the debugger, putting a breakpoint on the line of code, and examining the values in the debugger at runtime. – David Sep 13 '14 at 11:15

1 Answers1

0

It looks like you're assuming that listBox1.SelectedItem is never null, try doing somthing like

if (listBox1.SelectedItem != null)
{
    // code here
}
Joe
  • 1,214
  • 3
  • 15
  • 33
  • thank you. I tried this : if (listBox1 != null) { if (listBox1.SelectedItem.ToString() == "Chicken $15") { total += 15; ord += " Chicken\n"; } else { }} it didnt work – JC Balantakbo Sep 13 '14 at 11:23
  • Sorry, there was a mistake in my example, I've corrected it now. I actually meant to check whether the selected item was null, not the listbox – Joe Sep 13 '14 at 11:25
  • It doesn't throw the exception anymore but the problem is that it completely ignored the statements inside. – JC Balantakbo Sep 13 '14 at 11:30