I try to add each position of column 0 of the position array and store it in the transit array [duplicated]

0
import java.util.Random;
import java.util.Scanner;


public class Version2 {

    static double procesos[][];
    static int n;
    static double tiempo=0;
    static double transito[];
    static double diferencia=0;

    public static void main(String[] args) {



        generarDatos();
        System.out.println("\nDatos aleatorios");     
        imprimirDatos();
        ordernar_Tprocesamiento();
        System.out.println("\nDatos ordenados");        
        imprimirDatos();
        Ttransito();




    }

    private static void generarDatos() {
        Random valores = new Random();
        Scanner entrada = new Scanner(System.in);
        System.out.print("Entre el numero de tareas: ");
        n = entrada.nextInt();

        // Crea el vector nombre y la matriz examen
        procesos = new double[n][2];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 2; j++) {
                procesos[i][j] = valores.nextGaussian()*10 +100;
            }
        }
    }


        //En la posición 0,0 de la matriz se define el tiempo de procesamiento
        //En la posición 1,0 de la matriz se define el plazo faltante

    private static void imprimirDatos() {

        for (int i = 0; i < n ; i++) {
            System.out.print("\n");
            for (int j = 0; j < 2; j++) {

                    System.out.printf(String.format("%.2f", procesos[i][j]) + "   ");                 
            }
        }
    }
    //Ordena por tiempo de procesamiento

    private static void ordernar_Tprocesamiento() {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (procesos[j][0] > procesos[j + 1][0]) {
                    // Intercambia dos filas de la matriz
                    double[] filaTemp = procesos[j];
                    procesos[j] = procesos[j + 1];
                    procesos[j + 1] = filaTemp;

                }
            }

        }


    }  

    // aqui se me presenta el error, al tratar de sumar las posiciones de la primera columna del array posicion 
   private static void Ttransito(){
        for (int i = 0; i < n; i++){

                tiempo = tiempo + procesos[i][0];
                    transito[i]=tiempo;

    }

  }
}

In the end, this error is thrown

Enter the number of tasks: 5

Random data

102,38 84,59
101,22 87,27
117.50 102.96
110.34 103.68
102,50 99,03
Sorted data

101,22 87,27
102,38 84,59
102,50 99,03
110.34 103.68
117.50 102.96 102.37715253750525

  

Exception in thread "main" java.lang.NullPointerException at   Version2.Transmission (Version2.java:84) at   Version2.main (Version2.java:23)

    
asked by Esteban 25.09.2018 в 17:59
source

1 answer

0

As the error tells you, the problem is that you are trying to put an array in a variable that is not even an array and that's why it's null.

The first thing is to declare well the array transito :

double[] transito; //Ahora si que es un array de dobles

In the second place it is necessary to declare the size of this array. So when calculating the int n , which is the size you want to give, we pass it to the array:

transito = new double[n]; //Ajusta tamaño del array

And all the code would look like this:

import java.util.Random;
import java.util.Scanner;


public class Version2 {

    static double procesos[][];
    static int n;
    static double tiempo=0;
    static double[] transito;
    static double diferencia=0;

    public static void main(String[] args) {



        generarDatos();
        System.out.println("\nDatos aleatorios");     
        imprimirDatos();
        ordernar_Tprocesamiento();
        System.out.println("\nDatos ordenados");        
        imprimirDatos();
        Ttransito();




    }

    private static void generarDatos() {
        Random valores = new Random();
        Scanner entrada = new Scanner(System.in);
        System.out.print("Entre el numero de tareas: ");
        n = entrada.nextInt();
        transito = new double[n];
        // Crea el vector nombre y la matriz examen
        procesos = new double[n][2];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 2; j++) {
                procesos[i][j] = valores.nextGaussian()*10 +100;
            }
        }
    }


        //En la posición 0,0 de la matriz se define el tiempo de procesamiento
        //En la posición 1,0 de la matriz se define el plazo faltante

    private static void imprimirDatos() {

        for (int i = 0; i < n ; i++) {
            System.out.print("\n");
            for (int j = 0; j < 2; j++) {

                    System.out.printf(String.format("%.2f", procesos[i][j]) + "   ");                 
            }
        }
    }
    //Ordena por tiempo de procesamiento

    private static void ordernar_Tprocesamiento() {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (procesos[j][0] > procesos[j + 1][0]) {
                    // Intercambia dos filas de la matriz
                    double[] filaTemp = procesos[j];
                    procesos[j] = procesos[j + 1];
                    procesos[j + 1] = filaTemp;

                }
            }

        }


    }  

    // aqui se me presenta el error, al tratar de sumar las posiciones de la primera columna del array posicion 
   private static void Ttransito(){
        for (int i = 0; i < n; i++){

                tiempo = tiempo + procesos[i][0];
                    transito[i]=tiempo;

    }

  }
}
    
answered by 26.09.2018 в 10:38