Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Please help this is strange.

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help this is strange.

    ok here is my code i followed my instructors working example to the letter and i am getting this error

    H:\Pong\src\pong\Controls.java:54: error: non-static variable frame cannot be referenced from a static context
    frame.paper.draw();
    1 error

    i am a beginning Java student and i am not asking for anyone to do my project for me just some help solving this error thanks


    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package pong;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    /**
     *<p>Pong<p>
     *<p>The Game<p>
     *<p>Copyright; 2012<p>
     *<p>Alien Software<p> 
     * @author Jason Miller
     * @version 0.5.1
     */
    public class Controls extends JFrame implements ActionListener{
        private FlowLayout flo = new FlowLayout();
        private JButton btnStart,btnStop;
        private Timer tmrMoveBall = new Timer(10,new BallMover());
     
        private PongCollisions frame;
     
            public Controls(){
                btnStart = new JButton("Start");
                btnStop = new JButton("Stop");
     
                this.setLayout(flo);
                add(btnStart);
                add(btnStop);
     
                btnStart.addActionListener(this);
                btnStop.addActionListener(this);
            }      
         public Controls(PongCollisions frame){
             this();
             this.frame = frame;
         }
     
        @Override
        public void actionPerformed(ActionEvent ae) {
            if(ae.getSource() == btnStart){
                tmrMoveBall.start();
            }else{
                tmrMoveBall.stop();
            }
     
        }
     
        private static class BallMover implements ActionListener {
     
            @Override
            public void actionPerformed(ActionEvent ae) {
                frame.paper.draw();
            }
        }
    }


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Please help this is strange

    Quote Originally Posted by MillerJLee79 View Post
    ok here is my code i followed my instructors working example to the letter and i am getting this error

    H:\Pong\src\pong\Controls.java:54: error: non-static variable frame cannot be referenced from a static context
    frame.paper.draw();
    1 error
    You are missing something. The code that you posted does not define a PongCollisions class, and I get three errors:
    Controls.java:22: cannot find symbol
    symbol  : class PongCollisions
    location: class pong.Controls
        private PongCollisions frame;
                ^
    Controls.java:35: cannot find symbol
    symbol  : class PongCollisions
    location: class pong.Controls
         public Controls(PongCollisions frame){
                         ^
    Controls.java:54: non-static variable frame cannot be referenced from a static context
                frame.paper.draw();
                ^
    3 errors

    Are you absolutely sure that you are compiling the exact code that you posted?


    Cheers!

    Z

  3. #3
    Member suyog53's Avatar
    Join Date
    Sep 2012
    Location
    Mumbai
    Posts
    37
    My Mood
    Daring
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Please help this is strange

    Is this your full code?'

  4. #4
    Junior Member
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Please help this is strange.

    The class BallMover should not be static. Make it non static. That would resolve the issue

Similar Threads

  1. Strange boxes appearing
    By fishnj1333 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 11th, 2012, 06:36 AM
  2. something strange
    By frozen java in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 4th, 2011, 08:58 PM
  3. Strange error message
    By javapenguin in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 11th, 2011, 02:03 PM
  4. Strange problem with drawing string
    By Asido in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 26th, 2010, 04:38 PM
  5. Strange Compiling Error
    By crism85 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 13th, 2009, 01:59 AM