Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8754993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:45:17+00:00 2026-06-13T13:45:17+00:00

public class DoubleMatrix { private double[][] doubMatrix; public DoubleMatrix(int row, int col) { if(row

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

Hi, I have a NullPointerException error in the last line statement of the makeDoubMatrix method as well the call makedoubMatrix in side if statement of the first constructor.
doubMatrix seems to be null when I already initialize it. How will I be able to fix this problem ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:45:18+00:00Added an answer on June 13, 2026 at 1:45 pm

    You want to initialize the array, i.e.:

    private double[][] doubMatrix = new double[size1][size2];
    

    where size1 and size2 are arbitrary sizes. What you probably want is:

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

    which initializes the array doubMatrix to a size of row*col if both row and col are greather than 0, and to 1*1 otherwise, then calls makeDoubMatrix with its initialized size (you could have this method call after the if-else, using doubMatrix.size and doubMatrix[0].size, but I think it’s more readable now).

    Change the second constructor (which takes a 2D array) using the same reasoning.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class A { private A(int param1, String param2) {} public static A createFromCursor(Cursor
public class Encryption { private static final int[] encrypt = {2, 9, 3, 4,
public class SaveData extends Activity implements OnClickListener { private DataManipulator dh; static final int
public class Encryption { private static final int[] encrypt = {2, 9, 3, 4,
public class Bird { private static int id = 0; private String kind; public
public class TextFileExtractor{ public static String[] fileExtractor(String[] s){ StringBuilder sb=new StringBuilder(); for(int i=0;i<=s.length;i++){ if(s[i].endsWith(.txt)){
public class TextFileExtractor{ public static String[] fileExtractor(String[] s){ StringBuilder sb=new StringBuilder(); for(int i=0;i<=s.length-1;i++){ if(s[i].endsWith(.txt)){
public class MyBindingList : BindingList<int> { public MyBindingList() { } private BindingList<int> temp; public
public class LinkedList { Object contents; LinkedList next = null; public boolean equals(Object item)
public class RefMix { public static void main(String[] args) { Object[] a = {null,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.