Null pointer exception, "Attempt to read from field on a null object reference"
A null
reference is just that null
. In your code it is tasks[i].name
where you try to call name
on tasks[i]
so tasks[i]
is null
.
There is one scenario I can think of, where your code would definitely throw a NullPointerException
.
So, I will assume your tasks array can look looks like this:
tasks = [task0, null, task2, task3, null, task5]
Then full_tasks
will have a size of 4 but
for (int i=0; i <= full_tasks.length - 1; i++) {
full_tasks[i] = new Assignment(tasks[i].name, tasks[i].days_due, tasks[i].time);
}
will throw a NPE as soon as i == 1
because tasks[1]
is null
.
So, if you want to fill full_tasks
with only non-null tasks make sure you got the right indexes of tasks
.