Results 1 to 5 of 5
- 01-12-2013, 05:35 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 24
- Rep Power
- 0
<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...
and now for the classJava 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; } }
This ends up throwing the NRE when I try to accessJava 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; } }on line 26 of the app.Java Code:Box[0].Displaybox(g);
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.
-
Re: Declaring array of Objects throwing NRE
Look *very* carefully at this line:
Will this for loop ever loop?Java Code:for(int i = 0;i >= 16;i++)

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).
- 01-12-2013, 05:48 PM #3
Member
- Join Date
- Jan 2013
- Posts
- 24
- Rep Power
- 0
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!
-
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.
- 01-12-2013, 06:01 PM #5
Member
- Join Date
- Jan 2013
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
Using an array of objects
By katiebear128 in forum New To JavaReplies: 2Last Post: 11-17-2011, 06:05 PM -
How to convert array of Objects into array of Strings
By elenora in forum Advanced JavaReplies: 1Last Post: 06-10-2011, 03:48 PM -
declaring a different array each time i loop
By MichaelT in forum New To JavaReplies: 13Last Post: 06-09-2011, 02:13 AM -
Need help declaring array
By earnest in forum New To JavaReplies: 7Last Post: 02-22-2011, 12:17 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks