1

こんにちは、どなたでもご覧いただけます:

VERTX HTTP CLIENT を使用してデータを取得しようとすると、最後の行で null ポインター例外が発生し、Java HTTP CLIENT を使用したときに同じ URL がデータを提供します。

HttpClientOptions options = new HttpClientOptions().setKeepAlive(false);

options.setLogActivity(true);    
options.setDefaultHost("http://delvmpllbbab10");
options.setDefaultPort(7003);

HttpClient client = vertx.createHttpClient(options);
HttpClientResponse[] clientResponse = {null};

client.request(HttpMethod.GET, "/rcsservices/homePage", response -> {
    System.out.println("Received response with status code " + response.statusCode());

    clientResponse[0] = response;

}).putHeader("content-type", "application/json").end(clientResponse[0].toString());

このコードには問題がありますか...

4

2 に答える 2

0

を取得するのは正しいNPEので、コードではclientResponse変数を で初期化します{ null }。コードがどのように実行されるかをたどると、その理由がわかります。

  1. リクエスト オブジェクトを作成します。client.request(
  2. 応答を受信したときに実行する内容を構成します: `response -> { ... }
  3. リクエストにヘッダーを追加します。putHeader(...)
  4. あなたはリクエストをトリガーしますend(clientResponse[0].toString())

clientResponse[0]これでまだ nullであることがわかります (これは初期化した値です。つまり、次のように呼び出しているのです。

null.toString()

これは無効であり、常にNull Pointer Exception.

于 2016-10-20T14:00:58.713 に答える