title: ListView Object reference not set to an instance of an object
body: <p>I got a problem with my UserControl. When I start the program I got a "Object reference not set to an instance of an object." error in the line "listView1.Items.Add(horario);". I really don't know what to do. Maybe a stupid problem, but I can't find it. Could you help me? I have already tried to use ItemsSource dynamically and xaml ways, but didn't work.</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;UserControl x:Class="Clipboard_final.Horas"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
           mc:Ignorable="d" 
           d:DesignHeight="300" d:DesignWidth="300" Width="1258" Height="596"&gt;
  &lt;Grid Loaded="Grid_Loaded"&gt;
      &lt;Label Content="Horários" Height="61" Name="label1" VerticalAlignment="Top" FontSize="40" Margin="0,-9,1090,0" /&gt;
      &lt;Label Content="Cheque ou adicione novos horários realizados" Height="28" HorizontalAlignment="Left" Margin="29,40,0,0" Name="label2" VerticalAlignment="Top" /&gt;
      &lt;Label Content="Label" Height="32" Margin="0,85,567,0" Name="label3" VerticalAlignment="Top" FontSize="15" FontStretch="Normal" HorizontalAlignment="Right" Width="520" /&gt;
      &lt;Label Content="Entrou" Height="28" HorizontalAlignment="Left" Margin="183,132,0,0" Name="label4" VerticalAlignment="Top" /&gt;
      &lt;Label Content="Saiu" Height="28" HorizontalAlignment="Left" Margin="188,166,0,0" Name="label5" VerticalAlignment="Top" /&gt;
      &lt;Button Content="Enviar" Height="23" HorizontalAlignment="Left" Margin="219,543,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /&gt;
      &lt;TextBox Height="23" HorizontalAlignment="Left" Margin="228,135,0,0" Name="textBox1" VerticalAlignment="Top" Width="46" Text="00" /&gt;
      &lt;Label Content=":" Height="28" HorizontalAlignment="Left" Margin="272,132,0,0" Name="label6" VerticalAlignment="Top" /&gt;
      &lt;TextBox Height="23" HorizontalAlignment="Left" Margin="281,135,0,0" Name="textBox2" VerticalAlignment="Top" Width="46" Text="00" /&gt;
      &lt;TextBox HorizontalAlignment="Left" Margin="228,168,0,0" Name="textBox3" Width="46" Height="23" VerticalAlignment="Top" Text="00" /&gt;
      &lt;Label Content=":" Height="28" HorizontalAlignment="Left" Margin="272,166,0,0" Name="label7" VerticalAlignment="Top" /&gt;
      &lt;TextBox Height="23" HorizontalAlignment="Left" Margin="281,168,0,0" Name="textBox4" VerticalAlignment="Top" Width="46" Text="00" /&gt;
      &lt;TextBox Height="329" HorizontalAlignment="Left" Margin="78,208,0,0" Name="textBox5" VerticalAlignment="Top" Width="380" /&gt;
      &lt;ListView Height="329" HorizontalAlignment="Left" Margin="709,208,0,0" Name="listView1" VerticalAlignment="Top" Width="480" SelectionChanged="listView1_SelectionChanged" SelectionMode="Single"&gt;
          &lt;ListView.View&gt;
              &lt;GridView&gt;
                  &lt;GridViewColumn Header="Entrou" Width="60" DisplayMemberBinding="{Binding hora_entrada}"/&gt;
                  &lt;GridViewColumn Header="Saiu" Width="50" DisplayMemberBinding="{Binding hora_saida}" /&gt;
                  &lt;GridViewColumn Header="Conferido" Width="65" DisplayMemberBinding="{Binding visto}"/&gt;
                  &lt;GridViewColumn Header="Descrição" Width="300" DisplayMemberBinding="{Binding descricao}"/&gt;
              &lt;/GridView&gt;
          &lt;/ListView.View&gt;
      &lt;/ListView&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>    using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      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 MySql.Data.MySqlClient;
      using System.Windows.Controls;
      using System.Collections.ObjectModel;
  
      namespace Clipboard_final
      {
  
  public class Horarios
  {
      public string hora_entrada{ get; set; }
      public string hora_saida { get; set; }
      public string visto { get; set; }
      public string descricao { get; set; }
  
  
  
  }
  public partial class Horas : UserControl
  {
      private int id_user;
      ObservableCollection&lt;Horarios&gt; _Horarios =
      new ObservableCollection&lt;Horarios&gt;();
      public Horas(int _id_user)
      {
  
          id_user = _id_user;
          MySqlConnection oi = new MySqlConnection();
          oi.ConnectionString =
          "server=localhost;"
          + "database=clipboard;"
          + "uid=staff;"
          + "password=clipboardstaff4thewin;";
  
          try
          {
              oi.Open();
              MySqlCommand comando = new MySqlCommand("SELECT horario_entrada,horario_saida,visto,descricao from horarios where ID_Estagiario = @id_user", oi);
              comando.Parameters.Add("@id_user", MySqlDbType.Int32).Value = id_user;
              MySqlDataReader leitor = comando.ExecuteReader();
              while (leitor.Read())
              {
  
                  Horarios horario = (new Horarios
                  {
                      hora_entrada = leitor.GetString(0),
                      hora_saida = leitor.GetString(1),
                      visto = (leitor.GetChar(0)).ToString(),
                      descricao = leitor.GetString(2)
                  });
                  listView1.Items.Add(horario);
  
              }
  
              leitor.Close();
          }
          catch (MySqlException ex)
          {
              MessageBox.Show("Erro de conexão com o banco de dados. Erro " + ex.ToString());
          }
          InitializeComponent();
          label3.Content = "Hoje é dia "+Convert.ToString(DateTime.Now.Day)+" do mês "+Convert.ToString(DateTime.Now.Month)+" de "+Convert.ToString(DateTime.Now.Year);
      }
  
  
      private void button1_Click(object sender, RoutedEventArgs e)
      {
  
          try
          {
              MySqlConnection oi = new MySqlConnection();
              oi.ConnectionString =
              "server=localhost;"
              + "database=clipboard;"
              + "uid=staff;"
              + "password=clipboardstaff4thewin;";
              try
              {
                  oi.Open();
              }
              catch (Exception ex)
              {
                  MessageBox.Show("Erro de conexão com o banco de dados. Erro " + ex.ToString());
              }
              if (textBox5.Text == "")
              {
                  MessageBox.Show("Digite a descrição do dia");
              }
              else
              {
                  MySqlCommand comando = new MySqlCommand("Select id_estagiario from estagiario where id_funcao = @um", oi);
                  comando.Parameters.Add("@um", MySqlDbType.Int32).Value = 1;
                  MySqlDataReader leitor = comando.ExecuteReader();
                  int chefe = 0;
                  while (leitor.Read())
                  {
                      chefe = leitor.GetInt32(0);
                  }
                  leitor.Close();
                  int horaentrada_inteiro = Convert.ToInt32(textBox1.Text);
                  int minutosentrada_inteiro = Convert.ToInt32(textBox2.Text);
                  int horasaida_inteiro = Convert.ToInt32(textBox3.Text);
                  int minutossaida_inteiro = Convert.ToInt32(textBox4.Text);
  
                  if (((horasaida_inteiro - horaentrada_inteiro) == 0 &amp;&amp; (minutossaida_inteiro - minutosentrada_inteiro) &lt;= 0) || horasaida_inteiro - horaentrada_inteiro &lt; 0)
                  {
                      MessageBox.Show("Você digitou incorretamente os horários");
                  }
                  if (horaentrada_inteiro &lt; 13 || horasaida_inteiro &gt; (DateTime.Now.Hour + 1))
                  {
                      if (id_user != chefe)
                      {
                          MessageBox.Show("Ninguém trabalha nestes horários.", "Erro");
                          MySqlCommand comando1 = new MySqlCommand("Select Nome from estagiario where id_estagiario = @id_user", oi);
                          comando1.Parameters.Add("@id_user", MySqlDbType.Int32).Value = id_user;
                          MySqlDataReader leitor1 = comando1.ExecuteReader();
                          string nome = "";
                          while (leitor1.Read())
                          {
                              nome = leitor1.GetString(0);
                          }
                          leitor1.Close();
                          leitor1.Close();
                          comando1 = new MySqlCommand("Insert Into Recado values(0,@warn,0,'Clipboard SecuritySaver',@chefe)", oi);
                          comando1.Parameters.Add("@warn", MySqlDbType.VarChar).Value = "O estagiário " + nome + " tentou burlar o sistemas de horários.";
                          comando1.Parameters.Add("@chefe", MySqlDbType.Int32).Value = chefe;
                          comando1.ExecuteNonQuery();
                      }
                      else
                      {
                          MessageBox.Show("Chefe querido, você digitou os horários incorretamente", "Erro");
                      }
  
                  }
                  else
                  {
  
                      string data = Convert.ToString(DateTime.Now.Day) + '/' + Convert.ToString(DateTime.Now.Month) + '/' + Convert.ToString(DateTime.Now.Year);
                      string hora_entrada = Convert.ToString(horaentrada_inteiro) + ':' + minutosentrada_inteiro.ToString();
                      string hora_saida = horasaida_inteiro.ToString() + ':' + minutossaida_inteiro.ToString();
                      comando = new MySqlCommand("INSERT INTO HORARIOS VALUES (0,@data,@id_user,@hora_entrada,@hora_saida,@descricao,0)", oi);
                      comando.Parameters.Add("@data", MySqlDbType.VarChar).Value = data;
                      comando.Parameters.Add("@id_user", MySqlDbType.Int32).Value = id_user;
                      comando.Parameters.Add("@hora_entrada", MySqlDbType.VarChar).Value = hora_entrada;
                      comando.Parameters.Add("@hora_saida", MySqlDbType.VarChar).Value = hora_saida;
                      comando.Parameters.Add("@descricao", MySqlDbType.VarChar).Value = textBox5.Text;
                      comando.ExecuteNonQuery();
                  }
              }
          }
  
  
          catch (FormatException ex)
          {
              MessageBox.Show("Escreva somente números");
          }
  
      }
  
      private void listView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
  
      }
  
  
  
      private void Grid_Loaded(object sender, RoutedEventArgs e)
      {
  
  
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Sorry for the strange language</p>