Java NullPointerException during read from keyboard -
i need read values keyboard table, , guess it's going out of range, can't figure out how fix it.
exception in thread "main" java.lang.nullpointerexception @ b12.app.main(app.java:36)
this line tab.matrix[ai][aj]=parser;
whole code:
import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; class matrix{ public matrix() { } int rozmiar; double matrix[][]; } public class app { public static void main(string[] args){ matrix tab = new matrix(); int parser; bufferedreader br = new bufferedreader(new inputstreamreader(system.in) ); system.out.println("podaj rozmiar macierzy: "); try { parser = integer.parseint(br.readline()); tab.rozmiar = parser; } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } system.out.println("uzupelnij wiersze macierzy: "); for(int ai=1; ai<=tab.rozmiar; ai++){ for(int aj=1; aj<=tab.rozmiar; aj++){ bufferedreader zm = new bufferedreader(new inputstreamreader(system.in) ); try { parser = integer.parseint(zm.readline()); tab.matrix[ai][aj]=parser; } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } } drukuj(tab); } static void drukuj(matrix m){ int ai,aj; for(ai=1; ai>=m.rozmiar; ai++) { for(aj=1; aj>=m.rozmiar; aj++) { if(ai==1){ system.out.println("[" + m.matrix[ai][aj]); } if(ai!=1 && ai==m.rozmiar){ system.out.println(" " + m.matrix[ai][aj] + " " ); } else if(ai==m.rozmiar){ system.out.println(m.matrix[ai][aj] + "]" ); } } } } public double det(matrix m){ double wynik = 0; if(m.rozmiar==1){ wynik=m.matrix[1][1]; } else if(m.rozmiar==2){ wynik=(m.matrix[1][1]*m.matrix[2][2])-(m.matrix[1][2]*m.matrix[2][1]); } else{ for(int n=1; n<m.rozmiar +1; n++){ matrix tmpm = new matrix(); tmpm.rozmiar=m.rozmiar - 1; int k=1,j; for(j=2; j<m.rozmiar; j++){ for(k=1; k<m.rozmiar; k++){ if(k>n) tmpm.matrix[j-1][k] = m.matrix[j][k]; else if(k<n) tmpm.matrix[j-1][k-1] = m.matrix[j][k]; } } wynik+=m.matrix[1][n]*math.pow(-1, (j+k))*det(tmpm); } } return wynik; } }
your array declared never instantiated. add line:
tab.matrix = new double[parser][parser];
after this:
tab.rozmiar = parser;
Comments
Post a Comment