-2

I am getting a null pointer exception from my application.

I don't know, which class is firing this exception. This crash occurred in run time for one of my customer. He keeps on reporting this issue and finally I integrate Fabrics- Crash lytics. Then I got this error log. But it does not mention my package name or class name.

I added a null check before all Object.toString() in my application. But this issue is still existing.I hope this issue is coming from some library.

I am using 10 ArrayAdapter in this entire project.

This issue reported in Samsung - SMT5550 - OS -6.0.1

Please see the error log

Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
       at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:401)
       at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
       at android.widget.AbsListView.obtainView(AbsListView.java:2937)
       at android.widget.ListView.measureHeightOfChildren(ListView.java:1305)
       at android.widget.ListView.onMeasure(ListView.java:1212)
       at android.view.View.measure(View.java:20214)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
       at android.view.View.measure(View.java:20214)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333)
       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
       at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
       at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
       at android.view.View.measure(View.java:20214)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
       at android.view.View.measure(View.java:20214)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
       at android.view.View.measure(View.java:20214)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
       at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3083)
       at android.view.View.measure(View.java:20214)
       at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2683)
       at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1611)
       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1928)
       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1524)
       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7520)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
       at android.view.Choreographer.doCallbacks(Choreographer.java:686)
       at android.view.Choreographer.doFrame(Choreographer.java:622)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:7325)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Anyone has encountered similar issue, please update me. Anyone has any solution please update me.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Nithinjith
  • 1,775
  • 18
  • 40

3 Answers3

2

You're using an ArrayAdapter in your code, ArrayAdapter goes over a given array and calls toString on each object. Make sure an array passed to an ArrayAdapter doesn't contain any null elements.

marmor
  • 27,641
  • 11
  • 107
  • 150
1

ArrayAdapter.java:401, is what is throwing this error. Check the values added in the object passed to this method. Probably one of those is null. android.os.Looper.loop(Looper.java:148)

Gautam
  • 564
  • 2
  • 12
  • 2
    *ArrayAdapter.java:401, is what is throwing this error.* and it is not his code ... *Check the values added in the object passed to this method.* which method? ArrayAdapter.java has changed over the time ... it is hard to find out which version of the file is used in his client android implementation ... *Probably one of those is null* thank you captain obvious – Selvin Sep 19 '16 at 09:20
  • Read the comment. check values passed to this method. The method is mentioned. android.os.Looper.loop(Looper.java:148). No need to act oversmart, brother. – Gautam Sep 19 '16 at 11:05
  • *The method is mentioned.* where? `android.os.Looper.loop(Looper.java:148)` ? is internal android message loop he is not calling it by himself ... *No need to act oversmart* ... oversmart? it's only look oversmart when "answer" makes no sens ... *brother* ... i'm not your brother – Selvin Sep 19 '16 at 11:06
  • android.os.Looper.loop(Looper.java:148), here is the method that loops over the values. His stacktrace ends at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230). The person asking the question knows where exactly in his code, is he passing values to the Looper (implicitly). Is it still vague? – Gautam Sep 19 '16 at 11:11
  • *android.os.Looper.loop(Looper.java:148), here is the method that loops over the values.* ... seems like a lack of basics knowladge(how to read stacktrace) ... here is a main problem `at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:401) at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)` ... obviously because one of the array element passed to the adapter's constructor is null ... **`android.os.Looper.loop` is called internally by system to draw frame** – Selvin Sep 19 '16 at 11:14
  • Agreed android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:401) is where exception happened. But are you saying Looper.loop was invoked by ArrayAdapter.createViewFromResource, then I am stumped. As per how I read that stack trace, Looper.loop invoked methods that eventually invoked ArrayAdapter.createViewFromResource. So, look at what is being fed to Looper.loop. What does that array contain? – Gautam Sep 19 '16 at 12:12
0

Finally, I found the issue in my codebase.

I already have a BLE module in my code. The BLE module contains a BLE scan section. The scan section will scan the nearest available BLE's and get the device object. The object contains device name and device address.

In my implementation, I added an alert dialog with single choice item and pass all the available BLE as a string array list.

But the problem occurred in run time as, once the user disconnects one BLE and try to connect to another one randomly, the device name will return as null. So I am passing a null object to the alert dialog.
This is an issue from BLE.

Please refer: android BluetoothDevice.getName() return null

This is the reason while I am getting a null pointer exception as

Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference

Now I added a null check in the scan BLE object receiving section. Now this issue is fixed.

@Override public void onScanResult(int callbackType, ScanResult result) { super.onScanResult(callbackType, result);

        if (result != null && result.getDevice() != null && result.getDevice().getAddress() != null) {
            if (null != mOnDeviceScanListener) {
                BleDeviceData data = new BleDeviceData();
                data.setDeviceName(result.getDevice().getName() != null ? result.getDevice().getName() : "Unknown");
                data.setDeviceAddress(result.getDevice().getAddress());
                boolean isSame = false;
                for (BleDeviceData deviceData : mDeviceList) {
                    if (deviceData.getDeviceAddress().equalsIgnoreCase(data.getDeviceAddress())) {
                        // Neglect the ID
                        isSame = true;
                    }
                }
                if (!isSame) {
                    mDeviceList.add(data);
                }
            }
        }
    }

The issue is getting inside the dialog implementation. ie got confused.

@Marmor, thank you for your comment. This help me to solve the issue.

Community
  • 1
  • 1
Nithinjith
  • 1,775
  • 18
  • 40