Results 1 to 10 of 10
Thread: Static Versus Non-Static Error
- 04-15-2011, 05:55 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 18
- Rep Power
- 0
Static Versus Non-Static Error
I'm working in NetBeans 6.9.1 and have almost finished my first java project. It should build a pyramid of blocks with 14 on the bottom but I can't even get it to run. I get a red squiggly under line 15 that it cannot find the symbol method BuildPyramid. How do I get it to run?
Thanks for all the help!
Java Code:// This project should build a pyramid with a base of 14 blocks // of block size 12 x 30 pixels. The pyramid should be centered in the screen. package pleasework; import acm.graphics.*; import acm.program.*; public class BuildPyramid extends GraphicsProgram { private final int BRICK_WIDTH = 30; private final int BRICK_HEIGHT = 12; private final int BRICKS_IN_BASE = 14; public static void main(String[] args) { new BuildPyramid(); System.out.BuildPyramid(); } public void BuildPyramid() { int screenW = getHeight(); for(int i=0;(i==BRICKS_IN_BASE - 1);i++) { //starts a for loop that runs as many times as their are bricks in the base for(int j = 0; j > ((BRICKS_IN_BASE - 1) - i);j++) { //for loop to create a line counting down from bricks in base to one int blockPosition = screenW/14 + ((j/2) * BRICK_WIDTH); int rowHeight = (BRICKS_IN_BASE * BRICK_HEIGHT) * (BRICK_HEIGHT * i); GRect rect = new GRect(blockPosition,rowHeight,BRICK_WIDTH,BRICK_HEIGHT); add(rect); blockPosition = blockPosition + BRICK_WIDTH; } } } }Last edited by Tyre; 04-15-2011 at 05:57 PM.
- 04-15-2011, 06:00 PM #2
What is System.out.BuildPyramind();? I've never seen that in the API before.
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 04-15-2011, 06:10 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 18
- Rep Power
- 0
I won't say that I made that up, but I couldn't get it to output anything, so yeah I made that up. Even if I delete it and my main body is just BuildPyramid(); then it gives me all these errors and nothing outputs.
I have made the method, so how do I format it to run BuildPyramid()?
- 04-15-2011, 06:18 PM #4
Your method of BuildPyramid is void, meaning you cannot return anything. You can keep it void by putting the System.out.println(myVariable); line inside the loop. As I'm writing this I'm being forced away from the screen for a couple of minutes but I'll be back to take a closer look and help you out some more.
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 04-15-2011, 06:26 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 18
- Rep Power
- 0
Sounds good. I took away the void part and added your suggestion for it to output "rect", but the main still doesn't recognize BuildPyramid().
Java Code:// This project should build a pyramid with a base of 14 blocks // of block size 12 x 30 pixels. The pyramid should be centered in the screen. package pleasework; import acm.graphics.*; import acm.program.*; import java.lang.System; public class BuildPyramid extends GraphicsProgram { private final int BRICK_WIDTH = 30; private final int BRICK_HEIGHT = 12; private final int BRICKS_IN_BASE = 14; public static void main(String[] args) { new BuildPyramid(); BuildPyramid(); } private BuildPyramid() { int screenW = getHeight(); for(int i=0;(i==BRICKS_IN_BASE - 1);i++) { //starts a for loop that runs as many times as their are bricks in the base for(int j = 0; j > ((BRICKS_IN_BASE - 1) - i);j++) { //for loop to create a line counting down from bricks in base to one int blockPosition = screenW/14 + ((j/2) * BRICK_WIDTH); int rowHeight = (BRICKS_IN_BASE * BRICK_HEIGHT) * (BRICK_HEIGHT * i); GRect rect = new GRect(blockPosition,rowHeight,BRICK_WIDTH,BRICK_HEIGHT); add(rect); System.out.println(rect); blockPosition = blockPosition + BRICK_WIDTH; } } } }
- 04-15-2011, 06:34 PM #6
I'm not sure what removing void and replacing it with private does if you're still not returning anything... However you put the println command in the loop so it should print when it finally compiles.
Now the next thing is, where is your new BuildPyramid(); being stored? I'm not sure how to access an object unless it has a reference.- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 04-15-2011, 06:40 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 04-15-2011, 06:45 PM #8
That was a little more direct than I was intending, he isn't understanding why his program doesn't run because he doesn't understand how everything works. That's why I was trying to make him think through his problems. To help him get a better grasp of understanding.
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 04-15-2011, 06:50 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 04-15-2011, 07:15 PM #10
Yes I understand why its not compiling, but he doesn't. If it was simple syntax like he forgot a ; somewhere then I would point it out, and then if it wasn't completing the desired output yet he had a decent grasp of the subject I would point it out again with an explanation.
However since you offered some advice, I will wait to see if he still has problems.Last edited by Dark; 04-15-2011 at 07:25 PM.
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
Similar Threads
-
Can't make static reference to non-static method -> huh?! Simple car prgm
By enerj in forum New To JavaReplies: 7Last Post: 09-24-2010, 05:09 AM -
static method sparks error on overriding non-static method
By MuslimCoder in forum New To JavaReplies: 1Last Post: 02-10-2009, 10:03 AM -
Non-Static method in static context error
By wizmang in forum New To JavaReplies: 4Last Post: 04-24-2008, 08:51 AM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks