解决JAVA中URL请求的NullPointerException
因此,我发出 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您读取一行时,它绝不会为
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
: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.