2012-04-02 13 views
0

呼び出し可能なクラスを作成しようとすると、上記のエラーが発生しているようです。私は理由を捜しましたが、何かを見つけることができませんでした。 NetBeansは私に物事を抽象化させるためのいくつかの選択肢を提供しますが、私はこれを初めて知っています。なぜ何が起きているのかを知りたいのです。誰もこれに光を当てることができますか?呼び出し可能なクラスでエラーが発生しました:doPingは抽象メソッドではなく、抽象メソッド呼び出し()をオーバーライドしませんか?

public class doPing implements Callable<String>{ 

    public String call(String IPtoPing) throws Exception{ 

     String pingOutput = null; 

     //gets IP address and places into new IP object 
     InetAddress IPAddress = InetAddress.getByName(IPtoPing); 
     //finds if IP is reachable or not. a timeout timer of 3000 milliseconds is set. 
     //Results can vary depending on permissions so cmd method of doing this has also been added as backup 
     boolean reachable = IPAddress.isReachable(1400); 

     if (reachable){ 
       pingOutput = IPtoPing + " is reachable.\n"; 
     }else{ 
      //runs ping command once on the IP address in CMD 
      Process ping = Runtime.getRuntime().exec("ping " + IPtoPing + " -n 1 -w 300"); 
      //reads input from command line 
      BufferedReader in = new BufferedReader(new InputStreamReader(ping.getInputStream())); 
      String line; 
      int lineCount = 0; 
      while ((line = in.readLine()) != null) { 
       //increase line count to find part of command prompt output that we want 
       lineCount++; 
       //when line count is 3 print result 
       if (lineCount == 3){ 
        pingOutput = "Ping to " + IPtoPing + ": " + line + "\n"; 
       } 
      } 
     } 
     return pingOutput; 
    } 
} 

答えて

2

callメソッドは、引数を持っている:それは、Callableインタフェースのcallメソッドをオーバーライドしていない - それは次のようになります。あなたは、Javaを使用している場合

public String call() throws Exception{ //not public String call(String IPtoPing) 

} 

6+、 Overrideアノテーションを使用することは良い方法です(この場合は、すでにコンパイルエラーが発生しています)。

@Override 
public String call() throws Exception{ 
} 
+0

ありがとうございます:) – DMo

1

あなたの 'ドーピング' クラスがimplements Callable<String>として定義されています。これは、が引数を取らないcall()メソッドを実装する必要があることを意味します。あなたはdoPingCallableになりたい場合は、String IPtoPing引数を削除する必要があります

public interface Callable<V> { 
    V call() throws Exception; 
} 

public class doPing implements Callable<String> { 
    // you need to define this method with no arguments to satisfy Callable 
    public String call() throws Exception { 
     ... 
    } 
    // this method does not satisfy Callable because of the IPtoPing argument 
    public String call(String IPtoPing) throws Exception { 
     ... 
    } 
} 
+0

助けてくれてありがとう。 – DMo

1

Callableインタフェースはあなたがcall()メソッドを持っている必要があり、ここでCallableの定義があります。ただし、あなたの方法は
call(String ipToPing)です。

正しいipを設定するには、setIpToPing(String ip)メソッドを追加することを検討してください。

I.e.あなたのコードで

doPing myCallable = new doPing();//Note doPing should be called DoPing to keep in the java naming standards. 
myCallable.setIpToString(ip);//Simple setter which stores ip in myCallable 
myCallable.call(); 
+0

ありがとう。とても有難い。 IPtoPingは別のクラスのメインメソッド内の変数なので、この変数をcall()に渡すにはどうすればいいですか? – DMo

+0

@ user1286779問題ありません。私は例を追加しました。返信のために – Jim

関連する問題