从键盘读取期间出现 Java NullPointerException
我需要从键盘读取值到表,我猜它超出了范围,但不知道如何修复它。
线程“main”中出现异常 java.lang.NullPointerException 在 B12.App.main(App.java:36)
这是那行 tab.matrix[ai][aj]=parser;
整个代码:
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;
}
}
I need to read values from keyboard to table, and I guess it's going out of range, but can't figure out how to fix it.
Exception in thread "main" java.lang.NullPointerException
at B12.App.main(App.java:36)
This is that 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为 double matrix[][]; 未初始化,其
null
This is because
double matrix[][];
is not initilized , itsnull
您永远不会实例化
Matrix
类的matrix
变量。当您在此行构建矩阵时:rozmiar
和matrix
字段将被初始化为默认(空)值,对于矩阵
字段。然后这个值以后就不会被分配了,但是你尝试在有问题的行上引用它,这会创建 NullPointerException。
解决方法是在知道大小时初始化矩阵,如第 23 行所示:
请注意,这是一个二维数组,因此您还需要创建每个嵌套数组,例如第 32 行
:不过,更好的解决方案可能是完全摆脱 Matrix 的空构造函数,而是添加一个采用整数参数并设置字段的构造函数。这是一个改进,因为不可能在无效状态下构造一个对象 - 在构造函数中要求参数总是更好,而不是稍后初始化一个空对象。
所以这可能看起来像:
}
然后要使用它,您不会首先创建一个空的
tab
,而是计算出创建它的大小,如下所示:You never instantiate the
Matrix
class'matrix
variable. When you construct the Matrix on this line:the
rozmiar
andmatrix
fields will be initialised to default (empty) values, which isnull
for thematrix
field.Then this is never assigned later, but you try to deference it on the line in question, which creates the NullPointerException.
The fix is to initialise the matrix when you know the size, something like this on line 23:
Note that this is a two-dimensional array, and so you'll need to create each nested array as well, e.g. on line 32:
A better solution though, might be to get rid of Matrix's empty constructor altogether, and instead add a constructor that takes an integer argument and sets the fields. This is an improvement since then it's not possible to construct an object in an invalid state - it's always better to require arguments in the constructor, rather than to initialise an empty object later.
So this might look like:
}
and then to use it, you wouldn't create an empty
tab
first, but would work out the size to create it, something like this:您的数组已声明但从未实例化。添加此行:
在此之后:
Your array is declared but never instantiated. Add this line:
after this: