Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2013
    Posts
    24
    Rep Power
    0

    Lightbulb <Solved>Declaring array of Objects throwing NRE

    Hello,
    I looked around and read alot of posts. I have found nothing that is helping me understand what I am doing wrong. I am fairly certain there is an issue with how I am declaring my array of Box Objects.
    I am assuming you'd all alike the main applet adn then the class...

    Java Code:
    import java.applet.Applet;
    import java.awt.*;
    
    public class Grid extends Applet 
    {
    public Box[] Boxes = new Box[16];
    
     
    public void init()
            {
        for(int i = 0;i >= 16;i++)
        {
          Boxes[i] = new Box(12,12,12,12,Color.RED);
          Boxes[i].setUpperLeftX(i*12);
          Boxes[i].setUpperLeftY(i*12);
          Boxes[i].setWidth(12);
          Boxes[i].setHeight(12);
         
          Boxes[i].setBoxColor(RandColor());
        }
             }
    
        public void paint(Graphics glyph)
        {
      
            Boxes[0].displayBox(glyph);
         }
        
        public Color RandColor()
        {
          int RandRValue = ((int)(Math.random()* 254));
          int RandGValue = ((int)(Math.random()* 254));
          int RandBValue = ((int)(Math.random()* 254));
          Color randColor = new Color(RandRValue,RandGValue,RandBValue);
          return randColor;
        }
    }
    and now for the class

    Java Code:
    import java.awt.*;
    
    public class Box
    {
    private int upperLeftX = 0;
    private int upperLeftY = 0;
    private int height = 0;
    private int width = 0;
    private Color boxColor;
    
    //-Constructor-//
    
    public Box(int upperX, int upperY, int h, int w, Color MyColor)
          {
                /*this.upperLeftX = upperX;
                this.upperLeftY = upperY;
                this.height = h;
                this.width = w;
                this.boxColor = MyColor;*/
      
                this.setUpperLeftX(upperX);
                this.setUpperLeftY(upperY);
                this.setWidth(w);
                this.setHeight(h);
                this.setBoxColor(MyColor);
                
          }
    
    
    
    public void displayBox(Graphics BoxGlyph)
          {
              BoxGlyph.setColor(this.getBoxColor());
              
              BoxGlyph.fillRect(this.getUpperLeftX(), this.getUpperLeftY(),  this.getWidth(), this.getHeight());
          }
    
    public int getUpperLeftX() {
      return upperLeftX;
    }
    
    public int getUpperLeftY() {
      return upperLeftY;
    }
    
    public int getHeight() {
      return height;
    }
    
    public int getWidth() {
      return width;
    }
    
    public Color getBoxColor() {
      return boxColor;
    }
    
    public void setUpperLeftX(int upperLeftX) {
      this.upperLeftX = upperLeftX;
    }
    
    public void setUpperLeftY(int upperLeftY) {
      this.upperLeftY = upperLeftY;
    }
    
    public void setHeight(int height) {
      this.height = height;
    }
    
    public void setWidth(int width) {
      this.width = width;
    }
    
    public void setBoxColor(Color boxColor) {
      this.boxColor = boxColor;
    }
    
    }
    This ends up throwing the NRE when I try to access
    Java Code:
    Box[0].Displaybox(g);
    on line 26 of the app.

    I have tried to alter the declaration to look like

    Box Boxes;
    Boxes = new Box[16];

    and several other combinations. Eclipse seems to only let me declare it as you see above.
    I have been hammering away at this for two days, and I am going insane.
    I would be very grateful if anyone can help.

    Thanks!
    Last edited by LaughingSeraph; 01-12-2013 at 10:31 PM.

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

    Default Re: Declaring array of Objects throwing NRE

    Look *very* carefully at this line:

    Java Code:
    for(int i = 0;i >= 16;i++)
    Will this for loop ever loop?

    As an aside, you'll want to have your code adhere to Java naming standards including all class names beginning with an uppercase letter and all variable and method names with a lowercase letter. You'll also want to avoid use of "magic" numbers like 16 above, and instead use Boxes.length (which again should be boxes.length).

  3. #3
    Join Date
    Jan 2013
    Posts
    24
    Rep Power
    0

    Default Re: Declaring array of Objects throwing NRE

    omg, AHHHH!

    I may cry man.
    for (int i = 0; i LESS THAN array.length;i++)
    {
    programmer.makestupidmistake(goinsane);
    }

    Thanks!

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

    Default Re: Declaring array of Objects throwing NRE

    You're welcome!
    Show me a programmer who has never made a basic mistake, and I'll alert the news media that the Cubs are going to win the world series this year.

  5. #5
    Join Date
    Jan 2013
    Posts
    24
    Rep Power
    0

    Default Re: Declaring array of Objects throwing NRE

    PS, I wish I'd found this place two days ago. The instructor made me write out each adn every box array by hand. Again, thanks!

Similar Threads

  1. Using an array of objects
    By katiebear128 in forum New To Java
    Replies: 2
    Last Post: 11-17-2011, 06:05 PM
  2. How to convert array of Objects into array of Strings
    By elenora in forum Advanced Java
    Replies: 1
    Last Post: 06-10-2011, 03:48 PM
  3. declaring a different array each time i loop
    By MichaelT in forum New To Java
    Replies: 13
    Last Post: 06-09-2011, 02:13 AM
  4. Need help declaring array
    By earnest in forum New To Java
    Replies: 7
    Last Post: 02-22-2011, 12:17 AM

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
  •