简体   繁体   中英

Android - NullPointerException on accessing RadioButton array

Ok, so I think I might have some insight on how to solve this one, but I'm not exactly sure. I have a piece of code first declares a RadioButton[], then a method that sets it to a specific thing then another method that checks which RadioButton is checked.

RadioButton[] rB = null;

String geUrl(RadioButton[] rBp) {
    for(int radind = 0; radind < rBp.length; radind++) {
        Log.d("APP", "rBp value is " + rBp.toString());
        Log.d("APP", "In for loop, index = " + radind + ", length = " + rBp.length);
        if (rBp[radind].isChecked()) {
            String url = rB[radind].getText().toString();
            Log.d("APP", "Detected url, " + url);
            return url;
        }
    }
    return "err";
}

public void refresh() {
    if (rB != null) {
        rG.removeAllViews();
    }
for(int index = 0; index < tempWikiDB.size() ; index++) {
    rB = new RadioButton[tempWikiDB.size()];
    rB[index]  = new RadioButton(this);
    rG.addView(rB[index]);
    rB[index].setText(tempWikiDB.toArray()[index].toString());
}

}

The geUrl method is supplied with a sufficient argument. But, on run, I get a null pointer exception of the line if (rBp[radind].isChecked()). I think this is because RadioButton[] rB = null; isn't how I should declare the variable, but I don't know how otherwise.

Here's the logcat:

11-23 05:48:27.911: E/AndroidRuntime(1189): FATAL EXCEPTION: main
11-23 05:48:27.911: E/AndroidRuntime(1189): java.lang.NullPointerException
11-23 05:48:27.911: E/AndroidRuntime(1189):     at com.spng453.wikicenter.SearchWiki.geUrl(SearchWiki.java:37)
11-23 05:48:27.911: E/AndroidRuntime(1189):     at com.spng453.wikicenter.SearchWiki$1.onClick(SearchWiki.java:74)
11-23 05:48:27.911: E/AndroidRuntime(1189):     at android.view.View.performClick(View.java:4202)
11-23 05:48:27.911: E/AndroidRuntime(1189):     at android.view.View$PerformClick.run(View.java:17340)
11-23 05:48:27.911: E/AndroidRuntime(1189):     at android.os.Handler.handleCallback(Handler.java:725)
11-23 05:48:27.911: E/AndroidRuntime(1189):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-23 05:48:27.911: E/AndroidRuntime(1189):     at android.os.Looper.loop(Looper.java:137)
11-23 05:48:27.911: E/AndroidRuntime(1189):     at android.app.ActivityThread.main(ActivityThread.java:5039)
11-23 05:48:27.911: E/AndroidRuntime(1189):     at java.lang.reflect.Method.invokeNative(Native Method)
11-23 05:48:27.911: E/AndroidRuntime(1189):     at java.lang.reflect.Method.invoke(Method.java:511)
11-23 05:48:27.911: E/AndroidRuntime(1189):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-23 05:48:27.911: E/AndroidRuntime(1189):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-23 05:48:27.911: E/AndroidRuntime(1189):     at dalvik.system.NativeStart.main(Native Method)

在每次循环迭代中初始化rB数组时,请将此行从for循环中删除

rB = new RadioButton[tempWikiDB.size()];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM