使用节点的PriorityQueue中的nullPointerException

时间:2018-02-16 04:02:48

标签: java nullpointerexception

当我运行程序时,它没有告诉我哪一行我有一个nullPointerException,并且我一直试图解决这个问题几个小时。有人可以看看,可能修复代码?我是Java的新手,所以我刚刚开始。请不要将其标记为重复,我已经看到了nullpointerException,它并没有真正帮助,因为当我运行程序时,它不会告诉我空指针在哪一行。

Exception in thread "main" java.lang.NullPointerException
at Scheduler.executeProcesses(Scheduler.java:13)
at Scheduler.main(Scheduler.java:38)

代码:

import java.util.LinkedList;

public class PriorityQueue implements Queue {

Node head = new Node(null);

class Node {
    Process process;
    Node next;


    Node(Process p) {
        this.process = p;
        this.next = null;

    }
}
Node first;
@Override
public void offer(Process p) {
    Node newNode = new Node(p);
    Node previous = null;
    Node current = first;

    while (current != null) {
        previous = current;
        current = current.next;
    }
    if (previous == null) {
        newNode.next = first;
        first = newNode;
    }
    else {
        previous.next = newNode;
        newNode.next = current;
    }
}

@Override
public Process poll() {
    Node previous = null;
    Node current = first;
    //Node temp = current;

    while (current.next != null) {
        previous = current;
        current = current.next;
    }
    previous.next=null;

    return null;
}

@Override
public boolean isEmpty() {
    return first == null;

}

计划程序类:

import java.util.List;

public class Scheduler {

static double idleTime = 0;
static Queue queue = new PriorityQueue();

public static void executeProcesses() {
    double currentTime = 0;
    System.out.println("Simulation starts with default parameters.");
    while (!queue.isEmpty()) {
        Process process = queue.poll();
        if (process.getArrivalTime() > currentTime) {
            idleTime = process.getArrivalTime() - currentTime;
            currentTime = process.getArrivalTime();
        }
        System.out.printf("process %d with priority %d started at time %f.\n", process.getId(),
                process.getPriority(), currentTime);
        do {
            double currentBurstTime = process.getNextBurstTime();
            System.out.printf("process %d has CPU burst %f.\n", process.getId(), currentBurstTime);
            currentTime += currentBurstTime;
        } while (process.hasMoreBurst());
        System.out.printf("process %d with priority %d completes and terminates at %f.\n", process.getId(),
                process.getPriority(), currentTime);
    }

    System.out.println("Simulation summary");
    System.out.printf("CPU idle time: %f\n", idleTime);
    System.out.printf("CPU utilization percentage: %f\n", (currentTime - idleTime) / currentTime);
    System.out.println("Signature name: " + queue.getSignature());
}

public static void main(String[] args) {
    ProcessFactory factory = new ProcessFactory();
    List<Process> processList = factory.batchGenerate();
    processList.forEach(p -> queue.offer(p));
    executeProcesses();
}
}

0 个答案:

没有答案