Results 1 to 10 of 10
  1. #1
    Tyre is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default 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.

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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.

  3. #3
    Tyre is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    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()?

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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.

  5. #5
    Tyre is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    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;
                        }
                 }
                }
    }

  6. #6
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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.

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

    Default

    Quote Originally Posted by Tyre View Post
    Java Code:
    public class BuildPyramid extends GraphicsProgram {
    
        public static void main(String[] args) {
           new BuildPyramid();
           BuildPyramid();
        }
            
            private BuildPyramid() { }
    You do have a private constructor for that class but you don't have a static method named BuildPyramid() (bad name btw).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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.

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

    Default

    Quote Originally Posted by Dark View Post
    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.
    As it is now the program isn't even compiling; I'll keep my mouth shut and leave this victim to you ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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

  1. Replies: 7
    Last Post: 09-24-2010, 05:09 AM
  2. Replies: 1
    Last Post: 02-10-2009, 10:03 AM
  3. Non-Static method in static context error
    By wizmang in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 08:51 AM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

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