eclipse 如何调试此 NullPointer 异常?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1217449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:56:23  来源:igfitidea点击:

How can I debug this NullPointer Exception?

androideclipse

提问by Asa Ayers

I'm trying to write an application for Android and when I launch my new activity, I've set break points and found that it runs through my onCreate without any errors, but after that function returns, the debugger says there is a NullPointer Exception. The problem is, its not in my code, it says its in ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord) line: 2268 and where the source normally would be (if it were my code) I just get "Source not found." and a button that says "Edit Source Lookup Path"

我正在尝试为 Android 编写一个应用程序,当我启动我的新活动时,我设置了断点并发现它通过我的 onCreate 运行而没有任何错误,但是在该函数返回后,调试器说有一个 NullPointer 异常. 问题是,它不在我的代码中,它在 ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord) 行:2268 和源通常在哪里(如果它是我的代码)我只是得到“找不到源”。和一个显示“编辑源查找路径”的按钮

How can I setup eclipse so I can debug this problem? I'm sure I'm doing something wrong in my code, but because the exception originates in code I can't see, I have no idea how to figure out where the problem is.

如何设置 eclipse 以便调试此问题?我确定我的代码有问题,但由于异常源自我看不到的代码,我不知道如何找出问题所在。

I'm using android-sdk-linux_x86-1.5_r3 and my G1 for this project.

我在这个项目中使用 android-sdk-linux_x86-1.5_r3 和我的 G1。

UPDATE: I think my question really has more to do with android development than using eclipse. I'm not sure where to find the source for the the code that is throwing the exception. Maybe there is a way I can debug it without having that file, but I'm not sure what it would be.

更新:我认为我的问题确实与 android 开发有关,而不是使用 eclipse。我不确定在哪里可以找到引发异常的代码的来源。也许有一种方法可以在没有该文件的情况下调试它,但我不确定它会是什么。

采纳答案by Will

Your going in assumption should be the NPE is caused by you, not Android. The NPE may pop at ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord) line: 2268but that means you passed a null pointer to something and it kept on getting passed around until performLaunchActivityactually tried using it and crashed when it found a null pointer.

你的假设应该是 NPE 是由你引起的,而不是 Android。NPE 可能会弹出,ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord) line: 2268但这意味着您将空指针传递给某个东西,并且它一直在传递,直到performLaunchActivity实际尝试使用它并在找到空指针时崩溃。

If we can see the full LogCat, particularly everything in red in the Eclipse LogCat it would help for debugging. I've turned myself inside out looking for an NPE only for someone else to find a very subtle resource initializing line out of place.

如果我们可以看到完整的 LogCat,尤其是 Eclipse LogCat 中红色的所有内容,它将有助于调试。我已经把自己从内到外寻找 NPE,只是为了让其他人找到一个非常微妙的资源初始化行。

回答by mikelikespie

Was wondering this myself, and even though it showed the "caused by" in the logcat, I wanted the debugger to actually break where the NullPointer was thrown (even though it was being caught)

我自己也想知道这个问题,尽管它在 logcat 中显示了“caused by”,但我希望调试器能够真正中断 NullPointer 被抛出的位置(即使它被捕获)

Here's how:

就是这样:

  1. Run -> Add Java Exception Breakpoint
  2. Find NullPointerException (or whatever else you might be debugging (the "Caused by" exception)
  3. Make sure Suspend On Caught Exceptions is checked. Click OK.
  4. You should see a new breakpoint in your view
  1. 运行 -> 添加 Java 异常断点
  2. 查找 NullPointerException(或其他任何您可能正在调试的异常(“Caused by”异常)
  3. 确保在捕获异常时挂起被选中。单击确定。
  4. 您应该在视图中看到一个新的断点

Try running it now, and the debugger should break where it actually occurs.

现在尝试运行它,调试器应该在它实际发生的地方中断。

回答by duffymo

a button that says "Edit Source Lookup Path"

一个显示“编辑源查找路径”的按钮

Hit that button and tell Eclipse where to find the source code for that method. That should help. At least you'll be able to step into it in the debugger and read something more sensible.

点击该按钮并告诉 Eclipse 在哪里可以找到该方法的源代码。那应该有帮助。至少您将能够在调试器中进入它并阅读更明智的内容。

回答by CommonsWare

Could you show the full call stack?

你能显示完整的调用堆栈吗?

Usually, at least with Android, an exception triggers two separate traces. The first one is useless. The second one, noted by "Caused by exception", is the one you want.

通常,至少对于 Android,异常会触发两个单独的跟踪。第一个没用。由“异常引起”指出的第二个是您想要的。

And, even if neither stack trace has your code, we might recognize something by seeing the whole thing.

而且,即使堆栈跟踪都没有您的代码,我们也可能通过查看整个内容来识别某些内容。

回答by atk

I haven't worked with Andriod, but Eclipse should be able to show you the call stack. Look at the point where the program exits your code and enters someone else's, and make sure all your variables correct (non-null, contain the "right" data, etc).

我没有使用过 Andriod,但 Eclipse 应该能够向您展示调用堆栈。查看程序退出您的代码并输入其他人的代码的位置,并确保您的所有变量都正确(非空,包含“正确”数据等)。

回答by Rob Lourens

Even if it's happening within the Android code, you should still be able to narrow down at what point it's happening and which of your variables it doesn't like. Yeah, vague answer, not much else to say. Or download the source and tell Eclipse where it is.

即使它发生在 Android 代码中,您仍然应该能够缩小它发生的时间以及它不喜欢的变量。是的,含糊的回答,没什么好说的。或者下载源代码并告诉 Eclipse 它在哪里。

回答by Marvin

If you want to see the location where the exception happens without downloading the Android source code, there is a nice plugin for eclipse called GrepCode. It searches and displays the source code of popular java projects online.

如果您想在不下载 Android 源代码的情况下查看发生异常的位置,可以使用一个很好的 Eclipse 插件 GrepCode。它在线搜索并显示流行的 Java 项目的源代码。