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

Thread: compiler error "Cannot make a static reference to the non-static field TextArea1"

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default compiler error "Cannot make a static reference to the non-static field TextArea1"

    I did write a small application that catches and displays UDP messages sent via a certain port.
    It works fine whith "screen dump" output (System.out.println) but if I try to send the output to a TextArea I get a compiler error "Cannot make a static reference to the non-static field TextArea1"
    See code below.
    How can I fix this?

    Rik

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.IOException; 
    import java.net.DatagramPacket; 
    import java.net.DatagramSocket; 
     
     
    public class TextAreaApplet extends Applet implements ActionListener {
     public void init() {
      Button1 = new Button("Clear");
      Button1.addActionListener(this);
      TextArea1 = new TextArea("",5,40);
      TextArea1.setEditable(false);
      add(Button1);
      add(TextArea1);
     }
     
     public static void main(String[] args) throws IOException { 
       String NewData;
       DatagramSocket ds= new DatagramSocket(2237);
       byte[] receive= new byte[65535]; 
       DatagramPacket DpReceive= null; 
       while (true) { 
         DpReceive= new DatagramPacket(receive,receive.length);
         ds.receive(DpReceive);
         NewData= data(receive)+"\n\r";
         System.out.println(NewData); // this works fine
    //    TextArea1.append(NewData); // compiler error "Cannot make a static reference to the non-static field TextArea1"
         if (data(receive).toString().equals("bye")) {
           System.out.println("Client sent bye ... ");
           ds.close();
           break; 
         } 
         receive= new byte[65535];
       } 
     } 
     
     public void actionPerformed(ActionEvent e) {
      TextArea1.setText("");
     }
     
     public static StringBuilder data(byte[] a) {
       if (a == null) return null; 
       StringBuilder ret = new StringBuilder(); 
       int i = 0; 
       while (a[i] != 0) { 
         ret.append((char) a[i]); 
         i++; 
       } 
       return ret; 
     }  
     
     TextArea TextArea1;
     Button Button1;
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,337
    Thanks
    53
    Thanked 2,394 Times in 2,348 Posts

    Default Re: compiler error "Cannot make a static reference to the non-static field TextArea1"

    Cannot make a static reference to the non-static field TextArea1
    Code in a static method can not access fields that only exist when an instance of the class exists.
    Move the code that references textArea1 into a method that is not static.

    Applets are no longer supported in most browsers. Why does the code extend Applet?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: compiler error "Cannot make a static reference to the non-static field TextArea1"

    Please excuse my stupid questions, but Java is completely new to me.
    I just need a simple platfporm independent app that displays UDP messages from a given port, and thought of Java.

    Code in a static method can not access fields that only exist when an instance of the class exists.
    OK, I get that.

    Move the code that references textArea1 into a method that is not static.
    I created a method:
    public void ShowMessage(String s) {
       TextArea1.append(s);
     }
    But when I call this method from within main (
    ShowMessage(NewData);
    ) I get the same compiler error on that line.

    Applets are no longer supported in most browsers. Why does the code extend Applet?
    I just want to show the messages in some kind of box, if there is another way than applets that is fine for me (again, I am completely new to Java).

    Thanks,

    Rik

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,337
    Thanks
    53
    Thanked 2,394 Times in 2,348 Posts

    Default Re: compiler error "Cannot make a static reference to the non-static field TextArea1"

    The main method should create an instance of the class and let the constructor do the rest. Move the code currently in main to the constructor.



    Take a look at this list of topics for java: https://docs.oracle.com/javase/tutor...ybigindex.html
    Look at this part: https://docs.oracle.com/javase/tutor...ing/index.html
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: compiler error "Cannot make a static reference to the non-static field TextArea1"

    To avoid the error you're talking about I do the following:

    public class SomeClass {
       public static void main(String[] args) {
           new SomeClass().start();
       }
     
       public void start() {
            // put code in here that would normally go in 
            // main
       }
     
       //  rest of methods, static or otherwise
     
    }

    There are always exceptions of course, depending on what you're writing. But most of the time
    the above works just fine.

    Regards,
    Jim

  6. #6
    Junior Member
    Join Date
    Mar 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: compiler error "Cannot make a static reference to the non-static field TextArea1"

    Hello Jim, Norm,

    thank you both for your help.
    As mentioned before I am a Java newbie and only did some very basic reading on Java before writing the application (mostly cut & paste from pieces of code I found on the web).
    Maybe I should read/study a bit more about Java basics before going on ;-) .

    Rik

  7. #7
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: compiler error "Cannot make a static reference to the non-static field TextArea1"

    You can also check out the Java tutorials which provide a good basic start. The link is in my signature.

    Regards,
    Jim

Similar Threads

  1. Replies: 7
    Last Post: March 3rd, 2019, 07:44 PM
  2. Cannot make static reference to a non-static method?
    By SamJava_the_Hut in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 6th, 2018, 09:45 AM
  3. [SOLVED] Need help desperately: "non-static variable this cannot be referenced from a static context"
    By mikbferguson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 2nd, 2017, 08:27 PM
  4. Replies: 9
    Last Post: November 5th, 2011, 10:22 AM
  5. Replies: 10
    Last Post: October 26th, 2011, 02:22 PM

Tags for this Thread