How to enter data into an array of objects by a function?

0

I want to enter a random number generated by a function to an array of objects, the problem is that when I want to enter it, it sends me an error, here the code: (I have a class called "Processes" where are the getters and setters methods)

package roundrobin;

/**
 *
 * @author george
 */
public class RoundRobin {

    public static int numale() {
        int numale = 0;
        numale = (int) (Math.random() * (25 - 4 + 1) + 4); //Numeros aleatorios desde 4 hasta 25
        return numale;
    }

    public static void main(String[] args) {

        Procesos pro[] = new Procesos[15];

        pro[0].settempllegada(numale());  //linea 19

        System.out.println(pro[0].getllegada());

    }

}

And this is what I get when I execute:

Exception in thread "main" java.lang.NullPointerException
    at roundrobin.RoundRobin.main(RoundRobin.java:19)
/home/george/.cache/netbeans/8.2/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
    
asked by JORGE MIGUEL NUNEZ MACIEL 18.04.2017 в 20:02
source

3 answers

0

This

Procesos pro[] = new Procesos[15]; 

creates an array of 15 null elements. You have to loop to initialize it

for (int i=0;i<pro.length;i++) {
   pro[i] = new Procesos(...);
}

Greetings.

    
answered by 18.04.2017 в 21:53
0

You have to initialize the object in that position to be able to set the entry

pro[0] = new Proceso();
pro[0].setLLegada(numale());
    
answered by 19.04.2017 в 00:25
-1

class Example {

public static void main(String[] args) {
    Procesos pro[] = new Procesos[15];
    pro[0] = new Procesos(); //te falta este paso el cual tienes que hacer con las 15 posiciones
    pro[0].settempllegada(numale());  
    System.out.println(pro[0].getllegada());
}

}

class Example {

 public static void main(String[] args) {

    Procesos pro[] = new Procesos[15];

    for (int i = 0; i< pro.length;i++) {
      pro[i] = new Procesos();
      pro[i].settempllegada(numale());
    }

  //ya con esto queda instanceado todo tu array.
}

}

    
answered by 10.12.2018 в 01:35