解决JAVA中URL请求的NullPointerException

发布于 2024-10-09 03:39:04 字数 236 浏览 3 评论 0原文

因此,我发出 URL 请求以将数据流获取到 BufferedReader 中。我正在获取的数据的各个字段的值为“null”。我用来读取所有数据的 while 条件是:

while (((inputLine = in.readLine()) != null))

所以当遇到 null 值时,条件会中断,该值实际上不是 EOF,而只是一个字段值。我该如何解决这个问题?

So I am making a URL request to fetch a stream of data into a BufferedReader. The data that I am fetching has values "null" for various fields. The while condition I am using to read all the data is:

while (((inputLine = in.readLine()) != null))

so the condition is breaking in between, when it encounters a null value, which is actually not the EOF but only a field value. How do I resolve this?

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

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

发布评论

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

评论(1

余生共白头 2024-10-16 03:39:04

当您读取一行时,它绝不会null,直到它到达数据末尾。如果该行中没有数据,它将只是一个空字符串。

您没有显示足够的代码来解释为什么会收到 NullPointerException,但您确实需要了解在到达数据末尾之前您不会看到任何“空值”。

要找出出现 NullPointerException 的原因:

  • 查看堆栈跟踪中指示的行 识别
  • 每个取消引用操作
  • 在该行上放置一个断点,或者添加一些日志记录,或者将该行拆分为多个语句,以便每个语句只有一个解引用操作

,这样您就可以准确计算出哪个值为 null,从而引发异常。您需要做什么来修复它取决于您想要做什么以及哪个值为空 - 我们目前没有足够的信息来帮助您在这方面。

When you read a line, it will never be null until it reaches the end of the data. If there's no data in the line, it will just be an empty string instead.

You haven't shown enough code to explain why you're getting a NullPointerException, but you really need to understand that you won't see any "null values" before reaching the end of the data.

To work out why you're getting a NullPointerException:

  • Look at the line indicated in the stack trace
  • Identify every dereferencing operation
  • Either put a breakpoint on that line, or add some logging, or split the line into multiple statements so that each statement only has a single dereferencing operation

That should let you work out exactly which value is null, causing the exception to be thrown. What you need to do to fix it will depend on what you're trying to do and which value is null - we don't have enough information to help you on that front at the moment.

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