This discussion is archived
4 Replies Latest reply: Nov 11, 2007 2:34 PM by 807603 RSS

Why does it say the boolean cannot be de-referanced?

807603 Newbie
Currently Being Moderated
I'm writing Conway's Game of Life for my class in Java,
Here's the section of code that's giving me trouble:
     public static void give_life(boolean[][]grid_of_life, int x, int y)
     {
          System.out.println("width = " +x);
          System.out.println("height = "+y);
          int counter = 0;

          for(int i = 0;i<x;i++)
          {
               for(int j = 0;j<y;j++)
               {
                    if(grid_of_life[i][j] == true)
                    {
                         if(grid_of_life[i][j+1] == true)   counter++;
                         if(grid_of_life[i+1][j+1] == true) counter++;
                         if(grid_of_life[i+1][j] == true)   counter++;
                         if(grid_of_life[i+1][j-1] == true) counter++;
                         if(grid_of_life[i][j-1] == true)   counter++;
                         if(grid_of_life[i-1][j-1] == true) counter++;
                         if(grid_of_life[i-1][j] == true)   counter++;
                         if(grid_of_life[i-1][j+1] == true) counter++;
                              if(counter<=1) grid_of_life[i][j].equals(false); //ERROR: says 'boolean cannot be dereferanced
                              if(counter>=4) grid_of_life[i][j].equsls(false);//ERROR: says 'boolean cannot be dereferanced
                              counter = 0;
                    }
                    else
                    {
                         if(grid_of_life[i][j+1] == true)   counter++;
                         if(grid_of_life[i+1][j+1] == true) counter++;
                         if(grid_of_life[i+1][j] == true)   counter++;
                         if(grid_of_life[i+1][j-1] == true) counter++;
                         if(grid_of_life[i][j-1] == true)   counter++;
                         if(grid_of_life[i-1][j-1] == true) counter++;
                         if(grid_of_life[i-1][j] == true)   counter++;
                         if(grid_of_life[i-1][j+1] == true) counter++;
                              if(counter == 3) grid_of_life[i][j].equals(true);//ERROR: says 'boolean cannot be dereferanced
                              counter = 0;
                    }
               }
          }                    
     
//      check the result
          for(int i = 0;i<x;i++)
          {
               for(int j = 0;j<y;j++)
               {
               System.out.println("Final State at: " +(i+1)+ " " +(j+1)+ " = " + grid_of_life[i][j]);
               }
          }

                                   
                           

     return;
     }
  • 1. Re: Why does it say the boolean cannot be de-referanced?
    807603 Newbie
    Currently Being Moderated
    if(counter<=1) grid_of_life[i][j].equals(false); //ERROR: says 'boolean cannot be dereferanced
    if(counter>=4) grid_of_life[i][j].equsls(false);//ERROR: says 'boolean cannot be dereferanced
    
    // Are you comparing boolean values or are you setting
    // grid_of_life[i][j] to false?
    
    if (counter <= 1) grid_of_life[i][j] = false;
    if (counter >= 4) grid_of_life[i][j] = false;
    Edited by: lethalwire on Nov 11, 2007 3:45 PM

    retarded form scripts
  • 2. Re: Why does it say the boolean cannot be de-referanced?
    807603 Newbie
    Currently Being Moderated
    i'm setting whatever is at grid_of_life[i][j] to false, if there is 1 or less 'trues' around it, or 4 or more 'trues' around it.
    So imagine at [j] there are 4 trues around it, it should become false. Same thing if there are 1 or less. If there are two or three, nothing should happen.
  • 3. Re: Why does it say the boolean cannot be de-referanced?
    DarrylBurke Guru Moderator
    Currently Being Moderated
    You are attempting to call a equals(...) method on a boolean, but boolean is a primitive and primitives don't have methods.

    The correct way to test equality of primitives is with the == operator, and for boolean primitives even that is redundant.

    Try this sample to understand this:
    boolean t = true;
    boolean f = false;
    
    if (t)
            System.out.println ("t is true");
    
    if (!f)
            System.out.println ("f is not true");
    db
  • 4. Re: Why does it say the boolean cannot be de-referanced?
    807603 Newbie
    Currently Being Moderated
    thenextbesthang wrote:
    i'm setting whatever is at grid_of_life[i][j] to false, if there is 1 or less 'trues' around it, or 4 or more 'trues' around it.
    So imagine at [j] there are 4 trues around it, it should become false. Same thing if there are 1 or less. If there are two or three, nothing should happen.
    In any case of you changing a value, use =.
    int a = 3;
    boolean a1 = false;
    Same thing with your array

    boolean[][] a2 = new boolean[2][2];
    a2[0][0] = false;

    Don't use
    a2[0][0].equals(false)
    Read the reply someone posted for the reason.