英文:
Java - How can I add an object to a list of another object
问题
我有一个名为"Process"的类和一个名为"Event"的类。"Process"类具有一个事件列表作为其属性之一。我正在读取一个文件并填充"Process"属性和"Event"属性,但是当我尝试将"Event"添加到"Process"对象的事件列表中时,我会得到"NullPointerException"。我在C#方面有更多经验,但我必须在Java中完成这个任务。
文件中的一行如下所示:
0 1 CPU 10 IO 3 CPU 50 IO 3 CPU 10
以下是这些类:
"Process"类:
public class Process {
int arrivalTime;
int priority;
LinkedList<Event> events;
}
"Event.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();
}
}
}
英文:
I have an Class called Process and a Class Called Event. Process has a list of Events as one of its attributes. I am reading a file and populating the Process attributes and the Event attributes but when I try to add the Event to the events list of the Process object I get NullPointerException. I have more experience in C# but I have to do this in Java.
This is what a line in the file looks like:
0 1 CPU 10 IO 3 CPU 50 IO 3 CPU 10
These are the classes:
public class Process {
int arrivalTime;
int priority;
LinkedList<Event> events;
}
Event.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();
}
}
}
答案1
得分: 1
我猜这可能是因为没有初始化事件。你可以尝试进行以下操作:
public class Process {
int arrivalTime;
int priority;
LinkedList<Event> events = new LinkedList();
}
英文:
I guess it is due to not initializing the events. can you try doing the below
public class Process {
int arrivalTime;
int priority;
LinkedList<Event> events = new LinkedList();
}
答案2
得分: 1
你尚未初始化events
列表。要么在Process
类中将其初始化为空列表,要么在创建Process
对象后进行初始化。
在Java中,如果不初始化一个类类型,它会默认为null。
英文:
You have not initialized the events
list. Either initialize it in the Process
class itself to an empty list or after creating Process
object.
In Java, if you do not initialize a Class type, it defaults to null.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论