У меня есть класс с именем Process и Class Called Event. Процесс имеет список событий в качестве одного из своих атрибутов. Я читаю файл и заполняю атрибуты процесса и атрибуты события, но при попытке добавить событие в список событий объекта процесса я получаю исключение NullPointerException. У меня больше опыта в C#, но я должен сделать это в Java. Вот как выглядит строка в файле:
0 1 CPU 10 IO 3 CPU 50 IO 3 CPU 10
Это классы:
public class Process {
int arrivalTime;
int priority;
LinkedList<Event> events;
}
Событие. java,
public class Event {
String type;
int duration;
}
FCFS . java,
public class FCFS{
public static void main(String[] args) {
List<Process> readyQueue = new LinkedList<Process>();
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader("path.txt"));
String line = reader.readLine();
while (line != null) {
String[] arr = line.split(" ");
Process process = new Process();
process.arrivalTime = Integer.parseInt(arr[0]);
process.priority = Integer.parseInt(arr[1]);
for(int i = 2; i < arr.length; i = i + 2) {
Event event = new Event();
event.type = arr[i];
event.duration = Integer.parseInt(arr[i+1]);
process.events.add(event);
}
readyQueue.add(process);
// read next line
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}