Search code examples
androidexceptionandroid-arrayadapter

ArrayAdapter add method casting Illegal state exception


I have a problem adding items to my array adapter.

Here is the code

   public void find_BondedDevices (View view) {

        // Look for bonded devices
        pairedDevices = btAdapter.getBondedDevices();

        if (pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                String deviceBTName = device.getName();
                String deviceBTMajorClass = getBTMajorDeviceClass(device
                        .getBluetoothClass()
                        .getMajorDeviceClass());
                pairedDevicesAdapter.add(deviceBTName + "\n" + deviceBTMajorClass);
            }
        }

If the code is put in the onCreate method, it works!

If i comment the line

pairedDevicesAdapter.add(deviceBTName + "\n" + deviceBTMajorClass);

it works!

   java.lang.IllegalStateException: Could not execute method of the activity
                at android.view.View$1.onClick(View.java:3638)
                at android.view.View.performClick(View.java:4243)
                at android.view.View$PerformClick.run(View.java:17520)

android.app.ActivityThread.main(ActivityThread.java:5299)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:511)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
                at dalvik.system.NativeStart.main(Native Method)
         Caused by: java.lang.reflect.InvocationTargetException
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:511)
                at android.view.View$1.onClick(View.java:3633)
                at android.view.View.performClick(View.java:4243)
                at android.view.View$PerformClick.run(View.java:17520)
                at android.os.Handler.handleCallback(Handler.java:725)
                at android.os.Handler.dispatchMessage(Handler.java:92)
                at android.os.Looper.loop(Looper.java:153)
                at android.app.ActivityThread.main(ActivityThread.java:5299)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:511)
                at 

com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
                at dalvik.system.NativeStart.main(Native Method)
         Caused by: java.lang.NullPointerException
                at com.voice.benz.instaremote.MainActivity.find_BondedDevices(MainActivity.java:95)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:511)
                at android.view.View$1.onClick(View.java:3633)
                at android.view.View.performClick(View.java:4243)
                at android.view.View$PerformClick.run(View.java:17520)
                at android.os.Handler.handleCallback(Handler.java:725)
                at android.os.Handler.dispatchMessage(Handler.java:92)
                at android.os.Looper.loop(Looper.java:153)
                at android.app.ActivityThread.main(ActivityThread.java:5299)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:511)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
                at dalvik.system.NativeStart.main(Native Method)

I've tried almost everything but with no result, i need this code to be in a separate function because in the onCreate method is useless

Here is the complete code of the app: http://pastebin.com/nZYdSkaA

Any ideas? Thanks


Solution

  • You're assigning the adapter to the local variable pairedDevicesAdapter instead of to instance variable.

    It is local to the onCreate() method see this:

     ArrayAdapter<String> pairedDevicesAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
    

    Therefore, your pairedDevicesAdapter the one you're using to call the method 'add' is null (never initialized).

    to fix:

    on the onCreate() just do this instead:

    this.pairedDevicesAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);