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