1

I created a form in WPF (visual stutio 2013, C#) that allows the user to selected an item in the one combobox then in turn will open up text files and read a section of data in a text file that matches the combobox selection. The output goes to combobox2. the User will then select one item in this combobox which in turn opens and reads another text file data section and outputs to combobox3.

The program doesn't work properly. Here is the problem. If the user goes all the way through it is fine if and only if another selection in combobox1 is not made. If it is made an error is thrown.

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

I don't know how to handle this. The program is to work regardless of how many times the user makes a selection.

Please give suggestions also. I am new to Xaml and C#. Thanks

Here is the C# code.

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace WpfApplication3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
/// 
public partial class MainWindow : Window
{
    public ObservableCollection<string> list = new   ObservableCollection<string>();
    public int selectedIndex = -1;
    public MainWindow()
    {
        InitializeComponent();
        list.Add("Los Angeles");
        list.Add("Boston");
        list.Add("Atlanta");
        this.CityList.ItemsSource = list;


   } 
    public class HarCntyZip
    {
        public static List<string> lines = File.ReadLines(@"text1.txt").ToList();
    }

    public class StrbyZip
    {
        public static List<string> lines = File.ReadLines(@"text2.txt").ToList();
    }


    private void cityList_selectionChanged(object sender, SelectionChangedEventArgs e)
    {

        zip.Items.Clear();

        String getCityListSelItem = CityList.SelectedItem.ToString();
        var hczip = HarCntyZip.lines.IndexOf(getCityListSelItem);

        var g = hczip + 1;
        var j = 0;
        for (var i = 0; i < 300; i++){
            j = g + i;
            if (HarCntyZip.lines[j] == "")
            {
                break;
            }
            else if (HarCntyZip.lines[j] != "")
            {
                zip.Items.Add(HarCntyZip.lines[j]); 
            }
        }

    }
    private void zip_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        StreetList.Items.Clear();


        String getZipSelItem = zip.SelectedItem.ToString();
        var Str = StrbyZip.lines.IndexOf(zip.SelectedItem.ToString());

        var s_l = Str + 1;
        var s_c = 0;
        for (var i = 0; i < 12000; i++)
        {
            s_c = s_l + i;
            if (StrbyZip.lines[s_c].ToString() == "")
            {
                break;
            }
            else if (StrbyZip.lines[s_c].ToString() != "")
            {
                StreetList.Items.Add(StrbyZip.lines[s_c].ToString());
            }
        }


    }

    private void StreetList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    }


}
}

Here is the Xaml code

Window x:Class="WpfApplication3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="683.1">
<Grid>
    <ComboBox HorizontalAlignment="Left" Height="22" Name="CityList" Margin="10,55,0,0" VerticalAlignment="Top" Width="140" SelectionChanged="CityList_selectionChanged"/>

    <ComboBox  HorizontalAlignment="Left" Margin="232,55,0,0" VerticalAlignment="Top" Width="111" Name="zip" SelectionChanged="zip_SelectionChanged"/>
    <ComboBox HorizontalAlignment="Left" Height="20" Margin="425,57,0,0" VerticalAlignment="Top" Width="181" Name="StreetList" SelectionChanged="StreetList_SelectionChanged"/>

</Grid>

Text1.txt

Los Angeles
90005
90015

Boston
02120

Atlanta
30314

Text2.text

90005
Crenshaw Blvd
S Bronson Ave
W Norton Ave

90015 
S Union Ave
W Pico Blvd
Venice Blvd

02120
Centre St
Washington St
Columbus Ave

30314
WestMoor Dr NW
Lena St NW
Temple St NW
Nb_me
  • 231
  • 2
  • 6
  • 20
  • The problem is easy to find. Look at the StackTrace property of the exception. It will tell you exactly where the problem is. – 15ee8f99-57ff-4f92-890c-b56153 Dec 12 '16 at 17:24
  • @EdPlunkett I know where the problem is don't know how to fix it – Nb_me Dec 12 '16 at 17:29
  • @EdPlunkett I am using C# – Nb_me Dec 12 '16 at 17:30
  • Are you saying that you know which reference is null, and what line the exception is thrown on? if so, why didn't you include that information in your question? If that's not what you're saying, then you don't know where the problem is. – 15ee8f99-57ff-4f92-890c-b56153 Dec 12 '16 at 17:32
  • 1
    I was able to identify the code as C#, but thank you. – 15ee8f99-57ff-4f92-890c-b56153 Dec 12 '16 at 17:34
  • @EdPlunkett If it is not clear Sorry. I don't know how to fix the problem I don't know why it will not reference the selection in combobox1 when the user makes another item choice from it. The message says it does not exist. – Nb_me Dec 12 '16 at 17:37
  • @EdPlunkett I just want some help....you marked this as "asked already" When my situation is different. would you please look at the code. – Nb_me Dec 12 '16 at 17:44
  • I told you how to find out where it's throwing the exception. Go there. Add some code to make it avoid hitting the problem line when the problem exists. You need to read your own code. Read it and understand it. Read and understand every part of every line. Step through it in the debugger. Understand where and why and when it throws the exception and change the code so it doesn't do that thing at that time in that place. There is no other way. – 15ee8f99-57ff-4f92-890c-b56153 Dec 12 '16 at 17:44
  • Your situation is the same. You have a null reference exception. – 15ee8f99-57ff-4f92-890c-b56153 Dec 12 '16 at 17:45
  • @Nb_me seems like a unique question to me. I'm not sure exactly how to solve this but in general a really useful tip I found was that clearing data below the affected data is the best way to go about these things. What I mean by this is you should have a switch case or an event handler for when something in combobox1 is selected. When that happens you're going to want to clear the contents of all the boxes dependant on it (in your as combobox2 and combobox3) and then repopulate with data that's relevant to the data in combobox1. Do you see what I mean? – J.Doe Dec 12 '16 at 17:48
  • @J.Doe yes I understand and will try.. – Nb_me Dec 12 '16 at 17:55
  • @EdPlunkett I see the mistake I made but it did not fix my problem. I'll edit my original post – Nb_me Dec 12 '16 at 17:58
  • Yeah, in general anything that's in a list sort of form, whether it's an array or a combobox or a graph, there's almost always a method involved called `.Clear()` and it's really useful to call this and repopulate whenever a big change is made. It also helps with reducing wasted memory on things you may not be using – J.Doe Dec 12 '16 at 18:05
  • 1
    first of all you need to learn how to use debug, your case is quite simple, when you call zip.Items.Clear(); inside cityList_selectionChanged it triggers zip_SelectionChanged and as you understood after zip was cleared zip.SelectedItem is null .... – makison Dec 12 '16 at 22:41
  • @AlekseyNosik Thanks...I use debug but did not recognize or saw this as an issue. I did the zip.Items.clear to clear the last entries but it affected the new entry. – Nb_me Dec 13 '16 at 02:00
  • just add check if(zip.SelectedItem == null) return; – makison Dec 13 '16 at 07:51
  • @AlekseyNosik My I wanted to get rid of the zip.Items old entries then have it have the new entries displayed only – Nb_me Dec 14 '16 at 00:47

0 Answers0