如何在Java中全局声明数组?

时间:2016-10-28 17:15:10

标签: java arrays

我在java中有一个程序,它将两个矩阵相乘。我在全局错误声明中发现了一些错误。 这是我的代码

import java.util.Scanner;

    /**WAP in Java for multiplication of two (m*n) matrices. */
    public class PracProg_7{
        static int m,n,p;
        static int a[][];
        static int b[][];
        static int c[][];
        public static void main(String[] args){
            getMatrix();
            solveMatrix();
            display();

        }
        static void getMatrix(){
            int i,j;
            System.out.println("Enter size of matrix  || Enter m,n,p");
            Scanner sc = new Scanner(System.in);
            m = sc.nextInt();
            n = sc.nextInt();
            p = sc.nextInt();
            int a[][] = new int [m][n];
            int b[][] = new int [n][p];

            System.out.println("Enter matrix A");
            for(i=0;i<m;i++){
                for(j=0;j<n;j++){
                    a[i][j] = sc.nextInt();
                }
            }
            System.out.println("Enter matrix B");
            for(i=0;i<n;i++){
                for(j=0;j<p;j++){
                    b[i][j] = sc.nextInt();
                }
            }

        }
        static void solveMatrix(){
            int i,j,k;
            int c[][] = new int [m][p];
            for(i=0;i<m;i++) {
                for (j = 0; j < p; j++) {
                    c[i][j] = 0;
                    for (k = 0; k < n; k++) {
                        try{
                        c[i][j] += a[i][k] * b[k][j];
                    }
                    catch (ArrayIndexOutOfBoundsException e){System.out.println("Error" + e);}
                    }
                }
            }
        }
        static void display(){
            int i,j;
            for(i=0;i<m;i++){
                for(j=0;j<p;j++){
                    System.out.print(c[i][j] + "\t");
                }
                System.out.println();
            }
        }
    }

当我试图访问全局声明的数组a和b的数组时,我在static void solveMatrix()中收到错误。

我的错误是

Exception in thread "main" java.lang.NullPointerException
    at PracProg_7.solveMatrix(PracProg_7.java:46)
    at PracProg_7.main(PracProg_7.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

1 个答案:

答案 0 :(得分:1)

这是因为在你的getMatrix()方法中,数组对象实例变量a&amp; b没有被使用,而是你正在创建一个&amp; b阵列。

您需要更改getMatrix()中的a和b,如下所示:

a= new int [m][n];
b = new int [n][p];

您可以在下面找到更新的getMatrix()方法:

static void getMatrix(){
        int i,j;
        System.out.println("Enter size of matrix  || Enter m,n,p");
        Scanner sc = new Scanner(System.in);
        m = sc.nextInt();
        n = sc.nextInt();
        p = sc.nextInt();
        a= new int [m][n];
        b = new int [n][p];

        System.out.println("Enter matrix A");
        for(i=0;i<m;i++){
            for(j=0;j<n;j++){
                a[i][j] = sc.nextInt();
            }
        }
        System.out.println("Enter matrix B");
        for(i=0;i<n;i++){
            for(j=0;j<p;j++){
                b[i][j] = sc.nextInt();
            }
        }

    }