Android NullPointerException - Debugging a specific line

别说谁变了你拦得住时间么 提交于 2021-01-28 08:48:24

问题


If I have a line like this:

var.getSomething().getSomethingElse().setNewValue(stuff.getValue().getWhatever());

If that line creates a NullPointerException, is there any way of finding out which method is returning a null value?

I believe I was able to split the line at every dot and get the exception showing which line was failing. But I can't get that to work anymore (maybe I remember incorrectly).

Is the only good debugging possibility to write it like this?

a = var.getSomething();
b = a.getSomehingElse();
c = stuff.getValue();
d = c.getWhatever();
b.setNewValue(d);

With this I should be able to easily see where the exception happens. But it feels inefficient and ugly to write this way.

I use Android Studio. Used Eclipse before but moved to Android Studio some time ago.


回答1:


You might want to put every part into "Watches":

enter image description here

But I'm pretty sure that both Eclipse and Android Studio would let you inspect the content by just a selection of the part you' re interested in (if you are in debug mode)




回答2:


The best I can advice for you is to use @Nullable and @NonNull annotations for all methods with return values. It would not help you to get line where null pointer is but would help to prevent such situations in future.

So if method may return null and you have it in call sequence you will get warning from Android Studio about this. In this case it is better to break sequence and check for null.

For example:

private static class Seq {

    private final Random rand = new Random();

    @NonNull
    public Seq nonNull() {
        return new Seq();
    }

    @Nullable
    public Seq nullable() {
        return rand.nextInt() % 100 > 50 ? new Seq() : null;
    }

}

If you write new Seq().nonNull().nonNull().nullable().nonNull(); you will get warning from IDE:

Method invocation `new Seq().nonNull().nonNull().nullable().nonNull()` may produce 'java.lang.NullPointerException'

The best solution in this case is to change code like so:

    Seq seq = new Seq().nonNull().nonNull().nullable();
    if (seq != null) {
        seq.nonNull();
    }

Don't forget to add it into Gradle build script

compile 'com.android.support:support-annotations:22.+'



回答3:


I am not positive on the way you are doing it. This makes your code tightly coupled and not unit testable.

var.getSomething().getSomethingElse().setNewValue(stuff.getValue().getWhatever());

Instead do something like

var.getSomething();

that get something internally does whatever you are doing as a part of

getSomethingElse().setNewValue(stuff.getValue().getWhatever())

In the same way getSomethingElse() should perform whatever you are doing as a part of

setNewValue(stuff.getValue().getWhatever())


来源:https://stackoverflow.com/questions/31470034/android-nullpointerexception-debugging-a-specific-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!