简体   繁体   中英

Null Reference Exception was unhandeled by user c#

I'm Learning to program c# for WP8.

The error is: "Object reference not set to an instance of an object."

I'm getting this error and have no idea why, I've used similar code in other classes and it works fine /=

If you need more information let me know, thanks!(=

 public partial class DisplayScenario : PhoneApplicationPage
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler temp = PropertyChanged;
        if (temp != null)
        {
            temp(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    int index;
    private MyDataContext database;
    private ObservableCollection<Questions> questionList;
    public ObservableCollection<Questions> QuestionList
    {
        get
        {
            return questionList;
        }
        set
        {
            if (questionList != value)
            {
                questionList = value;
                NotifyPropertyChanged("QuestionList");
            }
        }
    }

    public void DisplaySceanrio()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
      //  QuestionsList.ItemsSource = null;

        if (NavigationContext.QueryString.ContainsKey("index"))
        {
            string value;

            NavigationContext.QueryString.TryGetValue("index", out value);
            index = System.Convert.ToInt32(value);

        } 
        database = new MyDataContext();

        QuestionList = new ObservableCollection<Questions>(from Questions q in database.MyQuestions where q.Id == index select q);
        //Getting error here: vvv
        QuestionsList.ItemsSource = QuestionList;

    }

Here's the xaml for the listBox:

<ListBox x:Name="QuestionsList" ItemsSource="{Binding QuestionList}" SelectionChanged="QuestionTapped" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock x:Name="QuestionName" Text="{Binding Question, Mode=OneWay}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

QuestionsList is not yet created when you call the OnNavigatedTo method. It's defined in XAML.

If you used the Loaded event handler, then you know that the ListBox has been created and you can reference it:

public DisplayScenario()
{
    InitializeComponent();
    this.Loaded += DisplayScenario_Loaded;
}

void DisplayScenario_Loaded(object sender, RoutedEventArgs e)
{
    QuestionsList.ItemsSource = QuestionList;
}

Also, your constructor has a typo and is never called. It should be DisplayScenario().

public DisplayScenario()
{
    InitializeComponent();
    this.DataContext = this;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM