Parker
Parker

Reputation: 73

MessageResponse returns null pointer in Watson Conversation

MessageResponse gives a NullPointerException.

ConversationService service = new ConversationService(ConversationService.VERSION_DATE_2016_09_20);
// Credentials of Workspace of Conversation
service.setApiKey("API_KEY");
service.setUsernameAndPassword("USERNAME", "PASSWORD");
MessageRequest newMessage = new MessageRequest.Builder()
  .inputText(request.getQuery())
  .build();

// Workspace ID of Conversation current workspace
String workspaceId = "WORKSPACEID";
service.setSkipAuthentication(true);
MessageResponse response = service.message(workspaceId, newMessage)
  .execute();

Upvotes: 2

Views: 221

Answers (2)

German Attanasio
German Attanasio

Reputation: 23653

The Conversation service doesn't use api_key but username and password

There are two erros in your code snippet: 1. setApiKey() is not required when using Conversation. 1. service.setSkipAuthentication(true); will instruct the SDK to ignore the service credentials, therefore, they are not being sent to the server on each request.

You just need to remove the line service.setSkipAuthentication(true);.

ConversationService service = new ConversationService(ConversationService.VERSION_DATE_2016_09_20);
// Credentials of Workspace of Conversation
// BTW: This are not your Bluemix credentials!
service.setUsernameAndPassword("USERNAME", "PASSWORD");

MessageRequest newMessage = new MessageRequest.Builder()
  .inputText("Hi! this is my first message to Watson")
  .build();

MessageResponse response = service.message("WORKSPACEID", newMessage)
  .execute();
System.out.println(response);

Upvotes: 2

Sayuri Mizuguchi
Sayuri Mizuguchi

Reputation: 5330

According to the IBM Developer (@German): "The Watson services currently use Basic Auth so instead of an api_key you will use username and password. In order to get the credentials, you need to bind the service you want to use (e.g. Question and Answer) to a Bluemix application."

Check the following examples.

Try to use the following code from Java SDK:

ConversationService service = new ConversationService(ConversationService.VERSION_DATE_2017_05_26);
service.setUsernameAndPassword("<username>", "<password>"); //Please make sure if this username and password is the Service Credentials from the Service that you have created to use Conversation

InputData input = new InputData.Builder("Hi").build();
MessageOptions options = new MessageOptions.Builder(workspaceId).input(input).build();

// sync
MessageResponse response = service.message(options).execute();
System.out.println(response);

Other example:

MessageRequest newMessage = new MessageRequest.Builder().inputText(input).context(context).build();

MessageResponse response = service.message(WORKSPACE_ID,newMessage).execute();

context = response.getContext();    
System.out.println(response);
  • You can see the Official example using Watson Conversation.
  • See the Started Guide for using Watson Services (Step 5) here.

Upvotes: 3

Related Questions