0
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class testing {

    public static int[] populationVectorFicheiro;
    public static double[] survivalMatrixFicheiro;
    public static double[] birthMatrixFicheiro;
    public static double[][] matrizLeslieNaoInterativo = new double[survivalMatrixFicheiro.length + 1][birthMatrixFicheiro.length];

    public static void main(String[] args) throws FileNotFoundException {
        readFileInfo();
    }

    public static double[] formarVetor(String[] line) {
        double[] matrix = new double[line.length];
        for (int i = 0; i < line.length; i++) {
            String[] numbers = line[i].trim().split("=");
            matrix[i] = Double.parseDouble(numbers[i]);
        }
        return matrix;
    }

    public static double[] getValores(String[] linha) {
        double[] matriz = new double[linha.length];
        for (int i = 0; i < linha.length; i++) {
            String[] objeto = linha[i].split("=");
            matriz[i] = Double.parseDouble(objeto[1]);
        }
        return matriz;
    }

    public static int[] getTotalPopulacao(String[] linha) {
        int[] matriz = new int[linha.length];

        for (int i = 0; i < linha.length; i++) {
            String[] objeto = linha[i].split("=");
             matriz[i] = Integer.parseInt(objeto[1]);
        }

        return matriz;
    }

    public static void readFileInfo() throws FileNotFoundException{
        Scanner fileReader = new Scanner(new File("caes.txt"));
        String[] primeiraLinha = fileReader.nextLine().trim().split(",");
        populationVectorFicheiro = getTotalPopulacao(primeiraLinha);

        String[] segundaLinha = fileReader.nextLine().trim().split(",");
        survivalMatrixFicheiro = getValores(segundaLinha);
        for (int i=0; i< survivalMatrixFicheiro.length;i++){
            System.out.println(survivalMatrixFicheiro[i]);
        }


        String[] terceiraLinha = fileReader.nextLine().trim().split(",");
        birthMatrixFicheiro = getValores(terceiraLinha);

        double[][] leslieMatrix = fillMatrix(survivalMatrixFicheiro,birthMatrixFicheiro);
        printMatrix(leslieMatrix);

    }

    public static void printMatrix(double[][] matriz) {
        for (int i = 0; i < matriz.length; i++)//Cycles through rows
        {
            for (int j = 0; j < matriz[i].length; j++)//Cycles through columns
            {
                System.out.print(matriz[i][j] + " ");
            }
            System.out.println(); //Makes a new row
        }
    }


    public static double[][] fillMatrix(double[] survivalMatrix, double[] birthMatrix) {

        for (int i = 0; i < birthMatrix.length; i++) {
            matrizLeslieNaoInterativo[0][i] = birthMatrix[i];         // preenche a primeira linha
        }
        int count = 0;
        for (int i = 1; i < survivalMatrix.length + 1; i++) {
            for (int j = 0; j < birthMatrix.length; j++) {
                if (i == j + 1) {
                    matrizLeslieNaoInterativo[i][j] = survivalMatrix[count];
                    count++;
                } else {
                    matrizLeslieNaoInterativo[i][j] = 0;
                }
            }
        }
        return matrizLeslieNaoInterativo;
    }

}

I currently have this code in Java for some testing for a bigger project. I'm trying to create some public variables to use throughout my bigger project, but I keep getting an error while trying to use them here.

Whenever I create a public matrix with other public variables as its parameters, I keep getting an error:

Exception in thread "main" java.lang.ExceptionInInitializerError Caused by: java.lang.NullPointerException: Cannot read the array length because "testing.survivalMatrixFicheiro" is null

Is there anyway to fix this? I need to keep that matrix as a public variable for other methods on the bigger project.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
patxico
  • 1
  • 1
  • 2
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) –  Jan 23 '21 at 21:42
  • When you initialize static field `matrizLeslieNaoInterativo` you try to use the length of `survivalMatrixFicheiro` which is `null` at this time. – Nowhere Man Jan 23 '21 at 22:08

0 Answers0