Results 1 to 10 of 10
  1. #1
    just_java is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Unhappy WHY is it not def? PLZ HELP IT IS VERY SIMPLE!!

    ok I have an obj called Cell, and i made a 2D array, and I try to rest eveycell,
    What is wrong ???
    Java Code:
    package thegame;
    class Cell {
        boolean occupied = false;
        char own = ' ';
    }
    
    /**
     *
     * @author joseph
     */
    class Queendom {
        public static final int MAX_LENGHT = 400;
        Cell [][]Board = new Cell[400][400];
        private int lenght = 0;
    
    public void reset(){
          for(int i=0;i<lenght;i++){
            for(int j=0;j<lenght;j++){
              Board[i][j].occupied = false;
              Board[i][j].own = ' ';
            }
          }
        }
    now, I have in my main :
    Java Code:
    public class The_Game {
        public static void main(String[] args) {
            // TODO code application logic here
            Queendom Q = new Queendom();
            Q.reset();
         }
    }
    But the when I cal the Q.reset(), and it look at the Board[i][j].occupied = false; it fail, and says:

    Exception in thread "main" java.lang.NullPointerException
    at thegame.Queendom.reset(Queendom.java:33)
    at thegame.The_Game.main(The_Game.java:17)


    PLZ help me on this !!!!
    Thanks a lot

  2. #2
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default

    I tried to compile your codes, but the compiler didnt throw me any NullPointerException... are you sure that it is your problem?

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You don't actually say what line 33 of Queendom.java is!

    I'll assume that it is "Board[i][j].occupied = false;" You will get a NullPointerException if Board[i][j] is null. Where did you think Board[i][j] was assigned a nonnull value? (for any i/j).

  4. #4
    just_java is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default

    ya did you try to run it?
    b/c it does compile, but could you run it too?
    @pbrockway2:
    That is it; I didnt access/ use any elements in Board yet, I just want to reset all the occupied values to false.
    Last edited by just_java; 02-07-2010 at 07:54 AM.

  5. #5
    just_java is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default

    could any of you guys run this simple program?:rolleyes:
    Last edited by just_java; 02-07-2010 at 07:59 AM.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    There's really no reason to run the program (and to do that we would have to add the bits you have left out). There's no reason because you have already told us what happens: you get a NullPointerException when you try and access the array elements.

    By the way, you are accessing the array elements when you set occupied to false. This isn't a nit-pick, it's a very important point. The line:

    Board[i][j].occupied = false;

    means "I want to access the Board array at element i/j, take the Cell which is there and set its occupied field to false". Now the runtime complains because Board[i][j] is null and you can't set the occupied field of something that doesn't exist.

    The answer to the question I asked before is that - at the moment - you are not assigning the elements of the Board array nonnull values. And you have to do that to avoid the NPE. Probably in the Queendom constructor, or in the code that alters lenght.

    -----

    It really doesn't help to post code that doesn't correspond with the runtime stack trace. No way is the NPE occurring on line 33. And the reset() method itself doesn't do anything unless something is happening to lenght in code that you haven't posted.
    Last edited by pbrockway2; 02-07-2010 at 08:51 AM.

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

    Default

    Quote Originally Posted by just_java View Post
    Java Code:
    class Queendom {
        public static final int MAX_LENGHT = 400;
        Cell [][]Board = new Cell[400][400];
        private int lenght = 0;
    
    public void reset(){
          for(int i=0;i<lenght;i++){
            for(int j=0;j<lenght;j++){
              Board[i][j].occupied = false;
              Board[i][j].own = ' ';
            }
          }
        }
    You variable 'lenght' is zero so your other faults won't even get executed. Keep them for another thread.

    kind regards,

    Jos

  8. #8
    just_java is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default

    THANKS EVERYONE;
    I reset length outside:
    So indeed, length is 400;

    So How should I fix it?
    Thanks again

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    By assigning some nonnull values to the array elements.

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

    Default

    Quote Originally Posted by just_java View Post
    THANKS EVERYONE;
    I reset length outside:
    So indeed, length is 400;

    So How should I fix it?
    Thanks again
    You have an array of references to Cells but those references don't refer to cells yet; you have to do that yourself, as in Board[i][j]= new Cell(), for every element in that array.

    kind regards,

    Jos

Similar Threads

  1. Simple Help
    By Andy_M in forum New To Java
    Replies: 6
    Last Post: 01-09-2010, 09:10 PM
  2. Very simple q
    By fresh83 in forum New To Java
    Replies: 2
    Last Post: 12-26-2009, 10:06 AM
  3. New simple application using a simple database
    By webbusiness23 in forum New To Java
    Replies: 9
    Last Post: 08-03-2009, 02:55 AM
  4. Replies: 2
    Last Post: 05-04-2008, 05:27 PM
  5. simple GUI
    By dim_ath in forum New To Java
    Replies: 3
    Last Post: 01-07-2008, 03:00 PM

Tags for this Thread

Posting Permissions

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