non-static method cannot be referenced from a static context

Joined
Jan 31, 2010
Messages
1
Reaction score
0
I'm new but I have been searching for the answers to this problem for 2 hours and have just about given up. Every one else's code situation with the same error looks different. Perhaps you guys can help me.

I am attempting to implement a linked queue in java but have the error "non-static method insert(java.lang.String) cannot be referenced from a static context".

linkedqueue.java
Code:
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 = object;
      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
Code:
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); // <----- error is 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());
               }
            }
         }
      }
   }

}

Any help would be appreciated
 
Joined
Jan 29, 2011
Messages
2
Reaction score
0
Hey are you at ucsc in cmps12b with mackey?

I am working on this project right now as well. I'm stumped. Hit me up if you figured out this problem and/or you want to work together on this.
 
Joined
Jan 29, 2011
Messages
2
Reaction score
0
When you call a method from linkedqueue.java, such as insert(), you need to use wordqueue.insert() so it calls the method with the linkedlist you are working with at the moment, otherwise insert doesn't know what data to deal with.


also, I called
Code:
linkedqueue wordqueue = new linkedqueue ();
in the scanfile function otherwise you'd have to pass the linkedqueue wordqueue to the scanfile function from main anyways.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,978
Messages
2,570,180
Members
46,716
Latest member
venkatesh

Latest Threads

Top