Results 1 to 5 of 5
  1. #1
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default Help to debug a common error please!

    My error is:

    Exception in thread "main" java.lang.NullPointerException
    at sudoku_solver.Main.main(Main.java:31)

    I did a bit of googling and it seems that my error arises because one of my objects is not initialized correctly and is therefore null. The thing is that I'm rather new to this Java lark and am not sure which object it is and how to rectify the problem. Anyway here's the code (there's lots of it I'm afraid as I'm not sure what the root cause is and apologies in advance I don't know how to tag it as code). Thanks:

    package sudoku_solver;



    public class Main {



    public static void main(String[] args) {

    /* Instantiate objects */

    Sudoku mySudoku = new Sudoku();
    Grid aGrid = new Grid();

    for(int x=1; x<10; x++) {

    for(int y=1; y<10; y++) {

    aGrid.fillContents(x, y);// fills grid

    }

    }


    for(int i=1; i<10; i++) {

    mySudoku.myRow[i].fillContents(i,aGrid); // fills all rows THIS IS WHERE MY FIRST ERROR OCCURS
    mySudoku.myCol[i].fillContents(i,aGrid); // fills all cols

    }

    for(int x=1; x<4; x++) {

    for(int y=1; y<4; y++) {

    mySudoku.myBox[x][y].fillContents(x, y, aGrid); //fills all boxes

    }

    }

    }

    }

    package sudoku_solver;



    public class Sudoku {

    /* Declare variables */

    public Grid myGrid;
    public Row[] myRow = new Row[9];
    public Col[] myCol = new Col[9];
    public Box[][] myBox = new Box[3][3];

    /* Instantiate objects */



    /* Declare methods */

    public void checkCell(Grid aGrid, int x,int y) {

    }

    public void checkNum(int x) {

    }

    }

    package sudoku_solver;

    import java.util.Scanner;

    public class Grid {

    /* Declare variables */

    public int[][] contents = new int[9][9];

    /* Instantiate objects */



    /* Declare methods */

    public void fillContents(int colNum, int rowNum) {

    Scanner myScanner = new Scanner(System.in);

    System.out.println("Assuming that the origin (0,0) is at the bottom-left corner of the grid, please enter the number in cell (" + colNum + "," + rowNum + "). Use zeros for blank spaces.");

    contents[colNum-1][rowNum-1]=myScanner.nextInt();

    }

    }

    package sudoku_solver;

    public class Col {

    /* Declare variables */

    public int[] contents = new int[9];
    public boolean[] holdInteger = new boolean[9];

    /* Instantiate objects */



    /* Declare methods */

    public void fillContents(int colNum, Grid aGrid) {

    for(int i=0; 1<9; i++) {

    contents[i]=aGrid.contents[colNum-1][i];

    }

    }

    }

    package sudoku_solver;



    public class Row {

    /* Declare variables */

    public int[] contents = new int[9];
    public boolean[] holdInteger = new boolean[9];

    /* Instantiate objects */



    /* Declare methods */

    public void fillContents(int RowNum, Grid aGrid) {

    for(int i=0; 1<9; i++) {

    contents[i]=aGrid.contents[i][RowNum-1];

    }

    }

    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by TaxpayersMoney View Post
    My error is:

    Exception in thread "main" java.lang.NullPointerException
    at sudoku_solver.Main.main(Main.java:31)

    mySudoku.myRow[i].fillContents(i,aGrid); // fills all rows THIS IS WHERE MY FIRST ERROR OCCURS
    If you create an array of Row objects all the elements of the array potentially refer to a Row object each but they don't refer to such Objects yet; you have to create a Row object for each array element individually (in a loop would be quite convenient). If you don't do that each array element will be null, hence the exception that was thrown at you.

    kind regards,

    Jos

  3. #3
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    Thanks for the help and very quick response. I tried:

    for(int i=1; i<10; i++) {

    Row myRow[i] = new Row();

    }

    Needless to say it didn't work. Grumbling about not being able to find symbol method Row(). Expected ; expected [. Any ideas?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by TaxpayersMoney View Post
    Thanks for the help and very quick response. I tried:

    for(int i=1; i<10; i++) {

    Row myRow[i] = new Row();

    }

    Needless to say it didn't work. Grumbling about not being able to find symbol method Row(). Expected ; expected [. Any ideas?
    It's just a compilation error; do this instead:

    Java Code:
    for(int i=0; i<myRow.length; i++) 
       myRow[i] = new Row();
    kind regards,

    Jos

  5. #5
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

Similar Threads

  1. Replies: 4
    Last Post: 04-23-2010, 05:51 PM
  2. Replies: 2
    Last Post: 11-06-2009, 02:56 PM
  3. error debug
    By stid in forum Eclipse
    Replies: 2
    Last Post: 10-16-2009, 07:44 PM
  4. Common error page using struts tiles...?
    By prabhurangan in forum Web Frameworks
    Replies: 0
    Last Post: 08-28-2008, 02:34 PM
  5. Error logged from JDT Debug UI Window
    By tsc in forum Eclipse
    Replies: 1
    Last Post: 07-10-2008, 08:37 AM

Posting Permissions

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