这个 NullPointerException 是否指 getFoos 返回 null 或者从 Collection 到 ArrayList 的转换存在问题?

发布于 2024-09-15 17:10:15 字数 513 浏览 4 评论 0原文

为什么此行会导致 NullPointerException

    List<Foo> foos = new ArrayList<Foo>(userDetailsService.getFoos(currentUser));

getFoos 方法仅返回 FooCollection

    public Collection<Foo> getFoos(final String username)

我可以'判断 NullPointerException 是否引用 getFoos 返回 null 或从 CollectionCollection 的转换出现问题代码>ArrayList。

Why does this line cause a NullPointerException:

    List<Foo> foos = new ArrayList<Foo>(userDetailsService.getFoos(currentUser));

The getFoos method simply returns a Collection of Foos:

    public Collection<Foo> getFoos(final String username)

I can't tell if the NullPointerException refers to getFoos returning null or some problem with the cast from a Collection to an ArrayList.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相对绾红妆 2024-09-22 17:10:15

取决于堆栈跟踪。前往第一行。如果它指向 java.util.ArrayList.,则 getFoos() 返回 null。或者,如果它指向问题中的特定行,则 userDetailsS​​ervicenull

Depends on the stacktrace. Head to the first line. If it points to java.util.ArrayList.<init>, then getFoos() has returned null. Or if it points to the particular line in your question, then userDetailsService is null.

恋你朝朝暮暮 2024-09-22 17:10:15

我不知道是否
NullPointerException 指的是 getFoos
返回 null 或出现一些问题
从集合到集合的演员阵容
数组列表。

这就是为什么我们有德墨忒尔法则。如果将连接在一起的两个语句分开,解决问题的运气会更好。

但是,很明显,返回的 null 集合(不是空集合)是导致该问题的原因。毕竟,使用空集合调用 ArrayList 构造函数不会导致空指针异常。

编辑

我的错。 ArrayList 构造函数将尝试复制集合。所以空集合会导致空指针异常。我坚持最初的建议,将这两个语句脱钩。按以下方式编码将有助于更快地识别问题:

Collection<Foo> myFoos = userDetailsService.getFoos(currentUser);
List<Foo> foos = new ArrayList<Foo>(myFoos);

I can't tell if the
NullPointerException refers to getFoos
returning null or some problem with
the cast from a Collection to an
ArrayList.

And that's why we have the Law of Demeter. If you separate the two statements that have been conjoined together, you would have better luck in resolving the issue.

However, it should be apparent that a null collection (not an empty collection) is returned which is responsible for the issue. After all, invoking the ArrayList constructor with a null collection will not result in a null pointer exception.

EDIT:

My bad. The ArrayList constructor will attempt to copy the collection. So a null collection can cause a null pointer exception. I stick to my original recommendation of decoupling the two statements. Coding in the following manner will help identify the issue faster:

Collection<Foo> myFoos = userDetailsService.getFoos(currentUser);
List<Foo> foos = new ArrayList<Foo>(myFoos);
夜清冷一曲。 2024-09-22 17:10:15

失败的类型转换不会给您一个 NullPointerException 异常。

Object o = new Integer(43);
String s = (String) o;  // throws ClassCastException

null 进行类型转换根本不会引发异常。

o = null;
String s = (String) o;  // no exception

因此,getFoo 返回 null 的理论是合理的。但也可能是 userDetailsS​​ervicenull 或 NPE 被抛出到 getFoo 中。

您应该能够通过仔细阅读堆栈跟踪来区分这些情况...您没有将其包含在问题中(tsk,tsk)。

A failed typecast cannot give you a NullPointerException.

Object o = new Integer(43);
String s = (String) o;  // throws ClassCastException

Typecasting a null will not throw an exception at all.

o = null;
String s = (String) o;  // no exception

Therefore the theory that getFoo is returning a null is plausible. But it could also be that userDetailsService is null or that the NPE is thrown in getFoo.

You should be able to distinguish these cases by carefully reading the stacktrace ... which you didn't include in your question (tsk, tsk).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文