Results 1 to 5 of 5
  1. #1
    hongi is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default Error from sudoku class

    This is my code about SUDOKU
    I got this error and don't know what it is.

    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at Sudoku.ForcedSet(test.java:265)
    	at test.main(test.java:66)

    Java Code:
      public class test
    {
       public static void main(String[] args)
       {
          SudCell sud1, sud2, sud3;
          
          sud1 = new SudCell();
          sud2 = new SudCell();
          sud3 = new SudCell();
    
          if ( !sud1.ForcedSet(10) )
             System.out.print( "Bad 10\n" );
    
          if ( !sud2.ForcedSet(-1) )
             System.out.print( "Bad -1\n" );
          if ( !sud3.RuleOut(50) )
             System.out.print( "Bad 50\n" );
    
          System.out.print( "A: " + sud1.ToString() + "\n" );
          System.out.print( "B: " + sud2.ToString() + "\n" );
          System.out.print( "C: " + sud3.ToString() + "\n" );
          System.out.print( "\n" );
    
          System.out.print( "D: " + sud1.ToStringReveal() + "\n" );
          System.out.print( "E: " + sud2.ToStringReveal() + "\n" );
          System.out.print( "F: " + sud3.ToStringReveal() + "\n" );
          System.out.print( "\n" );
    
          sud1.ForcedSet(3);
          sud2.ForcedSet(9);
          sud3.RuleOut(0);
          sud3.RuleOut(2);
          sud3.RuleOut(4);
          sud3.RuleOut(6);
          sud3.RuleOut(8);
          
          System.out.print( sud1.ToString() + "\n" );
          System.out.print( sud2.ToString() + "\n" );
          System.out.print( sud3.ToString() + "\n" );
          System.out.print( "\n" );
    
          System.out.print( sud1.ToStringReveal() + "\n" );
          System.out.print( sud2.ToStringReveal() + "\n" );
          System.out.print( sud3.ToStringReveal() + "\n" );
          System.out.print( "\n" );
    
          sud1.Complete();
          sud2.Complete();
          sud3.Complete();
    
          System.out.print( sud1.IsCompleted() + " " + sud2.IsCompleted() + " "
             + sud3.IsCompleted() + "\n" );
          System.out.print( sud1.ValuePresent(1) + " " 
                + Boolean.toString(sud1.ValuePresent(2))
                + " " + Boolean.toString(sud1.ValuePresent(3)) + "\n" );
          System.out.print( Boolean.toString(sud3.ValuePresent(1)) 
                + " " + Boolean.toString(sud3.ValuePresent(2))
                + " " + Boolean.toString(sud3.ValuePresent(3)) + "\n" );
          
          
          Sudoku puzzle;
    
          puzzle = new Sudoku();
          
          // set some values in the matrix, attempting illegal
          puzzle.ForcedSet(3, 4, 5);
          puzzle.ForcedSet(7, 1, 3);
          puzzle.ForcedSet(9, 2, 6);
          for (int k = -1; k < 12; k++)
             if ( !puzzle.ForcedSet(k, k, k) )
                System.out.println( "Could not set " + k);
    
          System.out.print( "\n ------------- first basic ------------- \n");
          puzzle.Display(false);
    
          System.out.print( "\n ------------- then revealed ------------- \n");
          puzzle.Display(true);
          System.out.print( "\n" );;
    
          for (int row = Sudoku.MIN_VAL; row <= Sudoku.MAX_VAL ; row++)
             for (int col = Sudoku.MIN_VAL; col <= Sudoku.MAX_VAL ; col++)
             {
                // rule out 2 through 8 in all places except middle row and col
                if ( row == 5 || col == 5 )
                   continue;
                 for (int k = 2; k <= 8; k++)
                   puzzle.RuleOut(row, col, k);
             }
    
          System.out.print( "\n ---------- after massive rule-outs ---------- \n");
          puzzle.Display(true);
       }
    }
    
    class Sudoku
    {
       static final int MIN_VAL = SudCell.MIN_VAL;
       static final int MAX_VAL = 9;
       static final int NUM_VALS = 9;
       static final int SQRT_NUM_VALS = 3;
       static final int NOT_FINAL_YET = -1;
       
       SudCell suds[][] = new SudCell[NUM_VALS][NUM_VALS];
       
       public Sudoku()
       {
          
       }
       void Display(boolean reveal)
       {
          String output;
          int levlinecount = 0, sepcount = 0, divider = 0, divideend = 0;
          if (reveal == false)
          {
             for (int x=0;x<MAX_VAL;x++)
             {
                for (int y=0;y<MAX_VAL;y++)
                {
                   output = suds[x][y].ToString();
                   System.out.print(output);
                   sepcount++;
                   if (sepcount > 2)
                   {
                      sepcount = 0;
                      if (levlinecount < 2)
                      {
                         System.out.print ("|");
                         levlinecount++;
                      }
                      else
                      {
                         levlinecount = 0;
                         if (divider < 2)
                         {
                            System.out.print( "\n" );
                            divider++;
                         }
                         else
                         {
                            if (divideend < 2)
                            {
                               System.out.print( "\n" + "---------------" +
                                     "--------------" +"\n" );
                               divider = 0;
                               divideend++;
                            }
                         }
                      }
                   }
                }
             }
          }
          else
          {
             for (int x=0;x<MAX_VAL;x++)
             {
                for (int y=0;y<MAX_VAL;y++)
                {
                   output = suds[x][y].ToStringReveal();
                   System.out.print(output);
                   sepcount++;
                   if (sepcount > 2)
                   {
                      sepcount = 0;
                      if (levlinecount < 2)
                      {
                         System.out.print ("|");
                         levlinecount++;
                      }
                      else
                      {
                         levlinecount = 0;
                         if (divider < 2)
                         {
                            System.out.print( "\n" );
                            divider++;
                         }
                         else
                         {
                            if (divideend < 2)
                            {
                               System.out.print( "\n" + "---------------" +
                                     "--------------" +"\n" );
                               divider = 0;
                               divideend++;
                            }
                         }
                      }
                   }
                }
             }
          }
       }
       
       
       public int GetFinalValue(int row, int col)
       {
          return suds[row-1][col-1].GetFinalValue();
       }
       public boolean IsCompleted(int row, int col)
       {
          return suds[row-1][col-1].IsCompleted();
          
       }
       public boolean ValuePresent(int row, int col, int val)
       {
          return suds[row-1][col-1].ValuePresent(val);
       }
       public int NumRemainingVals(int row, int col)
       {
          return suds[row-1][col-1].NumRemainingVals();
       }
       public int NumRemainingValsTotal()
       {
          int total = 0;
          for (int x=0;x<MAX_VAL;x++)
          {
             for (int y=0;y<MAX_VAL;y++)
             {
                total += suds[x][y].GetFinalValue();
             }
          }
          return total;
       }
       public boolean AllSingletons()
       {
          boolean answer = true;
          for (int x=0;x<MAX_VAL;x++)
          {
             for (int y=0;y<MAX_VAL;y++)
             {
                if (NOT_FINAL_YET == suds[x][y].GetFinalValue())
                   answer = false;
             }
          }
          return answer;
       }
       public boolean EmptyCellFound()
       {
          boolean answer = false;
          for (int x=0;x<MAX_VAL;x++)
          {
             for (int y=0;y<MAX_VAL;y++)
             {
                if (0 == suds[x][y].NumRemainingVals())
                   answer = true;
             }
          }
          return answer;
       }
       
       public void SetToBlank()
       {
          for (int x=0;x<MAX_VAL;x++)
          {
             for (int y=0;y<MAX_VAL;y++)
             {
                suds[x][y].SetToBlank();
             }
          }
       }
       public boolean ForcedSet(int row, int col, int val)
       {
          return suds[row-1][col-1].ForcedSet(val);
       }
       public boolean Complete(int row, int col)
       {
          return suds[row-1][col-1].Complete();
       }
       public boolean RuleOut(int row, int col, int val)
       {
          return suds[row-1][col-1].RuleOut(val);
       }
       public boolean SoftSet(int row, int col, int val)
       {
          return suds[row-1][col-1].SoftSet(val);
       }
       
       boolean Validate(int val)
       {
          if (val < MIN_VAL || val > MAX_VAL)
             return false;
          else
             return true;
       }
       boolean  IndexValidate(int n)
       {
          if (n < 0 || n > NUM_VALS-1)
             return false;
          else
             return true;
       }
    }
    
    class SudCell
    {
       private boolean digits[] = new boolean [9];
       private boolean completed;
       private int FinalValue;
       private int NumRemainingVals;
       private String String_Val,String_RVL;
       
       static final int MIN_VAL = 1;
       static final int MAX_VAL = 9;
       static final int NUM_VALS = 9;
       static final int SQRT_NUM_VALS = 3;
       static final int NOT_FINAL_YET = -1; 
       
       public SudCell()
       {
          for (int k=0;k<9;k++)
          {
             digits[k] = true;
          }
          completed = false;
       }
    
       public String ToString()
       {
          String output;
          int boolcount = 0;
          int whichdigit = 0;
          for (int x=0;x<9;x++)
          {
             if (digits[x] == true)
             {
                whichdigit = x;
                boolcount++;
             }
          }
          if (boolcount > 1 || boolcount < 1)
             output = " - ";
          else
          {
             whichdigit++;
             output = Integer.toString(whichdigit);
             output = " " + output + " ";
          }
    
          return output;
       }
       
       public String ToStringReveal()
       {
          String_RVL = "";
          if (digits[0] == true && digits[1] == false && digits[2] == false
                && digits[3] == false && digits[4] == false && digits[5] == false
                && digits[6] == false && digits[7] == false && digits[8] == false)
          {
             String_RVL = "1";
          }
          else if  (digits[0] == false && digits[1] == true && digits[2] == false
                && digits[3] == false && digits[4] == false && digits[5] == false
                && digits[6] == false && digits[7] == false && digits[8] == false)
          {
             String_RVL = "2";
          }
          else if  (digits[0] == false && digits[1] == false && digits[2] == true
                && digits[3] == false && digits[4] == false && digits[5] == false
                && digits[6] == false && digits[7] == false && digits[8] == false)
          {
             String_RVL = "3";
          }
          else if  (digits[0] == false && digits[1] == false && digits[2] == false
                && digits[3] == true && digits[4] == false && digits[5] == false
                && digits[6] == false && digits[7] == false && digits[8] == false)
          {
             String_RVL = "4";
          }
          else if  (digits[0] == false && digits[1] == false && digits[2] == false
                && digits[3] == false && digits[4] == true && digits[5] == false
                && digits[6] == false && digits[7] == false && digits[8] == false)
          {
             String_RVL = "5";
          }
          else if  (digits[0] == false && digits[1] == false && digits[2] == false
                && digits[3] == false && digits[4] == false && digits[5] == true
                && digits[6] == false && digits[7] == false && digits[8] == false)
          {
             String_RVL = "6";
          }
          else if  (digits[0] == false && digits[1] == false && digits[2] == false
                && digits[3] == false && digits[4] == false && digits[5] == false
                && digits[6] == true && digits[7] == false && digits[8] == false)
          {
             String_RVL =  "7";
          }
          else if  (digits[0] == false && digits[1] == false && digits[2] == false
                && digits[3] == false && digits[4] == false && digits[5] == false
                && digits[6] == false && digits[7] == true && digits[8] == false)
          {
             String_RVL = "8";
          }
          else if  (digits[0] == false && digits[1] == false && digits[2] == false
                && digits[3] == false && digits[4] == false && digits[5] == false
                && digits[6] == false && digits[7] == false && digits[8] == true)
          {
             String_RVL = "9";
          }
          else
          {
             for (int k=0;k<9;k++)
             {
                if (digits[0] = true)
                   k=k+1;
                String_RVL = String_RVL +(k);
             }
          }
          return String_RVL = " "+String_RVL+" ";
       }
    
       public int GetFinalValue()
       {
          if (digits[0] == true && digits[1] == false && digits[2] == false
                && digits[3] == false && digits[4] == false && digits[5] == false
                && digits[6] == false && digits[7] == false && digits[8] == false)
          {
             FinalValue = 1;
          }
          else if  (digits[0] == false && digits[1] == true && digits[2] == false
                && digits[3] == false && digits[4] == false && digits[5] == false
                && digits[6] == false && digits[7] == false && digits[8] == false)
          {
             FinalValue = 2;
          }
          else if  (digits[0] == false && digits[1] == false && digits[2] == true
                && digits[3] == false && digits[4] == false && digits[5] == false
                && digits[6] == false && digits[7] == false && digits[8] == false)
          {
             FinalValue = 3;
          }
          else if  (digits[0] == false && digits[1] == false && digits[2] == false
                && digits[3] == true && digits[4] == false && digits[5] == false
                && digits[6] == false && digits[7] == false && digits[8] == false)
          {
             FinalValue = 4;
          }
          else if  (digits[0] == false && digits[1] == false && digits[2] == false
                && digits[3] == false && digits[4] == true && digits[5] == false
                && digits[6] == false && digits[7] == false && digits[8] == false)
          {
             FinalValue = 5;
          }
          else if  (digits[0] == false && digits[1] == false && digits[2] == false
                && digits[3] == false && digits[4] == false && digits[5] == true
                && digits[6] == false && digits[7] == false && digits[8] == false)
          {
             FinalValue = 6;
          }
          else if  (digits[0] == false && digits[1] == false && digits[2] == false
                && digits[3] == false && digits[4] == false && digits[5] == false
                && digits[6] == true && digits[7] == false && digits[8] == false)
          {
             FinalValue = 7;
          }
          else if  (digits[0] == false && digits[1] == false && digits[2] == false
                && digits[3] == false && digits[4] == false && digits[5] == false
                && digits[6] == false && digits[7] == true && digits[8] == false)
          {
             FinalValue = 8;
          }
          else if  (digits[0] == false && digits[1] == false && digits[2] == false
                && digits[3] == false && digits[4] == false && digits[5] == false
                && digits[6] == false && digits[7] == false && digits[8] == true)
          {
             FinalValue = 9;
          }
          else
             FinalValue = NOT_FINAL_YET;
          return FinalValue;
       }
       public boolean IsCompleted()
       {
          return completed;
       }
       public boolean ValuePresent(int val)
       {
          if (digits[val-1] == true)
             return true;
          else
             return false;
       }
       public int NumRemainingVals()
       {
          int boolcount = 0;
          for (int x=0;x<9;x++)
          {
             if (digits[x] == true)
                boolcount++;
          }
          return boolcount;
       }
       
       public void SetToBlank()
       {
          for (int x=0;x<9;x++)
          {
             digits[x] = true;
          }
       }
       
       public boolean ForcedSet(int val)
       {  
          if(val < MIN_VAL || val > MAX_VAL)
             return false;
          else
          {
          for (int x=0;x<9;x++)
          {
             digits[x] = false;
          }
          digits[val-1] = true;
          return true;
          }
       }
       public boolean Complete()
       {
    
          if (digits[0] == true && digits[1] == false && digits[2] == false
                && digits[3] == false && digits[4] == false && digits[5] == false
                && digits[6] == false && digits[7] == false && digits[8] == false)
          {
             completed = true;
             return true;
          }
          else if  (digits[0] == false && digits[1] == true && digits[2] == false
                && digits[3] == false && digits[4] == false && digits[5] == false
                && digits[6] == false && digits[7] == false && digits[8] == false)
          {
             completed = true;
             return true;
          }
          else if  (digits[0] == false && digits[1] == false && digits[2] == true
                && digits[3] == false && digits[4] == false && digits[5] == false
                && digits[6] == false && digits[7] == false && digits[8] == false)
          {
             completed = true;
             return true;
          }
          else if  (digits[0] == false && digits[1] == false && digits[2] == false
                && digits[3] == true && digits[4] == false && digits[5] == false
                && digits[6] == false && digits[7] == false && digits[8] == false)
          {
             completed = true;
             return true;
          }
          else if  (digits[0] == false && digits[1] == false && digits[2] == false
                && digits[3] == false && digits[4] == true && digits[5] == false
                && digits[6] == false && digits[7] == false && digits[8] == false)
          {
             completed = true;
             return true;
          }
          else if  (digits[0] == false && digits[1] == false && digits[2] == false
                && digits[3] == false && digits[4] == false && digits[5] == true
                && digits[6] == false && digits[7] == false && digits[8] == false)
          {
             completed = true;
             return true;
          }
          else if  (digits[0] == false && digits[1] == false && digits[2] == false
                && digits[3] == false && digits[4] == false && digits[5] == false
                && digits[6] == true && digits[7] == false && digits[8] == false)
          {
             completed = true;
             return true;
          }
          else if  (digits[0] == false && digits[1] == false && digits[2] == false
                && digits[3] == false && digits[4] == false && digits[5] == false
                && digits[6] == false && digits[7] == true && digits[8] == false)
          {
             completed = true;
             return true;
          }
          else if  (digits[0] == false && digits[1] == false && digits[2] == false
                && digits[3] == false && digits[4] == false && digits[5] == false
                && digits[6] == false && digits[7] == false && digits[8] == true)
          {
             completed = true;
             return true;
          }
          else
             return false;
       }
       public boolean RuleOut(int val)
       {
          if (val == 1)
          {
             digits[0] = false;
             return false;
          }
          else if (val == 2)
          {
             digits[1] = false;
             return false;
          }
          else if (val == 3)
          {
             digits[2] = false;
             return false;
          }
          else if (val == 4)
          {
             digits[3] = false;
             return false;
          }
          else if (val == 5)
          {
             digits[4] = false;
             return false;
          }
          else if (val == 6)
          {
             digits[5] = false;
             return false;
          }
          else if (val == 7)
          {
             digits[6] = false;
             return false;
          }
          else if (val == 8)
          {
             digits[7] = false;
             return false;
          }
          else if (val == 9)
          {
             digits[8] = false;
             return false;
          }
          else
          return false;
       }
       public boolean SoftSet(int val)
       {
          for (int k=0;k<9;k++)
          {
             for (int l=1;l<10;l++)
             {
                if (val == l)
                {
                   digits[k] = true;
                }
             }
          }
          return false;
       }
       public boolean Validate(int val)
       {
          if (val >= MIN_VAL && val <= MAX_VAL)
          {
             return true;
          }
          else
             return false;
       }
    }

  2. #2
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    did you try debugging?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're trying to dereference an object which is null on the line that the NPE error is thrown. There's only one object that this can be attributed to,... so you must ask yourself, when do you ever initialize the items in this array?

  4. #4
    hongi is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default

    Why i can't just code like this
    Insert in to Sudoku class
    Java Code:
        public Sudoku()
       {
          SetToBlank();
       }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Sudoku randomizer help
    By mochibon in forum New To Java
    Replies: 0
    Last Post: 04-07-2010, 03:05 AM
  2. SUDOKU game problem
    By bumblyb33 in forum AWT / Swing
    Replies: 2
    Last Post: 05-05-2009, 11:18 PM
  3. Class Expected error
    By Radman23 in forum New To Java
    Replies: 11
    Last Post: 01-14-2009, 04:12 AM
  4. Bean class error
    By impact in forum Java Servlet
    Replies: 5
    Last Post: 08-16-2008, 03:46 PM
  5. Error in class
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 07-09-2007, 04:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •