Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Why does my two-dimensional array not accepting any values?

  1. #1
    Junior Member
    Join Date
    Sep 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why does my two-dimensional array not accepting any values?

    I'm creating a 2D array program that accepts values from users and to perform two operations, such as Adding and Multiplying the array, then print the results. However, the program is not accepting any values, it prints the following error:

    Exception in thread "main" java.lang.NullPointerException
    at twodimensionalarray.Matrix.getMatrix(Matrix.java:2 7)
    at twodimensionalarray.TwoDimensionalArray.main(TwoDi mensionalArray.java:18)

    package twodimensionalarray;
     
    import java.util.Scanner;
     
    public class Matrix {
        double [][]Array;
        int rows;
        int cols;
     
        Matrix(){
            rows = 0;
            cols = 0;
            int [][] Array = new int[rows][cols];
        }
     
        Matrix(int row1, int col1)
    	{
    		rows = row1;
    		cols = col1;
    	}
     
        public void getMatrix(){
            Scanner entry = new Scanner(System.in);
            System.out.println("Enter elements for array: ");
            for(int i = 0; i < rows; i++){
                for(int j = 0; j < cols; j++){
                    Array[i][j] = entry.nextDouble();
                }
            }
        }
     
        public void displayMatrix(){
            for(int i = 0; i < rows; i++){
                for(int j = 0; j < cols; j++){
                    System.out.printf("%2f  " + Array[i][j]);
                }
                System.out.println();
            }   
        }
     
        public Matrix addMartix(Matrix first){
            Matrix results = new Matrix();
     
            if (rows != first.rows || cols != first.cols){
                System.out.println("Unable to add matrix!");
            }else{
                for(int i = 0; i < rows; i++){
                    for(int j = 0; j < cols; j++){
                        results.Array[i][j] = (int) (Array[i][j] + Array[i][j]);
                    }
                }
            }
            return results;
        }
     
        public Matrix multiplyMartix(Matrix first){
            Matrix results = new Matrix();
     
            if (rows != first.rows || cols != first.cols){
                System.out.println("Unable to multiply matrix!");
            }else{
                for(int i = 0; i < rows; i++){
                    for(int j = 0; j < cols; j++){
                        results.Array[i][j] = (int) (Array[i][j] * Array[i][j]);
                    }
                }
            }
            return results;
        }
    }
     
     
    package twodimensionalarray;
     
    import java.util.Scanner;
     
    public class TwoDimensionalArray {
        public static Matrix results;
        public static void main(String[] args) {
            int rows = 0, cols = 0;
     
            Scanner entry = new Scanner(System.in);
     
            System.out.println("Enter number of rows: ");
            rows = entry.nextInt();
            System.out.println("Enter number of columns: ");
            cols = entry.nextInt();
     
            Matrix a = new Matrix(rows, cols);
            a.getMatrix();
     
     
            results = a.addMartix(a);
            results.displayMatrix();
     
            results = a.multiplyMartix(a);
            results.displayMatrix();
        }
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why does my two-dimensional array not accepting any values?

    Exception in thread "main" java.lang.NullPointerException
    at twodimensionalarray.Matrix.getMatrix(Matrix.java:2 7)
    What variable has the null value when line 27 is executed?

    Note: Array is a poor name for a variable. Variable names should begin with a lowercase letter.
    Also Array is the name of a Java SE class.

    These statements create a local array with 0 rows and 0 columns:
       Matrix(){
            rows = 0;
            cols = 0;
            int [][] Array = new int[rows][cols];  // Array is local to the constructor.
        }

    The variable named Array declared at the class level is never given a value so its value would be null.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why does my two-dimensional array not accepting any values?

    Also posted here: https://coderanch.com/t/734404/java/...values#3415346

    Please read: http://www.javaprogrammingforums.com...s-posting.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: February 22nd, 2019, 03:10 PM
  2. Trying to sort the values of a two dimensional array in ascending order.
    By SirPaco in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 8th, 2018, 07:27 PM
  3. Trouble inputting values into a 2-dimensional array
    By thegorila78 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: November 18th, 2013, 05:37 AM
  4. Create two dimensional array from a one dimensional array
    By Kristenw17 in forum Collections and Generics
    Replies: 1
    Last Post: April 9th, 2013, 07:51 PM
  5. Read A File and Store Values into a 2-Dimensional Integer Array?
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 9th, 2011, 09:13 PM

Tags for this Thread