3
I am learning about constructors in java but I stopped in college exercise that seemed simple, the proposal is to create a constructor class with a matrix, and call it in another class to assign the values to the matrix:
Builder:
public class Populacao {
public void atualizarPopulacao(int i, int j, int populacao){
if (i>=0 && i<4 && j>=0 && j<5 && populacao > 0)
pop[i][j] = populacao;
}
}
Main class:
import javax.swing.JOptionPane;
public class Administracao {
public static void main(String p[]) {
Populacao pop = new Populacao();
int i,j;
int n;
for (i=0; i<4; i++){
for(j=0; j<5; j++){
n = Integer.parseInt(JOptionPane.showInputDialog("Populacão: " + String.valueOf(i+1) + "\nEstado: " + String.valueOf(j+1)));
pop.atualizarPopulacao(i, j, n);
}
}
}
}
But I get the error:
Exception in thread "main" java.lang.Nullpointerexception
Can someone help me assign the values to the matrix by calling the constructor?
The problem is that you need to instantiate the matrix. Example:
pop = new int[4][5]
– utluiz