Java检查字符串是否为空或空

时间:2014-12-30 05:57:54

标签: java string nullpointerexception

我有一些字符串,我在使用图表上的值之前执行了一些检查。这是我的代码:

for (int j = 0; j < shareByTypeList.size(); j++) {
            if (j == 0) {
                facebookShare = shareByTypeList.get(j).getTotalShare();
            } else if (j == 1) {
                googleplusShare = shareByTypeList.get(j).getTotalShare();
            } else {
                twitterShare = shareByTypeList.get(j).getTotalShare();
            }
        }

        if (facebookShare != null && !facebookShare.isEmpty()
                && twitterShare != null && !twitterShare.isEmpty()
                && googleplusShare != null && !googleplusShare.isEmpty()) {
            txtDisplayFacebook.setText(facebookShare);
            txtDisplayTwitter.setText(twitterShare);
            txtDisplayGooglePlus.setText(googleplusShare);
            totalShare = Integer.parseInt(facebookShare)
                    + Integer.parseInt(googleplusShare)
                    + Integer.parseInt(twitterShare);
            txtDisplayShareAmt.setText(String.valueOf(totalShare));
        }

我正在检查字符串本身是null还是空,然后再将它们解析为Integer。但是,我仍然在那里的if语句中得到NullPointerException。当我尝试打印出facebookShare时,它在控制台上显示为空。有任何想法吗?

提前致谢。

堆栈跟踪:

12-30 13:53:39.787: E/AndroidRuntime(1787): java.lang.NullPointerException
12-30 13:53:39.787: E/AndroidRuntime(1787):     at nyp.edu.eneighbourhood.EventDetailChart.onCreateView(EventDetailChart.java:191)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:482)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:555)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:495)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at nyp.edu.eneighbourhood.EventDetailMain$2.onTabSelected(EventDetailMain.java:42)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:512)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:893)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at com.android.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:502)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at android.view.View.performClick(View.java:3620)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at android.view.View$PerformClick.run(View.java:14292)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at android.os.Handler.handleCallback(Handler.java:605)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at android.os.Looper.loop(Looper.java:137)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at android.app.ActivityThread.main(ActivityThread.java:4512)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at java.lang.reflect.Method.invokeNative(Native Method)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at java.lang.reflect.Method.invoke(Method.java:511)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
12-30 13:53:39.787: E/AndroidRuntime(1787):     at dalvik.system.NativeStart.main(Native Method)
12-30 13:53:49.334: I/Process(1787): Sending signal. PID: 1787 SIG: 9

1 个答案:

答案 0 :(得分:0)

我通过分离3个变量来执行检查然后将值设置为textview来解决它:

if (facebookShare != null && !facebookShare.isEmpty()) {
            txtDisplayFacebook.setText(facebookShare);
        } else if (twitterShare != null && !twitterShare.isEmpty()) {
            txtDisplayTwitter.setText(twitterShare);
        } else if (googleplusShare != null && !googleplusShare.isEmpty()) {
            txtDisplayGooglePlus.setText(googleplusShare);
        }

        if (!txtDisplayFacebook.getText().equals("0")
                && !txtDisplayTwitter.getText().equals("0")
                && !txtDisplayGooglePlus.getText().equals("0")) {
            totalShare = Integer.parseInt(facebookShare)
                    + Integer.parseInt(googleplusShare)
                    + Integer.parseInt(twitterShare);
            txtDisplayShareAmt.setText(String.valueOf(totalShare));
        }