检查包中是否存在 Null 文本,从而产生 NullPointer
我尝试了很多不同的方法,但我似乎无法摆脱这个空指针。我有一个应用程序,用户必须在三个 EditText 中输入文本,然后按提交按钮。我想处理这样一个事实:如果用户没有输入任何数据,只是按提交,那么当调用类并完成所有计算时,我可以显示一个祝酒词,然后使用意图返回到主函数。 但是,它不会工作, if 永远不会被查看......
这是代码
Bundle b = getIntent().getExtras();//the bundle is retrieved from the intent
int id = b.getInt("ID");//retreive the ID to know which function to call
int nullCounter=0;
if (b.getString("vf").trim().equals(""))
nullCounter++;
else if (!b.getString("vf").trim().equals(""))//checks each possibility that could have been included in the bundle
vf = Double.parseDouble(b.getString("vf"));//if the value exists, extract it and convert it to an integer
if (b.getString("t").trim().equals(""))
nullCounter++;
else if (!b.getString("t").trim().equals(""))
t = Double.parseDouble(b.getString("t"));
if (b.getString("d").trim().equals(""))
nullCounter++;
else if (!b.getString("d").trim().equals(""))
d = Double.parseDouble(b.getString("d"));
if (b.getString("a").trim().equals(""))
nullCounter++;
else if (!b.getString("a").trim().equals(""))
a = Double.parseDouble(b.getString("a"));
if (b.getString("v1").trim().equals(""))
nullCounter++;
else if (!b.getString("v1").trim().equals(""))
v1 = Double.parseDouble(b.getString("v1"));
if(nullCounter >2)
{
Intent i = new Intent(Results.this,PhysicsCalculatorActivity.class);
Results.this.startActivity(i);
}
I've tried many different things, but I cant seem to get rid of this nullpointer. I have an app, in which the user has to input text into three EditTexts then press a submit button. I want to deal with the fact that if the user inputs no data and just presses submit, when the class is called where all the calculations are done, I can show a toast, and then use an intent to go back to the main.
However, it wont work, the if never gets looked at...
Here's the code
Bundle b = getIntent().getExtras();//the bundle is retrieved from the intent
int id = b.getInt("ID");//retreive the ID to know which function to call
int nullCounter=0;
if (b.getString("vf").trim().equals(""))
nullCounter++;
else if (!b.getString("vf").trim().equals(""))//checks each possibility that could have been included in the bundle
vf = Double.parseDouble(b.getString("vf"));//if the value exists, extract it and convert it to an integer
if (b.getString("t").trim().equals(""))
nullCounter++;
else if (!b.getString("t").trim().equals(""))
t = Double.parseDouble(b.getString("t"));
if (b.getString("d").trim().equals(""))
nullCounter++;
else if (!b.getString("d").trim().equals(""))
d = Double.parseDouble(b.getString("d"));
if (b.getString("a").trim().equals(""))
nullCounter++;
else if (!b.getString("a").trim().equals(""))
a = Double.parseDouble(b.getString("a"));
if (b.getString("v1").trim().equals(""))
nullCounter++;
else if (!b.getString("v1").trim().equals(""))
v1 = Double.parseDouble(b.getString("v1"));
if(nullCounter >2)
{
Intent i = new Intent(Results.this,PhysicsCalculatorActivity.class);
Results.this.startActivity(i);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许你的 b.getString(...) 返回 null,而不是“”。
在调用 trim() 之前检查 b.getString(...) 是否不为 null。
Maybe your b.getString(...) returns null, not a "".
Check if b.getString(...) is not null, before call trim().