Java – 如何将一个对象添加到另一个对象的列表中

huangapple go评论74阅读模式
英文:

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&lt;Event&gt; events;
}

Event.java,

public class Event {
String type;
int duration;
}

FCFS.java,

public class FCFS{

public static void main(String[] args) {
	List&lt;Process&gt; readyQueue = new LinkedList&lt;Process&gt;();
	
	BufferedReader reader;
	try {
		reader = new BufferedReader(new FileReader(&quot;path.txt&quot;));
		String line = reader.readLine();
		while (line != null) {
			String[] arr = line.split(&quot; &quot;);
			
			Process process = new Process();
			
			process.arrivalTime = Integer.parseInt(arr[0]);
			process.priority = Integer.parseInt(arr[1]);
			
			for(int i = 2; i &lt; 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&lt;Event&gt; 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.

huangapple
  • 本文由 发表于 2020年5月2日 14:24:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/61555341.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定