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 */

Edited 4 Years Ago by shean1488: n/a

havent checked your code but i dont think you want this:

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++;
	         }
	       }

should it not be:

for (Cell myPuzzle : puzzle) {

Edited 4 Years Ago by DavidKroukamp: n/a

havent checked your code but i dont think you wnat this:

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++;
	         }
	       }

you should do:

for (Cell myPuzzle : puzzle) {

and change the code where necessrarry... maybe now that inner for loops is not needed you could do myPuzzle.getValue()

Well exactly this statement gives me an error =))
I wish I would do it. but it just doesn't work ))

Well exactly this statement gives me an error =))
I wish I would do it. but it just doesn't work ))

You wish you would do what?
What was the error given when you change the statement?

[edit] wait maybe i typed too fast before i thought... my code would give errors as this is not a normal array you are iterating through... the problem is still here though 'Cell[] myPuzzle:puzzle' you declared it only within the for loop... so what variables besides null do you expect it to be assign?

Edited 4 Years Ago by DavidKroukamp: n/a

You wish you would do what?
What was the error given when you change the statement?

it gives me something like this

Exception in thread "main" java.lang.NullPointerException
	at SudokuValidator.isSolution(SudokuValidator.java:73)
	at SudokuValidator.main(SudokuValidator.java:27)

+
at the end I'm thinking to get the (int) value from puzzle

You wish you would do what?
What was the error given when you change the statement?

[edit] wait maybe i typed too fast before i thought... my code would give errors as this is not a normal array you are iterating through... the problem is still here though 'Cell[] myPuzzle:puzzle' you declared it only within the for loop... so what variables besides null do you expect it to be assign?

I'm trying to get the variables from puzzle.

My output looks like this :

Please enter name of file containing puzzle to verify
bad-16.txt
Size: 16
Puzzle is:
   1   5   9  13   2   6  10  14   3   7  11  15   4   8  12  16
   2   6  10  14   3   7  11  15   4   8  12  16   5   9  13   1
   3   7  11  15   4   8  12  16   5   9  13   1   6  10  14   2
   4   8  12  16   5   9  13   1   6  10  14   2   7  11  15   3
   5   9  13   1   6  10  14   2   7  11  15   3   8  12  16   4
   6  10  14   2   7  11  15   3   8  12  16   4   9  13   1   5
   7  11  15   3   8  12  16   4   9  13   1   5  10  14   2   6
   8  12  16   4   9  13   1   5  10  14   2   6  11  15   3   7
   9  13   1   5  10  14   2   6  11  15   3   7  12  16   4   8
  10  14   2   6  11  15   3   7  12  16   4   8  13   1   5   9
  11  15   3   7  12  16   4   8  13   1   5   9  14   2   6  10
  12  16   4   8  13   1   5   9  14   2   6  10  15   3   7  11
  13   1   5   9  14   2   6  10  15   3   7  11  16   4   8  12
  14   2   6  10  15   3   7  11  16   4   8  12   1   5   9  13
  15   3   7  11  16   4   8  12   1   5   9  13   2   6  10  14
  16   4   8  12   1   5   9  13   2   6  10  14   3   7  11  14

Exception in thread "main" java.lang.NullPointerException
	at SudokuValidator.isSolution(SudokuValidator.java:73)
	at SudokuValidator.main(SudokuValidator.java:27)

So I just want to store all those numbers in some array, so I can later work with them

Edited 4 Years Ago by shean1488: n/a

I'm trying to get the variables from puzzle.

My output looks like this :

Please enter name of file containing puzzle to verify
bad-16.txt
Size: 16
Puzzle is:
   1   5   9  13   2   6  10  14   3   7  11  15   4   8  12  16
   2   6  10  14   3   7  11  15   4   8  12  16   5   9  13   1
   3   7  11  15   4   8  12  16   5   9  13   1   6  10  14   2
   4   8  12  16   5   9  13   1   6  10  14   2   7  11  15   3
   5   9  13   1   6  10  14   2   7  11  15   3   8  12  16   4
   6  10  14   2   7  11  15   3   8  12  16   4   9  13   1   5
   7  11  15   3   8  12  16   4   9  13   1   5  10  14   2   6
   8  12  16   4   9  13   1   5  10  14   2   6  11  15   3   7
   9  13   1   5  10  14   2   6  11  15   3   7  12  16   4   8
  10  14   2   6  11  15   3   7  12  16   4   8  13   1   5   9
  11  15   3   7  12  16   4   8  13   1   5   9  14   2   6  10
  12  16   4   8  13   1   5   9  14   2   6  10  15   3   7  11
  13   1   5   9  14   2   6  10  15   3   7  11  16   4   8  12
  14   2   6  10  15   3   7  11  16   4   8  12   1   5   9  13
  15   3   7  11  16   4   8  12   1   5   9  13   2   6  10  14
  16   4   8  12   1   5   9  13   2   6  10  14   3   7  11  14

Exception in thread "main" java.lang.NullPointerException
	at SudokuValidator.isSolution(SudokuValidator.java:73)
	at SudokuValidator.main(SudokuValidator.java:27)

So I just want to store all those numbers in some array, so I can later work with them

well then i guess this would actually have belonged in your original question here:http://www.daniweb.com/software-development/java/threads/413196/1763027 which i have tried to answer already...Please try not to start new Topics on threads that are ultimately the same questions just helps to keep it organized and others can see solutions you might have already tried etc and dont end up wasting time on a potentially solved problem

Edited 4 Years Ago by DavidKroukamp: n/a

still have an error is a pretty vague description.
and yes, off course you're getting an error there.
your puzzle variable isn't a two dimensional array, it contains one.

add a getPuzzle() getter to your Sudoku class, and change:

for ( Cell [] myPuzzle : puzzle )

to

for ( Cell [] myPuzzle : puzzle.getPuzzle() )

Edited 4 Years Ago by stultuske: n/a

This article has been dead for over six months. Start a new discussion instead.