Current location: Hot Scripts Forums » Programming Languages » Everything Java » Any help with this runtime error?


Any help with this runtime error?

Reply
  #1 (permalink)  
Old 02-17-04, 01:02 PM
bmw2213 bmw2213 is offline
Newbie Coder
 
Join Date: Jan 2004
Location: Kirksville, MO
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Any help with this runtime error?

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

}
}
Reply With Quote
  #2 (permalink)  
Old 02-17-04, 07:33 PM
stdunbar stdunbar is offline
Newbie Coder
 
Join Date: Jan 2004
Location: Superior, CO, USA
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
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.



Quote:
Originally Posted by bmw2213
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"
Reply With Quote
  #3 (permalink)  
Old 02-17-04, 09:25 PM
bmw2213 bmw2213 is offline
Newbie Coder
 
Join Date: Jan 2004
Location: Kirksville, MO
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
thank you very much, that solved all my problems.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
New to Java: weird main error msg hecresper Everything Java 2 02-10-04 03:03 PM
asp-iis-Server error nsuresh_rasr ASP.NET 3 02-08-04 12:47 AM
parse error... help? kappler0 PHP 2 01-21-04 03:57 AM
[php error] parse error | fatal error xeoHosting PHP 1 01-03-04 08:12 PM
php error plz help darkcarnival PHP 2 11-19-03 08:14 PM


All times are GMT -5. The time now is 05:59 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.