我正在构建一个程序作为cpu进程处理程序,并且每个进程都有双精度值,进程类中没有静态方法或变量,并且随机生成突发时间,我的方法是将等待时间计算为双计数器,从一个进程数组我添加每个进程作为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;
}
答案 0 :(得分:1)
您已经创建了一系列进程但从未初始化。