0

i'm using the following code to save the content of some webpages in a method, but i keep having the error which says : ("Cannot make a static reference to the non-static method convertor() from the type ConvertUrlToString")

please consider i just used this class to find the problem of my main project where i have the similar method ( convertor() ) in another class without "public static void main(String args[])"

package testing;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class ConvertUrlToString {
    public static void main(String args[]) throws IOException{
        convertor(); 
    }
    public void convertor() throws IOException    
    {
         Document doc =                        Jsoup.connect("http://www.willhaben.at/iad/immobilien/mietwohnungen/wien/wien-1060-        mariahilf/").get();
         String text = doc.body().text();
         Document doc1 = Jsoup.connect("http://www.google.at/").get();
         String text1 = doc1.body().text();    
         Document doc2 = Jsoup.connect("http://www.yahoo.com/").get();
         String text2 = doc2.body().text();
         System.out.println(text);
         System.out.println(text1);
         System.out.println(text2);
    }  
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • in main(): `new ConvertUrlToString().convertor();`. main() is static, so you need to create an instance before attempting to call non-static methods – Gus Jan 14 '15 at 21:15

1 Answers1

1

Just add a static keyword in yout convertor method. Since you are trying to invoke it inside a static context (main method), this method must be static. If you don't want to make it static, you will need to make an instance of ConvertUrlToString and them invoke the convertor method.

public static void main( String[] args ) {
    try {
        convertor();
    catch ( IOException exc ) {
    }
}

public static void convertor() throws IOException {
    ...
}

Or

public static void main( String[] args ) {
    try {
        new ConvertUrlToString().convertor();
    catch ( IOException exc ) {
    }
}

public void convertor() throws IOException {
    ...
}
davidbuzatto
  • 9,207
  • 1
  • 43
  • 50
  • in this testing class works... ofc... but in my main project not! as i said the method is written in another class in another package.. – Behnam Ezazi Jan 14 '15 at 21:18