如何解决 - java.lang.IllegalArgumentException: text cannot be null or empty
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40891082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve - java.lang.IllegalArgumentException: text cannot be null or empty
提问by Alia Su
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.
我相信这是说文本str
不能为空或为空。我该如何解决这个问题?在str
运行它的时候,所以不能确定为什么这个错误发生已经有里面的文字。
回答by karen
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.
所以你有一个 UI 类,这里的所有变量都是数据成员......这里的问题似乎是 str 为空,因此你的服务正在抱怨。数据成员在构造函数之前初始化,因此在设置配置文件时 ur str 为空。我会在类中声明成员并在构造函数中初始化它们,或者在第 6 行给 str 一个有效的初始值。
回答by Sergej Werfel
With this line you define a new variable str. It is null by default:
通过这一行,您可以定义一个新变量 str。默认为空:
String str;
It seems, that this.str
and str
are different variables.
看来,那this.str
和str
是不同的变量。
Remove that line and use this.str
when calling the method.
删除该行并this.str
在调用方法时使用。
回答by greg
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
在类中,但在从组件读取 str 后直接在构造函数中
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