I'm new to Java, and have been stuck on this project for days. I've exhausted as much time as I'd like, and am now admitting defeat. Would someone who is vastly superior than I give my code a glance... I'm to create a input file with a matrix of numbers. The program will read the file in, detect errors in the surrounding cells, correct the errors, and then output the matrix as a series of symbols. So far, I've gotten the numbers to read in from the input file, but I'm stuck at the detecting errors. Here's the code....(i've included the errors at the bottom)
import java.io.*;
import java.util.*;
public class MatrixFile
{
//Instance Data
private int[][] matrix = new int[30][30];
private char[][] newMatrix = new char [30][30];
private int i, j, numRows, numColumns;
private Scanner fileScan;
private Scanner fileScan2;
String rowLine;
//method to open the input file (file1.txt)
public MatrixFile()
{
try
{
fileScan = new Scanner(new File("java3.txt"));
}
catch(Exception e)
{
System.out.println("could not find file");
}
try
{
fileScan2 = new Scanner(new File("java3.txt"));
}
catch(Exception e)
{
System.out.println("could not find file");
}
//method to read the file
int numRow = 0, numColumn = 0;
String RowLine;
System.out.println("reading from the file and counting the rows and columns. ");
while(fileScan.hasNext())
{
RowLine = fileScan.nextLine();
numColumn = RowLine.length() / 2 + 1;
numRow++;
}
System.out.println("The number of columns is " + numColumn);
System.out.println("The number of rows is " + numRow);
for(i=0; i<numRow; i++)
{
for(j=0; j<numColumn; j++)
{
matrix[i][j] = fileScan2.nextInt();
System.out.print(matrix[i][j]);
}
System.out.println();
}
fileScan.close();
}
public void detectErrors()
{
for(int i=0; i<numRows; i++) {
for(int j=0; j<numColumns; j++)
{
if ((i == 0) && (j == 0)) // we are looking at the cell in the upper left corner
{upperLeft();}
else if ((i == 0) && (j == numColumns - 1)) // the cell in the upper right corner.
{upperRight();}
else if ((i == numRows - 1) && (j == numColumns - 1)) //lower right corner
{lowerRight();}
else if ((i == numRows - 1) && (j == 0)) // lower left corner
{lowerLeft();}
else if ((i == 0 ) && (j < numColumns - 1)) // upper row
{upperRow();}
else if ((i == numRows - 1) && (j < numColumns - 1))//lower row
{lowerRow();}
else if ((i < numRows - 1) && (j == numColumns - 1))//left columns
{leftRow();}
else if ((i < numRows - 1) && (j == 0))//right columns
{rightRow();}
else // we are in the middle
{middleCell();}
} }
}
public void upperLeft()
{
if((Math.abs(matrix[i][j])-(matrix[i][j+1])>1) || (Math.abs(matrix[i][j])-(matrix[i+1][j+1])>1) || (Math.abs(matrix[i][j])-(matrix[i+1][j])>1))
{matrix[i][j]=((matrix[i][j+1] + matrix[i+1][j+1] + matrix[i+1][j]) / 3);
//correctUpperLeft();
}
}
public void upperRight()
{
if((Math.abs(matrix[i][j])-(matrix[i][j-1])>1) || (Math.abs(matrix[i][j])-(matrix[i+1][j-1])>1) || (Math.abs(matrix[i][j])-(matrix[i+1][j])>1))
{matrix[i][j]=((matrix[i][j-1] + matrix[i+1][j-1] + matrix[i+1][j]) /3);
//correctUpperRight();
}
}
public void lowerRight()
{
if((Math.abs(matrix[i][j])-(matrix[i][j-1])>1) || (Math.abs(matrix[i][j])-(matrix[i-1][j-1])>1) || (Math.abs(matrix[i][j])-(matrix[i-1][j])>1))
{matrix[i][j]=((matrix[i-1][j] + matrix[i-1][j-1] + matrix[i][j-1]) /3);
//correctLowerRight();
}
}
public void lowerLeft()
{
if((Math.abs(matrix[i][j])-(matrix[i][j+1])>1) || (Math.abs(matrix[i][j])-(matrix[i-1][j+1])>1) || (Math.abs(matrix[i][j])-(matrix[i-1][j])>1))
{matrix[i][j]=((matrix[i-1][j] + matrix[i-1][j+1] + matrix[i][j+1]) /3);
//correctLowerLeft();
}
}
public void upperRow()
{
if((Math.abs(matrix[i][j])-(matrix[i][j+1])>1) || (Math.abs(matrix[i][j])-(matrix[i+1][j+1])>1) || (Math.abs(matrix[i][j])-(matrix[i+1][j])>1) ||
(Math.abs(matrix[i][j])-(matrix[i+1][j-1])>1) || (Math.abs(matrix[i][j])-(matrix[i][j-1])>1))
{matrix[i][j]=((matrix[i][j+1] + matrix[i+1][j+1] + matrix[i+1][j] + matrix[i][j-1] + matrix[i+1][j-1]) /5);
//correctUpperRow();
}
}
public void lowerRow()
{
if((Math.abs(matrix[i][j])-(matrix[i][j-1])>1) || (Math.abs(matrix[i][j])-(matrix[i-1][j-1])>1) || (Math.abs(matrix[i][j])-(matrix[i-1][j])>1) ||
(Math.abs(matrix[i][j])-(matrix[i-1][j+1])>1) || (Math.abs(matrix[i][j])-(matrix[i][j+1])>1))
{matrix[i][j]=((matrix[i][j-1] + matrix[i][j+1] + matrix[i-1][j-1] + matrix[i-1][j+1] + matrix[i-1][j]) /5);
//correctLowerRow();
}
}
public void rightRow()
{
if((Math.abs(matrix[i][j])-(matrix[i-1][j])>1) || (Math.abs(matrix[i][j])-(matrix[i-1][j+1])>1) || (Math.abs(matrix[i][j])-(matrix[i][j+1])>1) ||
(Math.abs(matrix[i][j])-(matrix[i+1][j+1])>1) || (Math.abs(matrix[i][j])-(matrix[i+1][j])>1))
{matrix[i][j]=((matrix[i-1][j] + matrix[i-1][j-1] + matrix[i][j-1] + matrix[i+1][j-1] + matrix[i+1][j]) /5);
//correctRightRow();
}
}
public void leftRow()
{
if((Math.abs(matrix[i][j])-(matrix[i+1][j])>1) || (Math.abs(matrix[i][j])-(matrix[i+1][j-1])>1) || (Math.abs(matrix[i][j])-(matrix[i][j-1])>1) ||
(Math.abs(matrix[i][j])-(matrix[i-1][j-1])>1) || (Math.abs(matrix[i][j])-(matrix[i-1][j])>1))
{matrix[i][j]=((matrix[i-1][j+1] + matrix[i-1][j] + matrix[i][j+1] + matrix[i+1][j+1] + matrix[i+1][j]) /5);
//correctLeftRow();
}
}
public void middleCell(){
if((Math.abs(matrix[i][j])-(matrix[i][j+1])>1) || (Math.abs(matrix[i][j])-(matrix[i+1][j+1])>1) || (Math.abs(matrix[i][j])-(matrix[i+1][j])>1) ||
(Math.abs(matrix[i][j])-(matrix[i+1][j-1])>1) || (Math.abs(matrix[i][j])-(matrix[i][j-1])>1))
{matrix[i][j]=((matrix[i-1][j-1] + matrix[i-1][j] + matrix[i-1][j+1] + matrix[i][j-1] + matrix[i][j+1] + matrix[i+1][j-1] + matrix[i+1][j] + matrix[i+1][j+1]) /8);
//correctMiddleCell();
}
}
/*public void correctUpperLeft()
{
matrix[i][j]=((matrix[i][j+1] + matrix[i+1][j+1] + matrix[i+1][j]) / 3);
}
public void correctUpperRight()
{
matrix[i][j]=((matrix[i][j-1] + matrix[i+1][j-1] + matrix[i+1][j]) /3);
}
public void correctLowerLeft()
{
matrix[i][j]=((matrix[i-1][j] + matrix[i-1][j+1] + matrix[i][j+1]) /3);
}
public void correctLowerRight()
{
matrix[i][j]=((matrix[i-1][j] + matrix[i-1][j-1] + matrix[i][j-1]) /3);
}
public void correctUpperRow()
{
matrix[i][j]=((matrix[i][j+1] + matrix[i+1][j+1] + matrix[i+1][j] + matrix[i][j-1] + matrix[i+1][j-1]) /5);
}
public void correctLowerRow()
{
matrix[i][j]=((matrix[i][j-1] + matrix[i][j+1] + matrix[i-1][j-1] + matrix[i-1][j+1] + matrix[i-1][j]) /5);
}
public void correctMiddleCell()
{
matrix[i][j]=((matrix[i-1][j-1] + matrix[i-1][j] + matrix[i-1][j+1] + matrix[i][j-1] + matrix[i][j+1] + matrix[i+1][j-1] + matrix[i+1][j] + matrix[i+1][j+1]) /8);
}
public void correctLeftRow()
{
matrix[i][j]=((matrix[i-1][j+1] + matrix[i-1][j] + matrix[i][j+1] + matrix[i+1][j+1] + matrix[i+1][j]) /5);
}
public void correctRightRow()
{
matrix[i][j]=((matrix[i-1][j] + matrix[i-1][j-1] + matrix[i][j-1] + matrix[i+1][j-1] + matrix[i+1][j]) /5);
}
*/
public void createImage()
{
for(int i=0; i<numRows; i++) {
for(int j=0; j<numColumns; j++)
{
if((matrix[i][j]>= 0) && (matrix[i][j] < 3)) {
newMatrix[i][j] = ' ';
}
else if ((matrix[i][j]>= 3) && (matrix[i][j] < 6)) {
newMatrix[i][j] = '.';
}
else if ((matrix[i][j]>= 6) && (matrix[i][j] < 8)) {
newMatrix[i][j] = '+';
}
else {
newMatrix[i][j] = '#';
}
System.out.print(newMatrix[i][j]);
} }
System.out.println();
}
@Override
public String toString()
{
String image = null;
for(int i=0; i<numRows; i++)
{
for(int j=0; j<numColumns; j++)
{
image += newMatrix[i][j] + ' ';
image += "\n";
}//end for
}
return image;
}// toString
}
public class MatrixTester{
public static void main(String[]args)
{
MatrixFile myMatrix = new MatrixFile();
myMatrix.detectErrors();
myMatrix.createImage();
System.out.print(myMatrix);
}}
errors I'm getting:
Exception in thread "main" java.lang.NullPointerException
at java.io.Writer.write(Writer.java:157)
at java.io.PrintStream.write(PrintStream.java:525)
at java.io.PrintStream.write(PrintStream.java:683)