System.NullReferenceException: 'Object reference not set to an instance of an object.'

doan gioi 0 Reputation points
2024-07-19T16:17:23.72+00:00

Hi guys if i uncomment code below it will error with System.NullReferenceException: 'Object reference not set to an instance of an object.' lvOrders was null . If i comment its ok , i started debug and every thing run ok and i check if i selectedItem.Content the it error . I just start learning i hope u guys can teach me thank you !

=== my code :

using Microsoft.IdentityModel.Tokens;

using MiYu.Models;

using System;

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;

namespace MiYu

{

/// <summary>

/// Interaction logic for ManageOrders.xaml

/// </summary>

public partial class ManageOrders : UserControl

{

    public ManageOrders()

    {

        InitializeComponent();

    }

    public void Load(string a,string b)

    {

        int staId = 5;

        if(b.Equals("Cancel")) { staId = 5; }

        if (b.Equals("Done")) { staId = 3; }

        var ctOrders = MiYuContext.INSTANCE.Orders.Where(x => x.StatusId == staId).Select(x => new

        {

            Id = x.Id,

            TimeStart = x.TimeStart,

            Price = x.Price,

            EmployeeName = MiYuContext.INSTANCE.Accounts.Where(y => y.Id.Equals(x.EmployeeId)).Select(x => x.Name).FirstOrDefault(),

            status = "Processing",

            CustomerName = "Guest",

            TableId = x.TableId,

            TableName = x.Table.Name,

            Floor = x.Table.Floor,

        }).ToList();

        if (!a.IsNullOrEmpty())

        {

            ctOrders = MiYuContext.INSTANCE.Orders.Where(x => x.StatusId == staId && x.Table.Name.Contains(a)).Select(x => new

            {

                Id = x.Id,

                TimeStart = x.TimeStart,

                Price = x.Price,

                EmployeeName = MiYuContext.INSTANCE.Accounts.Where(y => y.Id.Equals(x.EmployeeId)).Select(x => x.Name).FirstOrDefault(),

                status = "Processing",

                CustomerName = "Guest",

                TableId = x.TableId,

                TableName = x.Table.Name,

                Floor = x.Table.Floor,

            }).ToList();

        }

        lvOrders.ItemsSource = ctOrders;

    }

    private void UserControl_Loaded(object sender, RoutedEventArgs e)

    {

        Load("","");

    }

    private void txFindTable_TextChanged(object sender, TextChangedEventArgs e)

    {

        Load(txFindTable.Text, "");

    }

    private void search_Click(object sender, RoutedEventArgs e)

    {

        

    }

    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)

    {

        var selectedItem = cbStatus.SelectedItem as ComboBoxItem;

       /* if (selectedItem.Content.ToString().Equals("Processing")) { Load(txFindTable.Text, "Processing"); }

        if (selectedItem.Content.ToString().Equals("Cancel")) { Load(txFindTable.Text, "Cancel"); }

        if (selectedItem.Content.ToString().Equals("Done")) { Load(txFindTable.Text, "Done"); }*/

    }

}

}

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,624 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,630 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 114.5K Reputation points
    2024-07-19T16:59:48.23+00:00

    Try this:

    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if( !cbStatus.IsLoaded ) return;
    
        ... then your code ...
    }
    

  2. Bruce (SqlWork.com) 61,266 Reputation points
    2024-07-19T17:15:09.1633333+00:00

    I see no code to initial lvOrders, so it’s probably null. While not your current issue,

    if (!a.IsNullOrEmpty())
    

    Will fail if a is null. Try

    if (!string.IsNullOrEmpty(a))