从键盘读取期间出现 Java NullPointerException

发布于 2024-10-02 07:37:39 字数 3170 浏览 5 评论 0原文

我需要从键盘读取值到表,我猜它超出了范围,但不知道如何修复它。

线程“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

纵山崖 2024-10-09 07:37:39

这是因为 double matrix[][]; 未初始化,其 null

This is because double matrix[][]; is not initilized , its null

泅人 2024-10-09 07:37:39

您永远不会实例化 Matrix 类的 matrix 变量。当您在此行构建矩阵时:

Matrix tab = new Matrix();

rozmiarmatrix 字段将被初始化为默认(空)值,对于矩阵字段。

然后这个值以后就不会被分配了,但是你尝试在有问题的行上引用它,这会创建 NullPointerException。

解决方法是在知道大小时初始化矩阵,如第 23 行所示:

tab.rozmiar = parser;
tab.matrix = new double[parser][];

请注意,这是一个二维数组,因此您还需要创建每个嵌套数组,例如第 32 行

for(int ai=1; ai<=tab.rozmiar; ai++){
    tab.matrix[ai] = new double[tab.rozmiar];
    for(int aj=1; aj<=tab.rozmiar; aj++){

:不过,更好的解决方案可能是完全摆脱 Matrix 的空构造函数,而是添加一个采用整数参数并设置字段的构造函数。这是一个改进,因为不可能在无效状态下构造一个对象 - 在构造函数中要求参数总是更好,而不是稍后初始化一个空对象。

所以这可能看起来像:

class Matrix{
public Matrix(int size) {
    rozmier = size;
    matrix = new double[size][];
    for (int i = 0; i < size; i++)
    {
        matrix[i] = new double[size];
    }
}
final int rozmiar;
final double matrix[][];

}

然后要使用它,您不会首先创建一个空的 tab ,而是计算出创建它的大小,如下所示:

public static void main(String[] args){
    int parser;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in) );
    System.out.println("Podaj rozmiar macierzy: ");
        try {
            parser = Integer.parseInt(br.readLine());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    Matrix tab = new Matrix(parser);

    // The rest as before

You never instantiate the Matrix class' matrix variable. When you construct the Matrix on this line:

Matrix tab = new Matrix();

the rozmiar and matrix fields will be initialised to default (empty) values, which is null for the matrix 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:

tab.rozmiar = parser;
tab.matrix = new double[parser][];

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:

for(int ai=1; ai<=tab.rozmiar; ai++){
    tab.matrix[ai] = new double[tab.rozmiar];
    for(int aj=1; aj<=tab.rozmiar; aj++){

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:

class Matrix{
public Matrix(int size) {
    rozmier = size;
    matrix = new double[size][];
    for (int i = 0; i < size; i++)
    {
        matrix[i] = new double[size];
    }
}
final int rozmiar;
final double matrix[][];

}

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:

public static void main(String[] args){
    int parser;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in) );
    System.out.println("Podaj rozmiar macierzy: ");
        try {
            parser = Integer.parseInt(br.readLine());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    Matrix tab = new Matrix(parser);

    // The rest as before
微暖i 2024-10-09 07:37:39

您的数组已声明但从未实例化。添加此行:

tab.matrix = new double[parser][parser];

在此之后:

tab.rozmiar = parser;

Your array is declared but never instantiated. Add this line:

tab.matrix = new double[parser][parser];

after this:

tab.rozmiar = parser;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文