dafyddgj dafyddgj - 9 days ago 5
Java Question

Getting error - Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: text cannot be null or empty

Trying to use Text Area to send String to IBM Watson.

Could do this by hard coding String. I want to be able to input my own text in JTextArea that can be sent to IBM Watson.

PersonalityInsights service = new PersonalityInsights();
service.setUsernameAndPassword("Username", "password"); // I have correct credentials


String text = textArea.getText();


JButton btnAnalyse = new JButton("Analyse");
btnAnalyse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Profile profile = service.getProfile(text).execute();
System.out.println(profile);


}
});
btnAnalyse.setBounds(42, 219, 115, 29);
frame.getContentPane().add(btnAnalyse);


}
}


This is the error I get;

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: text cannot be null or empty
at com.ibm.watson.developer_cloud.util.Validator.notEmpty(Validator.java:85)
at com.ibm.watson.developer_cloud.personality_insights.v2.PersonalityInsights.getProfile(PersonalityInsights.java:115)
at com.ibm.watson.developer_cloud.personality_insights.v2.menu$3.actionPerformed(menu.java:96)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)

It goes on for a while.

Here is the whole code if it helps.

package com.ibm.watson.developer_cloud.personality_insights.v2;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import com.ibm.watson.developer_cloud.personality_insights.v2.model.Profile;

public class menu {

private JFrame frame;
private JTextArea textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
menu window = new menu();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public menu() {
initialize();
}

/**
* Initialise the contents of the frame.
*/
public void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 555, 320);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

JButton btnExit = new JButton("Exit");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnExit.setBounds(380, 219, 115, 29);
frame.getContentPane().add(btnExit);

JLabel lblString = new JLabel("Input Text");
lblString.setBounds(218, 21, 85, 20);
frame.getContentPane().add(lblString);

textArea = new JTextArea();
textArea.setBounds(15, 57, 503, 150);
frame.getContentPane().add(textArea);
textArea.setColumns(10);


//Connect to IBM Watson by pressing the analyse button

PersonalityInsights service = new PersonalityInsights();
service.setUsernameAndPassword("Username", "password"); // I have correc


String text = textArea.getText();


JButton btnAnalyse = new JButton("Analyse");
btnAnalyse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Profile profile = service.getProfile(text).execute();
System.out.println(profile);


}
});
btnAnalyse.setBounds(42, 219, 115, 29);
frame.getContentPane().add(btnAnalyse);


}
}


Any help would be great, Thanks.

Answer

Move String text = textArea.getText(); inside the actionPerformed method like this:

public void actionPerformed(ActionEvent e) {
            String text = textArea.getText();
            Profile profile = service.getProfile(text).execute();  
            System.out.println(profile);


                }