Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

non-static method cannot be referenced from a static context

843789Jan 31 2010 — edited Jan 31 2010
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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 28 2010
Added on Jan 31 2010
8 comments
902 views