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 8 of 8

Thread: static &netbeans

  1. #1
    Junior Member
    Join Date
    Jan 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default static &netbeans

    this program in notepad is correct i run it in cmd
    but this is not correct in netbeans
    the error is non-static variable this cannot be referenced from a static contex
    i change the SimpleFrame class up the SimpleFrameTest i run my program in netbeans
    this is not run notepad
    i do not understand!?
    import javax.swing.*;
     
     
    public class SimpleFrameTest
    {
       public static void main(String[] args)
       {
          SimpleFrame frame = new SimpleFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS  E);
          frame.setVisible(true);
       }
    }
     
    class SimpleFrame extends JFrame
    {
       public SimpleFrame()
       {
          setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
       }
     
       public static final int DEFAULT_WIDTH = 300;
       public static final int DEFAULT_HEIGHT = 200;
    }


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: static &netbeans

    Netbeans is just a convenient wrapper for the command line compiler javac. It is not possible for it to compile on the command line and not in netbeans since they do exactly the same thing.

    Your code runs correctly on my machine with ONE exception. It looks like you accidentally hit the space bar a few times

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS  E);

    Do you see those space in CLOS E? Remove them and it compiles.

    You can save yourself a heap of time by looking at the error message. It will tell you what line the problem is occuring on. Posting the error message will also help us help you so they are very important.

  3. #3
    Junior Member
    Join Date
    Jan 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: static &netbeans

    i posted error the error is
    non-static variable this cannot be referenced from a static contex
    in line SimpleFrame frame = new SimpleFrame();
    The code is my post have space in my computer do not space
    you run my code in netbeans or cmd
    i know this same
    you change the code and run it?
    class SimpleFrame extends JFrame
    {
       public SimpleFrame()
       {
          setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
       }
     
       public static final int DEFAULT_WIDTH = 300;
       public static final int DEFAULT_HEIGHT = 200;
    }
     
     
     
    public class SimpleFrameTest
    {
       public static void main(String[] args)
       {
          SimpleFrame frame = new SimpleFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
       }
    }
    this code run in netbeans and do not run in cmd!?
    Last edited by ardashir; January 7th, 2012 at 10:01 AM.

  4. #4
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: static &netbeans

    I still don't understand. This runs fine in netbeans. I didn't want to have to open it up to prove it but I did. If it compiles in the command prompt, it compiles in netbeans. Like I said, they do the same thing.

    i posted error the error is
    non-static variable this cannot be referenced from a static contex
    in line SimpleFrame frame = new SimpleFrame();
    No this is a description of the error you have typed up. Copy and paste the exact error message you are getting.

    What version of Java are you using. To check , type this in the command prompt
    $ javac -version
    javac 1.7.0_02

    What version of netbeans?

    Most likely this is a user error - something you are not doing correctly. There is a very slight chance it could be a problem with netbeans. You could try uninstalling and reinstalling.

  5. #5
    Junior Member
    Join Date
    Jan 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: static &netbeans

    my java and netbeans version
    Product Version: NetBeans IDE 7.0.1 (Build 201107282000)
    Java: 1.6.0_07
    i copy and past the error in netbeans
    I:\javaprogarm\SimpleFramTest\src\simpleframtest\SimpleFramTest.java:19: non-static variable this cannot be referenced from a static context
            SimpleFrame frame = new SimpleFrame();
    1 error
    I:\javaprogarm\SimpleFramTest\nbproject\build-impl.xml:603: The following error occurred while executing this line:
    I:\javaprogarm\SimpleFramTest\nbproject\build-impl.xml:245: Compile failed; see the compiler error output for details.
    BUILD FAILED (total time: 3 seconds)

  6. #6
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: static &netbeans

    Okay cool. Check the name of the java file. It should be SimpleFrameTest.java. In the error message it says the file is SimpleFramTest.java (missing the 'e' in Frame)

  7. #7
    Junior Member
    Join Date
    Jan 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: static &netbeans

    thank you
    my Problem is solution
    Last edited by ardashir; January 11th, 2012 at 01:32 AM.

  8. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: static &netbeans

    Quote Originally Posted by ardashir View Post
    thank you
    my Problem is solution
    Don't forget to credit the one, who helped you. Either add to their reputation or hit thanks that will auto add to reputation (if i am not wrong).

Similar Threads

  1. Replies: 9
    Last Post: November 5th, 2011, 10:22 AM
  2. Troubles with toString and static and non-static
    By BadgerWatch in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 3rd, 2011, 05:04 AM
  3. [SOLVED] non static variable this cant be referenced from a static context
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 06:13 PM
  4. non-static method cannot be referenced from a static context
    By Kaltonse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2010, 07:51 PM
  5. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM