PDA

View Full Version : Any help with this runtime error?


bmw2213
02-17-04, 02:02 PM
Hi, I'm having problems with this code for homework. It compiles fine, but when I run it, I get a NullPointerException. I understand that means I am most likely trying to insert something into a null array in this case, but I dont understand what I am doing wrong. The only code that wasnt given by the teacher is the constructor, copy constructor, swap, transform, and scale. Any help would be much appreciated.

Error Received:
java.lang.NullPointerException
at Matrix.setVal(Matrix.java:48)
at Matrix.<init>(Matrix.java:11)
at Matrix.main(Matrix.java:91)
Exception in thread "main"


import java.text.DecimalFormat;
public class Matrix
{
//constructors
public Matrix(int numRows, int numCols)
{
this.numRows = numRows;
this.numCols = numCols;
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
this.setVal(i,j,(double)0);
}
}

}

public Matrix(Matrix aMatrix) //Copy constructor
{
this(aMatrix.getNumRows(), aMatrix.getNumCols());
}

//accessors
public int getNumRows( )
{ return numRows;
}

public int getNumCols( )
{ return numCols;
}

public double getVal(int row, int col)
{ return item[row][col];
}

public void print( )
{// This method displays the matrix as a rectangular
// array with numRows rows and numCol columns.
DecimalFormat df = new DecimalFormat("##,000.000");
for (int i = 0; i < numRows; i++)
{ for (int j = 0 ; j < numCols; j++)
System.out.print(" " + df.format(item[i][j]));
System.out.println("");
}
}

//mutators
public void setVal(int row, int col, double val)
{ item[row][col] = val;
}

public void swap(int row1, int row2)
{ // Elementary row operation: swap the two rows
// with row indexes row1 and row1.

for (int i = 0; i < numCols; i++)
{ double temp = item[row1][i];
item[row1][i] = item[row2][i];
item[row2][i] = temp;
}
}

public void scale(int resultRow, double factor)
{// Elementary row operation: Multiply each element of the row
// with row index == resultRow by the constant factor.

for (int i = 0; i < numCols; i++)
item[resultRow][i] *= factor;
}

public void transform(int resultRow, double factor, int usedRow)
{// Elementary row operation: Add to each element of the row with
// row index == resultRow a constant multiple (factor) of the
// corresponding element of the row with row index usedRow.

for (int i = 0; i < numCols; i++)
item[resultRow][i] = factor * item[usedRow][i];
}

// data members
private int numRows, numCols;
private double[][] item;


//Testing routine
public static void main(String[] arg) {
EasyIn keyin = new EasyIn( );
System.out.print("Enter the number of rows of the matrix: ");
int rows = keyin.readInt();
System.out.print("Enter the number of columns of the matrix: ");
int cols = keyin.readInt();
Matrix coeff = new Matrix(rows, cols);

// Fill the matrix coeff.
for (int row = 0; row < coeff.getNumRows( ); row++)
for (int col = 0; col < coeff.getNumCols( ); col++)
coeff.setVal(row, col, keyin.readDouble( ));

// Display the matrix coeff.
System.out.println("The original matrix is: ");
coeff.print( );
System.out.println("");

// Copy the matrix.
Matrix other = new Matrix(coeff);
System.out.println("The copy of the first matrix is: ");
other.print( );
System.out.println("");

// Test the "swap rows" routine
coeff.swap(0,2);
System.out.println("After swapping rows 0 and 2: ");
coeff.print( );
System.out.println("");

// Test the "scale" routine
coeff.scale(1, 4.5);
System.out.println("After multiplying row 1 by 4.5: ");
coeff.print( );
System.out.println("");

// Test the "transform" routine
coeff.transform(1, 1.1, 2);
System.out.println("After adding (1.1*row2) to row 1 : ");
coeff.print( );

// Verify that the copy has not been changed.
System.out.println("The copy of the first matrix is unchanged: ");
other.print( );
System.out.println("");

}
}

stdunbar
02-17-04, 08:33 PM
You haven't allocated any memory for the two dimensional array "item". To do this you'd have to do something like:

item = new double[numRows][numCols];

in the constructor.




Error Received:
java.lang.NullPointerException
at Matrix.setVal(Matrix.java:48)
at Matrix.<init>(Matrix.java:11)
at Matrix.main(Matrix.java:91)
Exception in thread "main"

bmw2213
02-17-04, 10:25 PM
thank you very much, that solved all my problems.