2015-04-24 15 views
-2

我一直在setter上得到一個零點錯誤。
我試圖通過將數組的每個點設置爲零來修復它,但它仍然無法工作。數組的setter中的空指針錯誤?

請幫忙。

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 

它切斷了代碼,所以這裏是該類的其餘部分。

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

這裏是主要的方法。

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

回答

0

那麼,首先,這是什麼語言?我假設C#但我只是不知道。我認爲你可能遇到的問題是你正在使用鋸齒狀數組而不是多維數組。

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

錯誤。您應該這樣做:

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

或本:

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

然後初始化每個單獨的子陣列:

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

你可以看到別人給了很好的解釋here

+0

對我來說看起來像Java,但我認爲這對於這個特定的問題沒有根本的區別。 – Marvin