0

i am building a program as a cpu process handler and each process have doubles value there are no static methods or variables in process class and bursttime is randomly generated, the method i have is to compute the waiting time as a double counter, from an array of processes i add each process as an object to arraylist to add them to jtable but it gives me a null pointer exception in

process1[0].setWaitingtime(waittimecounter + process1[0].getWaitingtime());

why is this happening can any one give me a solution to try? thank you this is my code

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;
    }
markspace
  • 9,246
  • 2
  • 20
  • 35
  • 2
    Probably `process1[0]` is null. – markspace Jun 11 '18 at 02:06
  • As an unrelated side note, there's an [`Arrays.asList`](https://docs.oracle.com/javase/10/docs/api/java/util/Arrays.html#asList(T...)) command to turn an array into a `List`. Although it does make me wonder why you're not using a List in the first place. – Powerlord Jun 11 '18 at 02:11

1 Answers1

1

You have created array of process but never intialized.

Gaurav Srivastav
  • 1,960
  • 1
  • 11
  • 17