Results 1 to 7 of 7
  1. #1
    Pombi is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Question java.lang.NullPointerException

    Hello and thanks for Reading.

    I have 5 classes.
    Matrix is the abstract class.
    IntegerMatrix and RationalMatrix are subclasses of Matrix.
    A class Rational and a class MatrixTester

    Java Code:
    public abstract class Matrix{//This is the Generic Class for both the Integers and the Rational Number for Matrices
    
    		//_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-This part is for the integers_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
    		
    	public static void showMatrices(int [][]m1, int [][]m2){ // Prints the Matrix that is given as a parameter
    		System.out.println("m1:\n\n ");
    		System.out.print(m1[0][0]+"\t"+m1[0][1]+"\n\n"+m1[1][0]+"\t"+m1[1][1]+"\n\n\n" );
    		System.out.println("m2:\n\n ");
    		System.out.print(m2[0][0]+"\t"+m2[0][1]+"\n\n"+m2[1][0]+"\t"+m2[1][1]+"\n\n\n" );
    	}
    	
    	public static void showResult(int [][]m_result, int [][]m1, int [][]m2, char operand ){ //Prints the result of the calculation
    		System.out.println("The result of the m1 "+operand+" m2 is:\n");
    		System.out.print(""+m1[0][0]+"\t"+m1[0][1]+"\t   "+m2[0][0]+"\t   "+m2[0][1]+"\t    "+m_result[0][0]+"\t"+m_result[0][1]+"\n" );
    		System.out.println("\t     "+operand+"                 ==");
    		System.out.print(""+m1[1][0]+"\t"+m1[1][1]+"\t   "+m2[1][0]+"\t   "+m2[1][1]+"\t    "+m_result[1][0]+"\t"+m_result[1][1]+"\n");
    		
    	}
    	
    	public static int[][] integerAddition(int [][]m1, int [][]m2){ //This is addition only for integer numbers
    		int [][]result = new int[2][2];
    		result[0][0] = m1[0][0] + m2[0][0];
    		result[0][1] = m1[0][1] + m2[0][1];
    		result[1][0] = m1[1][0] + m2[1][0];
    		result[1][1] = m1[1][1] + m2[1][1];
    		return result;
    	}
    	public static int[][] integerMultiplication(int [][]m1, int [][]m2){ //This is multiplication only for integer numbers
    		int [][]result = new int[2][2];
    		result[0][0] = (m1[0][0] * m2[0][0]) + (m1[0][1] * m2[1][0]);
    		result[0][1] = (m1[0][0] * m2[0][1]) + (m1[0][1] * m2[1][1]);
    		result[1][0] = (m1[1][0] * m2[0][0]) + (m1[1][1] * m2[1][0]);
    		result[1][1] = (m1[1][0] * m2[0][1]) + (m1[1][1] * m2[1][1]);
    		return result;
    	}
    	
    	//_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-This part is for the Rational-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
    	
    	
    	
    	public Rational[][] rationalAddition(Rational [][]m1, Rational [][]m2){
    		Rational [][]result = new Rational[2][2];
    		for (int i=0; i<2; i++){
    			for(int j=0; j<2; j++){
    					result[i][j].a  = (m1[i][j].a  * m2[i][j].b ) + (m2[i][j].a  * m1[i][j].b );
    					result[i][j].b  = m1[i][j].b  * m2[i][j].b ;	
    			}
    		}
    		return Rational.lowest_term(result);
    	}
    	public static Rational[][] rationalmultiplication(Rational [][]m1, Rational [][]m2){
    		Rational [][]result = new Rational[2][2];
    		for (int i=0; i<2; i++){
    			for(int j=0; j<2; j++){
    					result[0][0].a  = (m1[0][0].a  * m2[0][0].a ) + (m1[0][1].a  * m2[1][0].a );
    					result[0][0].b  = (m1[0][0].b  * m2[0][0].b ) + (m1[0][1].b  * m2[1][0].b );
    					result[0][1].a  = (m1[0][0].a  * m2[0][1].a ) + (m1[0][1].a  * m2[1][1].a );
    					result[0][1].b  = (m1[0][0].b  * m2[0][1].b ) + (m1[0][1].b  * m2[1][1].b );
    					result[1][0].a  = (m1[1][0].a  * m2[0][0].a ) + (m1[1][1].a  * m2[1][0].a );
    					result[1][0].b  = (m1[1][0].b  * m2[0][0].b ) + (m1[1][1].b  * m2[1][0].b );
    					result[1][1].a  = (m1[1][0].a  * m2[0][1].a ) + (m1[1][1].a  * m2[1][1].a );
    					result[1][1].b  = (m1[1][0].b  * m2[0][1].b ) + (m1[1][1].b  * m2[1][1].b );
    			}
    		}
    		return Rational.lowest_term(result);
    	}
    	
    	
    	
    	public static void showMatrices(Rational [][]m1, Rational [][]m2){ // Prints the Matrix that is given as a parameter
    		System.out.println("m1:\n\n ");
    		System.out.print(m1[0][0].a+"/"+m1[0][0].b+"\t"+m1[0][1].a+"/"+m1[0][1].b+"\n\n"+m1[1][0].a+"/"+m1[1][0].b+"\t"+m1[1][1].a+"/"+m1[1][1].b+"\n\n\n");
    		System.out.println("m2:\n\n ");
    		System.out.print(m2[0][0].a+"/"+m2[0][0].b+"\t"+m2[0][1].a+"/"+m2[0][1].b+"\n\n"+m2[1][0].a+"/"+m2[1][0].b+"\t"+m2[1][1].a+"/"+m2[1][1].b+"\n\n\n");
    		
    	}
    }

    Below are the other Classes

    Java Code:
    public class Rational{		
    		public int a; //numerator 
    		public int b; //denominator ===> CAN NOT BE ZERO!
    		public  static int euclid(int a, int b)//algortithm that finds the greatest common divisor (GCD) of two integers a and b
    		{ 
    			if (a < 0) a = a * (-1);
    			if (b < 0) b = b * (-1);
    
    			int r = a  %  b;
    			while (r != 0)
    			{ 	a = b;
    				b = r;
    				r = a  %  b;
    			}
    			//when r is 0, t2 is the GCD between t1 and t2
    			return b;
    		}
    		public  static RationalNum[][] lowest_term(RationalNum [][]simplified)
    		{
    			int GCD ;
    			for (int i=0; i<2; i++){
    				for(int j=0; j<2; j++){
    						GCD = euclid(simplified[i][j].a , simplified[i][j].b );		
    						simplified[i][j].a = simplified[i][j].a  / GCD ; 
    						simplified[i][j].b = simplified[i][j].b  / GCD ;
    				}
    			}
    			return simplified;
    		}
    	}

    Java Code:
    public class RationalMatrix extends Matrix {
    	
    	 Rational [][]matrix1 = new Rational[2][2];
    	 Rational [][]matrix2 = new Rational[2][2];
    	
    	public RationalMatrix(){
    		for (int i=0; i<2; i++){
    			for (int j=0; j<2; j++){
    				matrix1[i][j].a = 0;
    				matrix1[i][j].b = 1;
    				matrix2[i][j].a = 0;
    				matrix2[i][j].b = 1;
    			}
    		}
    	}
    	
    	public void populate_matrices()
    	{
    		System.out.println("Please give the numbers for the first matrix");
    		for (int i=0; i<2; i++){
    			for (int j=0; j<2; j++){
    					System.out.print("Numerator  : "); 
    					matrix1[i][j].a = MyInput.readInt();
    					System.out.print("Denominator: "); 
    					matrix1[i][j].b = MyInput.readInt();
    					while(matrix1[i][j].b == 0)
    					{	
    						System.out.println("Error...... Not allowed to assign ZERO as a denominator. Division with zero is not Defined");
    						System.out.print("Insert number again: ");
    						matrix1[i][j].b = MyInput.readInt();
    					}
    					
    			}
    		}
    		System.out.println("Please give the numbers for the second matrix");
    		for (int i=0; i<2; i++){
    			for (int j=0; j<2; j++){
    					
    					System.out.print("Numerator  : "); 
    					matrix2[i][j].a  = MyInput.readInt();
    					System.out.print("Denominator: "); 
    					matrix2[i][j].b  = MyInput.readInt();
    					while(matrix2[i][j].b  == 0){
    						System.out.println("Error...... Not allowed to assign ZERO as a denominator. Division with zero is not Defined");
    						System.out.print("Insert number again: ");
    						matrix2[i][j].b  = MyInput.readInt();
    					}
    			}
    		}
    	}
    	
    	public void show(){
    		super.showMatrices(matrix1, matrix2);
    	}
    	
    }

    Java Code:
    public class IntegerMatrix extends Matrix{
    	int [][]matrix1 = new int[2][2];
    	int [][]matrix2 = new int[2][2];
    	public IntegerMatrix(){
    		for (int i=0; i<2; i++){
    			for(int j=0; j<2; j++){
    				matrix1[i][j] = 0;
    				matrix2[i][j] = 0;
    			}
    		}
    	}
    	
    	public void populate_matrices(){
    		System.out.println("Please enter the values of the m1 is: ");
    		for (int i=0; i<2; i++){
    			for (int j=0; j<2; j++){
    				matrix1[i][j] = MyInput.readInt();
    			}
    		}
    		System.out.println("Please enter the values of the m2 is: ");
    		for (int i=0; i<2; i++){
    			for (int j=0; j<2; j++){
    				matrix2[i][j] = MyInput.readInt();
    			}
    		}
    	}
    	public void show(){
    		super.showMatrices(matrix1, matrix2);
    	}
    	public int[][] add(){
    		return super.integerAddition(matrix1, matrix2);
    	}
    	public int[][] mul(){
    		return super.integerMultiplication(matrix1, matrix2);
    	}
    	public void showRes(int [][]m_result, int [][]m1, int [][]m2, char operand ){
    		super.showResult(m_result, m1, m2, operand);
    	}
    	public void additionResult(int [][]result_matrix){
    		super.showResult(result_matrix,matrix1, matrix2, '+' );
    	}
    	public void multiplicationResult(int [][]result_matrix){
    		super.showResult(result_matrix,matrix1, matrix2, 'x' );
    	}
    }


    Java Code:
    public class MatrixTester
    {
    	public static void main (String[] args)
    	{
    			int option = 0;
    			System.out.println("Welcome to the 2x2 Matrix calculation program\n");
    			System.out.println("Please choose from the options bellow\n");
    			System.out.println("1.Integers\n2.Rational\n3.Exit");
    			option = MyInput.readInt();
    			
    			switch (option)
    			{
    					case 1 :	{	
    									IntegerMatrix matrices = new IntegerMatrix();
    									matrices.populate_matrices();
    									matrices.show();
    									int [][]addition = new int[2][2];
    									int [][]multiplication = new int[2][2];
    									addition = matrices.add();
    									multiplication = matrices.mul();
    									option = 0;
    									System.out.println("What do you want to do?\n1.Addition\n2.Multiplication");
    									option = MyInput.readInt();
    									if (option != 1 && option != 2) {
    										System.out.println("\n\nError.... Wrong number... Program will terminate...\n\n");
    										break;
    									}
    									if (option == 1)matrices.additionResult(addition);
    									else matrices.multiplicationResult(multiplication);
    								
    									break;
    								}
    								
    					case 2 :	{
    									RationalMatrix matrices = new RationalMatrix();
    									matrices.populate_matrices();
    									matrices.show();
    								
    									break;
    								}
    					case 3 :
    								break;
    								
    					default: 	System.out.println("Error wrong number inserted... Program will be terminated...");
    								break;
    			}
    			
    	}
    }



    All the classes compile just fine.
    But when I run the MatrixTester it runs perfectly except if I select option which is for Rational Numbers... And I get the following error:

    __________________________________________________ ______________
    C:\users\Pombi\Desktop\java MatrixTester

    Please choose from the options bellow
    1.Integers
    2.Rational
    3.Exit
    2
    Exception in thread "main" java.lang.NullPointerException
    at RationalMatrix.<init><RationalMatrix.java:9>
    at MatrixTester.main<MatrixTester.java:35>

    __________________________________________________ _______________

    Ok I usually program in C/C++. Its been a while now that I've been learning Java and I get confused sometimes.:confused:

    As far as I can tell in RationalMatrix class in line 9 it throws an exception because I try to define my variable. But why?

    I declared my matrix1 so why is there a problem?

    What can I do?:(

    Thanks again for reading

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Pombi is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    Well Line 9 there, refers to RationalMatrix Class.
    If you count the lines in the RationalMatrix Class you will see that in line 9 there is :

    matrix1[i][j].a =0;

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Pombi View Post
    Well Line 9 there, refers to RationalMatrix Class.
    If you count the lines in the RationalMatrix Class you will see that in line 9 there is :

    matrix1[i][j].a =0;
    If you define a Rational[][] matrix you define a matrix of references (pointers) to Rationals but they don't point to any Rationals yet, they are null. You have to instantiate each and every Rational object your self and make those references point to it. e.g.

    Java Code:
    for (int i= 0; i < matrix.length; i++)
       for (int j= 0; j < matrix[i].length; j++)
          matrix[i][j]= new Rational( ... ); // make it point to a Rational
    kind regards,

    Jos

  5. #5
    Pombi is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    Thx for guiding me to solve my problem.
    I wrote the following and it worked

    for (int i=0; i<2; i++){
    for (int j=0; j<2; j++){
    matrix1[i][j] = new Rational();
    matrix1[i][j] = new Rational();
    matrix2[i][j] = new Rational();
    matrix2[i][j] = new Rational();
    }
    }
    Thanks again!!

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Pombi View Post
    Thx for guiding me to solve my problem.
    I wrote the following and it worked



    Thanks again!!
    You're welcome but why are you instantiating four objects for each matrix element? That is not how loops work; have a look:

    Java Code:
    for (int i= 0; i < 2; i++)
       for (int j= 0; j < 2; j++)
          matrix[i][j]= new Rational(); // new Rational for all elements i,j
    For each element i,j just one Rational object is allocated, so four elements in total are allocated; your version allocated 16 Rationals; way too many ...

    kind regards,

    Jos

  7. #7
    Pombi is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    Yes. You are right... :P
    I didn't noticed that I was doing that :p

    I think that I pressed the ctrl + v Combination 4 times :p


    Thanks !!!

Similar Threads

  1. java.lang.nullPointerException
    By KSUliz in forum New To Java
    Replies: 10
    Last Post: 04-11-2010, 07:15 PM
  2. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 3
    Last Post: 02-28-2009, 05:41 AM
  3. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 2
    Last Post: 02-27-2009, 10:11 AM
  4. java.lang.NullPointerException
    By stevemcc in forum AWT / Swing
    Replies: 2
    Last Post: 02-08-2008, 09:01 AM
  5. java.lang.NullPointerException
    By Felissa in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 06:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •