-2

I keep getting a null point error on the setter.
I tried to fix it by setting every point of the array to zero, but it still didn't work.

Please help.

public class SudokuArray {

public int[][] sArray;
public int falsecheck = 0; //used as invalid/valid check

public SudokuArray()
{
    int[][] sArray = new int[9][9];
    for (int i=0; i<9; i++)         
        for (int x = 0; x<9; x++) 
            sArray[i][x]=0;
}

public int[][] getArray()
{
    return sArray;
}//end array getter

public void setValue (int value, int column,int row)
{       
    sArray[column][row] = value;
}//ends setter

public void Check()
{
    //row checking
    for (int i = 0; i<9; i++)//decides which row
    {
        for (int x=0; x<9; x++)// decides first point
        {
            for (int y=0; y<9; y++) //decides second point in row
                while (x != y && sArray[i][x] != 0 && sArray[i][y] != 0)// makes sure each point isn't zero (null)
                {                                                       // and making sure its not comparing a point to itself
                    if (sArray[i][x] == sArray[i][y]) 
                        falsecheck = 1;
                }       
        }
    }//ends row check

it cut off the code so here's the rest of that class.

//column checking
    for (int i = 0; i<9; i++)//decides which column
    {
        for (int x=0; x<9; x++)// decides first point
        {
            for (int y=0; y<9; y++) //decides second point in column
                while (x != y && sArray[x][i] != 0 && sArray[y][i] != 0)
                    if (sArray[x][i] == sArray[y][i]) 
                        falsecheck = 1;
        }
    }//ends column check

    //3x3 square check
    for (int a = 1; a<4; a++)// check which column the square is in
    {
        for (int b = 1; b<4; b++) // check which row the square is in
            for (int i = 3*a-3; i< 3*a; i++) //which row 1st point
                for (int x = 3*b-3; x< 3*b; x++) //which column 1st point
                {
                    for (int j = 3*a-3; j< 3*a; j++) //which row  1st point
                        for (int y = 3*b-3; y<3*b; y++)
                            while (sArray[i][x] !=0 && sArray[j][y] !=0)
                                while (j != i || y != x)
                                    if (sArray[i][x] == sArray[j][y])
                                        falsecheck = 1;
                }

        }//ends 3x3 square check
    }//ends check

    public int getCheck()
    {
        return falsecheck;
    }// ends getter
}//ends class

import java.util.Random;

public class SudokuMain {

And Here is the main method.

    public static void main(String[] args) 
    {
        SudokuArray Sudoku;
        Sudoku = new SudokuArray();
        Sudoku.getArray();
        Random rand = new Random();
        int column,row,value;
        int Amount = rand.nextInt(15) + 15;
        for ( int i= 0; i < Amount + 1; i++) // to assign each variable
        {
            column = rand.nextInt(9); // assigns random column
            row = rand.nextInt(9); //assigns random row
            do { 
                value = rand.nextInt(9); //assigns random value
                Sudoku.setValue(value,column,row); //actually sets the value
                Sudoku.Check();
            } while (Sudoku.getCheck() == 1);           
        }    
    }
}
dragosht
  • 3,237
  • 2
  • 23
  • 32

1 Answers1

0

So, firstly, what language is this? I assume C# but I just don't know. I think the problem you are probably encountering is you are using a jagged array instead of a multidimensional array.

int[][] sArray = new int[9][9];

Is incorrect. You should either do this:

int[,] sArray = new int[9,9];

or this:

int[][] sArray = new int[9][];

then initialize each sub array individually:

sArray[0] = new int[9];
sArray[1] = new int[1];
...
sArray[8] = new int[8];

You can see other people give a good explanation here

Community
  • 1
  • 1
Psymunn
  • 376
  • 1
  • 9
  • Looks like Java to me, although I don't think it makes a fundamental difference for that specific problem. – Marvin Apr 27 '15 at 08:11