gpt4 book ai didi

java - 空指针异常在哪里?

转载 作者:行者123 更新时间:2023-12-02 11:08:01 24 4
gpt4 key购买 nike

我正在构建一个程序作为CPU进程处理程序,每个进程都有 double 值,进程类中没有静态方法或变量,突发时间是随机生成的,我的方法是将等待时间计算为双计数器,从进程数组中,我将每个进程作为对象添加到 arraylist 中,以将它们添加到 jtable 中,但它在

中给了我一个空指针异常
process1[0].setWaitingtime(waittimecounter + process1[0].getWaitingtime());

为什么会发生这种情况,有人可以给我一个解决方案吗?谢谢这是我的代码

public class start extends javax.swing.JFrame {

public int processnumber = 100;
//public int processnumber = 100;
public int degree = 20;
public int contextswitch = 2;
public int timecountom = 10;

/**
* Creates new form start
*/
public start() {
initComponents();
addRowToJTable();
}

public ArrayList listprocess() {
ArrayList<process> list = new ArrayList<process>();
process[] p = new process[processnumber];
fcfs(p);
for (int i = 0; i < processnumber; i++) {
list.add(p[i]);
}
return list;
}

public void addRowToJTable() {
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
ArrayList<process> list = listprocess();

Object rowData[] = new Object[4];
for (int i = 0; i < list.size(); i++) {
rowData[0] = i;
rowData[1] = list.get(i).getBursttime();
rowData[2] = list.get(i).getWaitingtime();
rowData[3] = list.get(i).getTurnaroundtime();
model.addRow(rowData);
}
}

public void fcfs(process[] proces) {
double turnaroundtimecounter = 0;
double waittimecounter = 0;
double turnaroundcounter = 0;
int index = degree - 1;
double avgwaitingtime = 0;
double avgturnaroundtime = 0;
process[] process1 = new process[degree];
for (int i = 0; i < degree; i++) {
process1[i] = proces[i];
}
//for(int j=0;j<processnumber;j++)
for (int i = 0; i < processnumber; i++) {
process1[0].setWaitingtime(waittimecounter + process1[0].getWaitingtime());
turnaroundcounter = process1[0].getBursttime() + turnaroundcounter;
process1[0].setTurnaroundtime(turnaroundcounter);
waittimecounter = process1[0].getBursttime() + waittimecounter;
proces[i] = process1[0];
for (int f = 0; f < degree; f++) {
process1[f] = process1[f + 1];
}
if (index < 100) {
process1[19] = process1[index];
process1[19].setWaitingtime(-waittimecounter);
index++;
}
}
avgwaitingtime = waittimecounter / processnumber;
avgturnaroundtime = turnaroundcounter / processnumber;
}

最佳答案

您已创建进程数组但从未初始化。

关于java - 空指针异常在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50789664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com