0

I'm trying to create a simple JavaFX application that switches between 2 screens, one where you can change the variables stored and one that just displays, I have a function defined in the main method that just returns a nullpointerexception when compiling the project in the error log it says its on the line that reads

taskList[i].setTaskName(taskNames[i]);

I tried adding an if statement, similar to the one for the section for the getTechAssigned method, because this variable will be constantly changing, while the taskName variable will be unchanged. I'm just not sure why it's coming up, here's the full code:

variable declarations

Button assignTask, finalizeTask;
    FlowPane pane1, pane2;
    Scene scene1, scene2;
    Stage theStage;
    String[] taskNames = {"Notes", "Paging", "Event Mgmt - Early", 
                          "Event Mgmt -Late", "Airhub 2DA", "Airhub 1DA"
                          "GSSD", "GSSI", "GSSRR", "Publishing",
                          "Check Holdovers", "Radio", "PTI Walkoff", "Phones
                          1300"};
    task[] taskList = new task[taskNames.length];
    GridPane taskGrid = new GridPane();

function that is causing the error:

public void initializeTasks() {
    for (int i = 0; i < taskList.length; i++) {
        taskList[i].setTaskName(taskNames[i]);
        Text t = new Text(taskList[i].getTaskName());
        taskGrid.setRowIndex(t, i);
        taskGrid.setColumnIndex(t, 0);

        if(taskList[i].getTechAssigned() == null) {
            taskList[i].setTechAssigned("Not Assigned");
            }

        Text r = new Text(taskList[i].getTaskName());
        taskGrid.setRowIndex(r, i);
        taskGrid.setColumnIndex(r, 1);
        }

    }

2 Answers2

0

You created the array but didn't put anything in it. Your array contains taskNames.length elements, all of which are null.

You should init all elements of array like:

taskList[i] = new task();

before the line where you set taskList[i].setTaskName(taskNames[i]);

0
 task[] taskList = new task[taskNames.length];

taskList currently has 10 null elements as per your init since you are using setTaskName on null its giving error.

First add object to taskList and then use it.

taskList[i]=new task();
taskList[i].setTaskName(taskNames[i]);
gladiator
  • 1,249
  • 8
  • 23