I am creating an application in which the user enters a list of tasks, and this list is stored in an array. Each task in the array is an instance of the class Assignment. However, I realized that in java it is not possible to add an element to an array after creating the array. So what I've done, I've created an array with the name tasks, which consisted of many zero values: Assignment[]tasks = {null, null, null, null, null, null, null, null, null, null, null, null};. When I want to add a task to an array, I simply replace the following null object. However, I also need to have an array of tasks only, no null values. Therefore, I created an array with a name full_tasksfor all elements that are not null:
for (Assignment task: tasks) {
if (task != null) {
realLength += 1;
}
}
Assignment[] full_tasks = new Assignment[realLength];
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);
}
, full_tasks , , ? , , , , , null-:
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)
, :
full_tasks[i] = new Assignment(tasks[i].name, tasks[i].days_due, tasks[i].time);
, , , , full_tasks . ? , , , full_tasks tasks?
!
: - :
public Assignment(String name, int days, int time) {
this.name = name;
this.days_due = days;
this.time = time;
this.toSortBy = "nothing";
}