Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 2D数组空指针异常错误 公共类双矩阵 { 专用双[][]双矩阵; 公共双矩阵(整数行,整数列) { 如果(行>0和列>0) { makeDoubMatrix(行、列); } 其他的 { 行=1; col=1; } } 公共双矩阵(双[][]临时数组) { if(tempArray!=null) { for(int i=0;i_Java_Arrays_Object_2d - Fatal编程技术网

Java 2D数组空指针异常错误 公共类双矩阵 { 专用双[][]双矩阵; 公共双矩阵(整数行,整数列) { 如果(行>0和列>0) { makeDoubMatrix(行、列); } 其他的 { 行=1; col=1; } } 公共双矩阵(双[][]临时数组) { if(tempArray!=null) { for(int i=0;i

Java 2D数组空指针异常错误 公共类双矩阵 { 专用双[][]双矩阵; 公共双矩阵(整数行,整数列) { 如果(行>0和列>0) { makeDoubMatrix(行、列); } 其他的 { 行=1; col=1; } } 公共双矩阵(双[][]临时数组) { if(tempArray!=null) { for(int i=0;i,java,arrays,object,2d,Java,Arrays,Object,2d,您没有初始化doubMatrix。唯一为doubMatrix赋值的行是注释掉的一行: public class DoubleMatrix { private double[][] doubMatrix; public DoubleMatrix(int row, int col) { if(row > 0 && col > 0) {

您没有初始化
doubMatrix
。唯一为
doubMatrix
赋值的行是注释掉的一行:

public class DoubleMatrix
{
    private double[][] doubMatrix;

    public DoubleMatrix(int row, int col)
    {              
            if(row > 0 && col > 0)
            {
                    makeDoubMatrix(row,col);
            }
            else
            {
                    row = 1;
                    col = 1;
            }
    }

    public DoubleMatrix(double[][] tempArray)
    {
            if(tempArray != null)
            {
                    for(int i = 0; i < tempArray.length-1;i++)
                    {
                            if(tempArray[i].length == tempArray[i+1].length)
                            {
                                    tempArray = doubMatrix;
                            }
                    }
            }
            else
            {
                    makeDoubMatrix(1,1);
            }
    }

    public int getDim1()
    {
            return doubMatrix.length;
    }

    public int getDim2()
    {
            return doubMatrix[0].length;
    }

    private void makeDoubMatrix(int row, int col)
    {
      double[][] tempArray  = new double[row][col];
      for (int i = 0;i < row;i++)
      {
          for(int j = 0;j < col;j++)
          {
              tempArray[i][j] = Math.random() * (100);            
              doubMatrix[i][j] = tempArray[i][j];
         }
      }  
   }
    public DoubleMatrix addMatrix(DoubleMatrix secondMatrix)
    {      
            //this. doubMatrix = doubMatrix;
            double[][] tempArray;
            if(secondMatrix.doubMatrix.length == doubMatrix.length)
                    if(secondMatrix.doubMatrix[0].length == doubMatrix[0].length)
                    {
                            tempArray = new double[doubMatrix.length][doubMatrix[0].length];
                            for(int i = 0; i< secondMatrix.doubMatrix.length;i++)
                                  for(int j = 0; j< secondMatrix.doubMatrix[i].length;j++ )
                                  {
                                      tempArray[i][j] = secondMatrix.doubMatrix[i][j] + doubMatrix[i][j];// add two matrices
                                  }//end for    
                            return new DoubleMatrix (tempArray);
                    }


                            return  new DoubleMatrix(1,1);                                                                 
    }


    public DoubleMatrix getTransposedMatrix()
    {
            double[][] tempArray = new double[doubMatrix.length][doubMatrix[0].length];
            for(int i = 0;i < doubMatrix.length;i++)
            for(int j = 0;j < doubMatrix[i].length;j++)
            {
                tempArray[j][i] = doubMatrix[i][j];// transposed    matrix2          
            }//end for         
            return new DoubleMatrix(tempArray);
    }

    public DoubleMatrix multiplyingMatrix(DoubleMatrix secondMatrix)
    {
            double[][] tempArray = new double[secondMatrix.doubMatrix.length][doubMatrix[0].length];
            //check if dimension of matrix1 equal to dimension of matrix2  
            if(secondMatrix.doubMatrix[0].length == doubMatrix.length)
                    if(doubMatrix.length == secondMatrix.doubMatrix[0].length)
            {

                             for (int i = 0; i <secondMatrix.doubMatrix.length; i++)
                   {
                       for(int j = 0; j < doubMatrix[0].length; j++)
                        {

                           for (int k = 0; k < doubMatrix.length; k++)
                           {
                               tempArray[i][j] = tempArray[i][j] + secondMatrix.doubMatrix[i][k]*doubMatrix[k][j]; // multiply 2 matrices

                           }
                        }
                   }//end for  
            }// end if



                            return new DoubleMatrix(1,1);                                  
    }

    public void printMatrix(String text)
    {
            System.out.println(text);// output string
            for(int i = 0; i< doubMatrix.length;i++)
            {
                  for(int j = 0; j< doubMatrix[i].length;j++ )      {                                
                      System.out.printf("%9.1f", doubMatrix[i][j]);// out put value for matrices                   
                  }  
                  System.out.println();
            }
    }
}

public class Program3
{
public static void main(String[] args)
{
        int num1 = (int) (Math.random()*(10-3+1)+3);
        int num2 = (int) (Math.random()*(10-3+1)+3);
        DoubleMatrix doubMatObj1 = new DoubleMatrix(num1,num2);
        DoubleMatrix doubMatObj2 = new DoubleMatrix(doubMatObj1.getDim1(),doubMatObj1.getDim2());
        DoubleMatrix doubMatObj3;


        doubMatObj2.getDim1();

        doubMatObj3 = doubMatObj1.addMatrix(doubMatObj2);
        doubMatObj1.printMatrix("First Matrix Object");
        doubMatObj2.printMatrix("Second Matrix Object");
        doubMatObj3.printMatrix("Result of Adding Matrix Objects");
        doubMatObj2 = doubMatObj2.getTransposedMatrix();
        doubMatObj2.printMatrix("Result of inverting Matrix Object");
        doubMatObj3 = doubMatObj1.multiplyingMatrix(doubMatObj2);
        doubMatObj3.printMatrix("Result of Multiplying Matrix Objects");
}
}
(这也无济于事。)

问问自己,你认为你在哪里初始化它-你认为你在哪里得到了这样的东西:

//this. doubMatrix = doubMatrix;
…或从另一个数组复制值的赋值:

doubMatrix = new double[1][2];

如果没有任何语句为其赋值,则不会对其进行初始化,因此它的默认值始终为
null

您要初始化数组,即:

doubMatrix = someOtherVariable;
其中size1和size2是任意大小。您可能需要的是:

private double[][] doubMatrix = new double[size1][size2];
它将数组doubMatrix的大小初始化为row*col(如果row和col都大于0),否则初始化为1*1,然后使用其初始化的大小调用makeDoubMatrix(您可以在if-else之后使用
doubMatrix.size
doubMatrix[0].size
,但我认为它现在更可读)

使用相同的推理更改第二个构造函数(采用2D数组)

if(row > 0 && col > 0)
{
    doubMatrix = new double[row][col];
    makeDoubMatrix(row,col);
}
else
{
    doubMatrix = new double[1][1];
    makeDoubMatrix(1,1);
}