8 Replies Latest reply: Jan 31, 2010 6:08 PM by 843789 RSS

    non-static method cannot be referenced from a static context

    843789
      I have filtered though an hour of forum posts and find that my problem seems different to everyone elses. I am trying to implement a linked queue through jroff and I keep having the error come up.

      Also I have an incompatible type error that comes up too. Does anyone know how to convert a string to an object?

      linkedqueue.java
      import java.util.NoSuchElementException;
      
      class linkedqueue <Object> {
      
         private class node{
            Object value = null;
            node link = null;
         }
      
         private node front = null;
         private node rear = null;
      /*
         public boolean empty (){
            return front == null;
         }
      */
         // ------- insert new obejct into rear of the list
         public void insert (String obj) {
           // stub if list is null return null
            node previous = null;
            // Find the end of the list.
            for( node itor = this.front; itor != null; itor = itor.link ){
               previous = itor;
            };
            // Make a new node.
            node newnode = new node();
            newnode.value = obj; <---- incompatible error here
            newnode.link = null;
            // Insert it at the end of the list or at the head, if empty.
            if( previous == null ) this.front = newnode;
                              else previous.link = newnode;
         }
      
        public String remove (){
          Object obj = front.value;
          String temp = obj.toString();
          front = front.link;
          return temp;
       }
      
      }
      jroff.java
      import java.io.*;
      import java.util.Scanner;
      import static java.lang.System.*;
      
      public class jroff{
         final static String STDIN_NAME = "-";
      public
         static void scanfile (String filename, Scanner infile) {
            err.printf ("STUB: filename = %s%n", filename);
            for (int linenr = 1; infile.hasNextLine(); ++linenr) {
               String line = infile.nextLine(); // get the line
               if(line.length()==0){
                 err.printf (".sp 1%n");
                 
               }
               else if(line.length()>=1){
              char c = line.charAt( 0 );
               if(c == '.'){
                 err.printf ("This line begins with a period.");
               }
               }
               err.printf ("STUB: %s: %4d: \"%s\"%n", filename, linenr, line);
               String[] words = line.split ("\\s+"); // split the line by spaces
               for (String word: words) {
      
                 linkedqueue.insert(word); <---static context error here
                  err.printf ("print STUB: word = \"%s\"%n", word);
               }
            }
         }
      
         public static void main (String[] args) {
                 linkedqueue wordqueue = new linkedqueue ();
            if (args.length == 0) {
               scanfile (STDIN_NAME, new Scanner (in));
            }else {
               for (String filename : args) {
                  if (filename.equals (STDIN_NAME)) {
                     scanfile (STDIN_NAME, new Scanner (in));
                  }else {
                     try {
                        Scanner scan = new Scanner (new File (filename));
                        scanfile (filename, scan);
                        scan.close();
                     }catch (IOException error) {
                        auxlib.warn (error.getMessage());
                     }
                  }
               }
            }
         }
      
      }
      I am trying to add the words printed to the rear of the linked queue.

      Any and all help will be greatly appraciated.
        • 1. Re: non-static method cannot be referenced from a static context
          699554
          beyondavatars wrote:
          I keep having the error come up.
          And the error message is?
          Also I have an incompatible type error that comes up too.
          And the full error message is?

          Mel
          • 2. Re: non-static method cannot be referenced from a static context
            843789
            Its in the thread name and body and I even commented the lines with the errors, but very well.

            2 errors found:
            File: /Users/info/Desktop/Java/final/jroff.java [line: 28]
            Error: /Users/info/Desktop/Java/final/jroff.java:28: non-static method insert(java.lang.String) cannot be referenced from a static context
            File: /Users/info/Desktop/Java/final/linkedqueue.java [line: 29]
            Error: /Users/info/Desktop/Java/final/linkedqueue.java:29: incompatible types
            found : java.lang.String
            required: Object
            • 3. Re: non-static method cannot be referenced from a static context
              699554
              beyondavatars wrote:
              Its in the thread name and body and I even commented the lines with the errors, but very well.
              No they are not the full error messages.
              2 errors found:
              File: /Users/info/Desktop/Java/final/jroff.java [line: 28]
              Error: /Users/info/Desktop/Java/final/jroff.java:28: non-static method insert(java.lang.String) cannot be referenced from a static context
              File: /Users/info/Desktop/Java/final/linkedqueue.java [line: 29]
              Error: /Users/info/Desktop/Java/final/linkedqueue.java:29: incompatible types
              found : java.lang.String
              required: Object
              But this is!

              The first error tells your exactly what is going on. On line 28 of "jroff" you are calling a non static method from a static context. That means you need to instantiate the Object first and call the method, or change the method to static.

              The second error is straight forward. On line 29 of linkedqueue an Object is expected but a String is found. You can cast a String into an Object like so:
              String foo = "foofoo";
              Object obj = (Object) foo;
              But do note that a String is an Object so this is also fine:
              String foo = "foofoo";
              Object obj = foo;
              So explicit casting is not necessary.

              Finally by convention class names start with a lower case letter.

              Mel
              • 4. Re: non-static method cannot be referenced from a static context
                843789
                Thank you for helping me with the casting.
                Melanie_Green wrote:
                The first error tells your exactly what is going on. On line 28 of "jroff" you are calling a non static method from a static context. That means you need to instantiate the Object first and call the method, or change the method to static.
                What code would I need to add or change to correct this error? Could you provide me with an example?
                • 5. Re: non-static method cannot be referenced from a static context
                  699554
                  beyondavatars wrote:
                  What code would I need to add or change to correct this error? Could you provide me with an example?
                  I have already told you. You are calling a non-static method from a static context. Hence the error message:

                  So you can either instantiate the Object with the non-static method, then call the non-static method from this instantiation.

                  Or you can change the non-static method to static.

                  The former is the best idea in this situation.

                  Mel
                  • 6. Re: non-static method cannot be referenced from a static context
                    699554
                    If you are still stuck, scroll down this page to [Class Methods|http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html].

                    Mel
                    • 7. Re: non-static method cannot be referenced from a static context
                      843789
                      Melanie_Green wrote:
                      If you are still stuck, scroll down this page to [Class Methods|http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html].

                      Mel
                      They are helpful it's just that try it and every way I get an error.

                      I tried
                         for (String word: words) {
                                 node wordqueue = new node ();
                                 wordqueue.insert(word);
                                  err.printf ("print STUB: word = \"%s\"%n", word);
                               }
                      Still not working.
                      • 8. Re: non-static method cannot be referenced from a static context
                        843789
                        I seem to have fixed it now, thank you for your help :)