检查包中是否存在 Null 文本,从而产生 NullPointer

发布于 2024-12-23 11:27:12 字数 1506 浏览 6 评论 0原文

我尝试了很多不同的方法,但我似乎无法摆脱这个空指针。我有一个应用程序,用户必须在三个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

川水往事 2024-12-30 11:27:12

也许你的 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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文