title: Null pointer exception, "Attempt to read from field on a null object reference"
body: <p>I am making an app in which the user types in a list of tasks, and that list is saved to an array. Each task in the array is an instance of the <code>Assignment</code> class. However, I realized that in java it is not possible to add an element to an array after the array is created. So, what I did was I created an array called <code>tasks</code> which consisted of many null values: <code>Assignment[]tasks = {null, null, null, null, null, null, null, null, null, null, null, null};</code>. When I want to add a task to the array, I just replace the next null value with the object. However, I also need to have an array of just the tasks, with no null values. So I created an array called <code>full_tasks</code> for all of the elements that aren't null:</p>
  
  <pre><code>for (Assignment task: tasks) {
      if (task != null) {
          realLength += 1;
      }
  }
  
  Assignment[] full_tasks = new Assignment[realLength];
  
  for (int i=0; i &lt;= full_tasks.length - 1; i++) {
          full_tasks[i] = new Assignment(tasks[i].name, tasks[i].days_due, tasks[i].time);
  }
  </code></pre>
  
  <p>So now, the <code>full_tasks</code> array should be an array of all the tasks, none of which are null, right? However, when I run the app, it can't launch the activity, an error it says is caused by a null pointer exception:</p>
  
  <pre><code> Caused by: java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.example.lb.homeworkappv11.Assignment.name' on a null object reference
          at com.example.lb.homeworkappv11.Schedule.sortTasks(Schedule.java:64)
  </code></pre>
  
  <p>The line that the error points to is:</p>
  
  <pre><code>full_tasks[i] = new Assignment(tasks[i].name, tasks[i].days_due, tasks[i].time);
  </code></pre>
  
  <p>I'm still not totally sure what a null object reference is, but I think it means that one of the elements in the <code>full_tasks</code> array is null. Would that be correct? And if it is, what can I do to make sure that the <code>full_tasks</code> array is <strong>only</strong> the non-null elements in the <code>tasks</code> array?</p>
  
  <p>Thank you so much!</p>
  
  <p><strong>Edit:</strong> the constructor function for the assignment class is:</p>
  
  <pre><code>public Assignment(String name, int days, int time) {
      this.name = name;
      this.days_due = days;
      this.time = time;
      this.toSortBy = "nothing";
  }
  </code></pre>