我正在做一個模擬正在處理的CPU作業的項目。基本上,用戶將輸入一個長度將被處理的作業和一個優先級鍵(從-20到19,從-20開始獲得更高優先級)。到目前爲止,我已經完成了所有這些工作,除了程序正常終止外。基本上我需要程序在我的優先級隊列中的所有作業都被處理完後終止。當每個作業得到處理時,它將從長度減去,直到它爲0.當它爲0時,它將從優先級隊列中移除。當沒有作業剩餘時,(優先隊列爲空),程序應該在輸入n後不終止任務。然而,由於某種原因,我完成了我的代碼的方式,它不是終止。相反,當我輸入n時,當沒有作業需要輸入時,它會給我一個空指針錯誤。任何人都可以指出我可能犯了什麼錯誤嗎?謝謝。如何讓我的Java程序終止?
樣品輸入輸出程序將具有以下格式(作業名稱,長度,優先權)執行以下操作:
輸入要處理的作業:
I:JOB1 3 0
ö :JOB1輸入新的要處理的作業:
我:N
○:JOB1輸入新待處理的作業:
我:作業2 1-3
○:作業2輸入新的要處理的作業:
我:N
○:JOB1輸入要處理的新的工作:
我:N
這是我的代碼:
import java.util.Scanner;
import java.lang.Integer;
public class PQScheduler {
String command = "";
String process = "";
String[] inputParts = null;
SLPQueue Q;
boolean empty = false;
PQScheduler(String inputCommand) {
Q = new SLPQueue();
Scanner comReader = new Scanner(System.in);
this.command = inputCommand;
this.inputParts = command.split("\\s"); // will split input at whitespace and store parts into array
while (empty == false) { // will loop until priority queue becomes empty
if (this.inputParts[0].equals("n")) { // input of no new jobs, will continue processing previous jobs however
if (Q.isEmpty()) {
System.exit(0);
}
else {
this.process(Q.entries.getHead(), Q.entries.getHead().getValue());
}
}
else {
int value = Integer.parseInt(inputParts[1]); // parses value of length into an integer
int key = Integer.parseInt(inputParts[2]); // parse value of key into an integer
try {
Q.insert(inputParts[0], key, value); // inserts job, length, and key into priority queue
} catch (InvalidKeyException e) {
e.printStackTrace();
}
this.process(Q.entries.getHead(), Q.entries.getHead().getValue()); // processes the highest priority job
}
System.out.println("Enter new job to be processed: ");
command = comReader.nextLine();
inputParts = command.split("\\s");
}
}
public void process(Node n, int value) {
if (value == 0) {
try {
Q.removeMin(); // removes the finished job
if (Q.isEmpty()) {
empty = true;
}
else {
process(Q.entries.getHead(), Q.entries.getHead().getValue()); // processes next highest priority job
}
} catch (EmptyPriorityQueueException e) {
e.printStackTrace();
}
}
else {
n.setValue(value - 1); // decrements job length by 1 when processed
process = n.getElement();
System.out.println(process);
}
}
public static void main(String[] args) {
String inputCommand = "";
Scanner mainReader = new Scanner(System.in);
System.out.println("Enter the job to be processed: "); // takes in user input of job, length, and priority
inputCommand = mainReader.nextLine();
PQScheduler scheduler = new PQScheduler(inputCommand);
}
}
public class SLPQueue implements PQueue {
protected SLList entries;
protected int size;
public SLPQueue() {
entries = new SLList(); // creates a new linked list to store elements
size = 0;
}
public String min() throws EmptyPriorityQueueException {
if(entries.isEmpty()) throw new EmptyPriorityQueueException("Priority Queue is empty.");
else
return entries.getHead().getElement(); // gets the first node in priority queue
}
public Node insert(String e, int k, int v) throws InvalidKeyException {
Node tempNode = new Node(e, k, v, null);
insertEntry(tempNode);
return tempNode; // insertion method to add values to node and then insert into priority queue
}
protected void insertEntry(Node n) { // insertion method to add into priority queue
if (entries.isEmpty()) { // checks if it is empty, if so, it will make it head of list
entries.addFirst(n);
size++;
}
else if (compare(n.getKey(), entries.getTail().getKey()) > 0) { // checks to see if lower priority key inserted
entries.addLast(n); // adds last if key inserted is lower priority than the current tail of list
size++;
}
else if (compare(n.getKey(), entries.getHead().getKey()) < 0) {
entries.addFirst(n); // adds first if key inserted is higher priority than the current head of list
size++;
}
else {
Node temp1 = entries.getHead(); // starts traversal from beginning of list
Node temp2 = temp1; // creates a second temporary node to "capture" node for node n to be inserted after
while (compare(n.getKey(), temp1.getKey()) > 0) { // loop to see if keys are of lower priority
if (temp1.equals(entries.getHead())) { // if statement created to set back temp2 by one node
temp1 = temp1.getNext();
}
else {
temp2 = temp2.getNext(); // gets the node value before temp1
temp1 = temp1.getNext(); // gets the next temp1 value
}
}
temp2.setNext(n); // loop will break when key has higher priority than temp1 key found
n.setNext(temp1);
size++;
}
}
public int compare(int k1, int k2) { // comparator method to check for key priorities
int compare = 0;
if (k1 >= k2) {
compare = 1; // returns 1 if first key is of lower priority than second key
}
else if (k1 < k2) { // returns -1 if first key is of higher priority than second key
compare = -1;
}
return compare;
}
public Node removeMin() throws EmptyPriorityQueueException { // removal method to remove from head of list
if (entries.isEmpty()) throw new EmptyPriorityQueueException("Priority Queue is empty.");
else {
return entries.removeFirst();
}
}
public int size() { // returns size of priority queue list
return size;
}
public boolean isEmpty() { // checks to see if priority queue list is empty
return size == 0;
}
}
什麼線產生的NPE? –
線程「main」中的異常java.lang.NullPointerException \t at PQScheduler.process(PQScheduler.java:57) \t at PQScheduler。(PQScheduler.java:29) \t at PQScheduler.main(PQScheduler.java:78) – Asdeev
這是它給我的例外。 – Asdeev