Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.
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 < tempArray.length;i++)
        for(int j = 0;j < tempArray[i].length;j++)
        {
            tempArray[i][j] = Math.random() * (100);            
        } //end for   
    doubMatrix = new double[row][col];
    doubMatrix = tempArray;
}

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;

    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");
}

}

can someone help me to review this code ? There is an error in this code said there is a NullPointerException but the only reason for it o be null is if doubMatrix is null, and I initialize doubMatrix in makedoubMatrix method, also when I use a printf line to find what is the doubMatrix length, it seems to be 1 everywhere in the code.This is my output which is wrong it only read 1 element

First Matrix Object
     69.1
Second Matrix Object
     30.0
Result of Adding Matrix Objects
Exception in thread "main" java.lang.NullPointerException
    at homework3.DoubleMatrix.printMatrix(DoubleMatrix.java:127)
    at homework3.Program3.main(Program3.java:16)
share|improve this question
 
Sorry, this question is off-topic here and will be closed. Please post such questions on Stack Overflow in the future (but debug and do some research first, posting only the relevant code). To determine if your questions are on-topic here, see the faq. –  codesparkle Oct 31 '12 at 10:26
 
The original question: codereview.stackexchange.com/questions/18026/… –  codesparkle Oct 31 '12 at 11:07
add comment

closed as off topic by Glenn Rogers, palacsint, codesparkle, Trevor Pilley, Brian Reichle Oct 31 '12 at 11:17

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

Try this:

 private void makeDoubMatrix(int row, int col)
 {
   doubMatrix  = new double[row][col];
   for (int i = 0;i < row;i++)
   {
       for(int j = 0;j < col;j++)
       {
           doubMatrix[i][j] = Math.random() * (100);            
      }
   }  
}
share|improve this answer
 
oh i fix it right after i ask this question but the problem still persist –  Khoa Vo Oct 31 '12 at 6:27
 
You cannot copy arrays like: doubMatrix = tempArray; They point to the same array. –  Dreamer78692 Oct 31 '12 at 6:47
 
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.