Here is the code for the programm. My question is why this statement in SudokuValidator doesn't work?
for (Cell[] myPuzzle : puzzle) {
it gives me errors:
Exception in thread "main" java.lang.NullPointerException
at SudokuValidator.isSolution(SudokuValidator.java:54)
at SudokuValidator.main(SudokuValidator.java:27)
import java.util.*;
public class Cell {
int value;
public Cell() {}
// Constructor works like a setter
public Cell(int value) {
this.value=value; //return references to Sudoku object
}
//returns value from a file
public int getValue() {
return this.value;
}
//checks if the valuse is unique
public boolean equals(Cell that){
return (this == that)?true:false;
}
public String toString() {
return ((this.value>9)?" ":" ") + String.valueOf(this.value);
}
}
import java.util.*;
public class Sudoku implements Iterable<Cell []>
{
private Cell [] [] puzzle;
/**
* Default constructor should not be called. Make it private.
*/
private Sudoku() {}
/**
* Puzzle constructor that uses a Scanner object to read a file.
* File contains 81 numbers that are the values of the 81 cells.
* @param file a Scanner object on a File object
*/
//initiates N^2 Cell object
public Sudoku(Scanner file)
{
int size = file.nextInt();
System.out.println("Size: " + size);
puzzle = new Cell[size][size];
for (int i = 0; i < size; i++) //two dimensional array of values in the file
for (int j = 0; j < size; j++)
puzzle[i][j] = new Cell(file.nextInt());
}
/**
* Generates and returns a String representation of the puzzle cells
* @return A String representing the contents of the puzzle array
*/
public String toString()
{
// display the puzzle
String value = "Puzzle is:\n";
for (int i = 0; i < puzzle.length; i++) {
for (int j = 0; j < puzzle[i].length; j++)
value += puzzle[i][j].toString();
value += "\n";
}
return value;
}
public Iterator<Cell[]> iterator() {
new SudokuIterator(puzzle);
return null;
}
/**
* Instantiates and returns a new SudokuIterator object
* @return A SudokuIterator object on the puzzle array
*/
// write your code for the iterator method here
} /* 201220 */
import java.util.*;
public class SudokuIterator implements Iterator<Cell[]> {
private Cell[][] puzzle;
private int cursor;
private SudokuIterator() {} //default constructor
public SudokuIterator(Cell[][] puzzle) {
this.puzzle=puzzle;
cursor = 0;
}
public boolean hasNext() {
return cursor<puzzle.length;
}
public Cell[] next() {
Cell[] value = puzzle[cursor];
cursor++;
if(cursor==puzzle.length)
throw new NoSuchElementException(); //throw an exception if puzzle have no more elements
else
return value;
}
public void remove() {
throw new UnsupportedOperationException(); // remove operation is not supported
}
}
import java.util.*;
import java.io.*;
/**
* This class instantiates a Sudoku object passing a Scanner on a
* file to the Sudoku constructor. It prints the puzzle using the
* Sudoku toString method. It determines if the digit matrix is a
* valid solution for a Sudoku puzzle or not and prints the result.
*
*
*/
public class SudokuValidator
{
private static Sudoku puzzle;
/**
* @param args - not used
*/
public static void main( String [] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter name of file containing puzzle to verify");
SudokuValidator myValidator = new SudokuValidator(scan.nextLine()); //initiates
System.out.println(myValidator.isSolution() ?
"It is a valid solution" : "It is not a valid solution");
}
public SudokuValidator(String fileName)
{
Scanner file = null;
try
{
file = new Scanner(new File(fileName));
}
catch (Exception e)
{
System.out.println("Bad file name");
System.exit(0);
}
puzzle = new Sudoku(file);
System.out.println(puzzle);
}
//isSolution method that iterates over the puzzle and
// determines if each row, column, and N1/2xN1/2 box is valid
public boolean isSolution() {
int count=0;
boolean solution = false;
for (Cell[] myPuzzle : puzzle) {
for (int i=0; i<myPuzzle.length; i++) {
for (int j=i+1; j<myPuzzle.length; j++) {
if (myPuzzle[i].getValue() == myPuzzle[j].getValue())
count++;
}
}
}
if (count > 0)
solution= false;
else
solution = true;
return solution;
}
} /* 201220 */