2

I am trying to declare variables at the top of the class so I don't have to declare them constantly when using them. I have done so no error comes up until I want to run the program. My code is below:

public class UI extends javax.swing.JFrame {

/**
 * Creates new form UI
 */
public UI() {    
    initComponents();
    this.service.setUsernameAndPassword("9a1d5de6-7c2b-427d-a574-d6fe097c86b9", "ztil6GSHwd34");  
    this.str = txtInput.getText();


}
String str;
PersonalityInsights service = new PersonalityInsights();
 ArrayList<Double> percentagesList = new ArrayList();        
 Profile profile = service.getProfile(str).execute();
 Gson gson = new Gson();  
 Root C = gson.fromJson(profile.toString(), Root.class);
 Root.SubTree t = C.getSubTree(); //Gets subtree from root    
 ArrayList<Children> c = t.getChildren(); //Gets <Children> from Root.getSubTree

Error

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

I believe it is saying the text str cannot be null or empty. How can I resolve this? the str already has text inside it when running it, so unsure why this error is occurring.

Alia Su
  • 45
  • 1
  • 2
  • 9

3 Answers3

3

So you have a UI class and all the variable here are data members... Here problem seems to be str which is null and hence ur service is complaining. Data Member are intialised before constructor,hence ur str is null when profile is being set. I would declare the members in class and initialize them in the constructor or give str a valid intial value at line number 6.

karen
  • 91
  • 6
0

With this line you define a new variable str. It is null by default:

 String str;

It seems, that this.str and str are different variables.

Remove that line and use this.str when calling the method.

Sergej Werfel
  • 1,335
  • 2
  • 14
  • 25
  • Sorry, I have done that and says cannot find variable str. – Alia Su Nov 30 '16 at 14:45
  • the point is, that you code snipped is not a valid java/android application code. The code outside of the UI method: is it executed in an other method? In which class? – Sergej Werfel Nov 30 '16 at 14:45
  • The code has been updated , there a few methods in the same class using it yes. – Alia Su Nov 30 '16 at 15:05
  • ah ok, in such case karen ist totaly right. In java constructor is executed after the fields are set. The str variable is null at the beginning and is set in constructor, but the constructor is never called. – Sergej Werfel Nov 30 '16 at 15:21
0

You should post the whole stack trace to have a better understanding of the problem, specifically at which moment the exception is thrown ? Ah ok I just saw the comment in your code :)

You should not initialise the

Profile profile = service.getProfile(str).execute();

in the class but directly in the constructor after reading str from the component

public UI() {  
    txtInput.setText("TEXT");
    profile = service.getProfile(str).execute();
    Root C = gson.fromJson(profile.toString(), Root.class);
    Root.SubTree t = C.getSubTree(); //Gets subtree from root    
    ArrayList<Children> c = t.getChildren(); //Gets <Children> from   Root.getSubTree
greg
  • 306
  • 1
  • 13