- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Twitter4j 来获取给定用户的关注者。在某些时候我总是达到速率限制。我很高兴程序 hibernate 15 分钟来克服这个问题。然而,当它醒来时,我收到一个错误。
请参阅以下代码:
protected IDs fetchAndStoreFollowers(TwitterUser user) {
Twitter twitter = this.getTwitterApi();
long cursor = -1;
IDs ids = null;
do {
try {
ids = twitter.getFollowersIDs(user.getId(), cursor);
System.out.println("Got a followers batch");
cursor = ids.getNextCursor();
this.storeFollowers(user, ids);
System.out.println("Saved!");
} catch (TwitterException e) {
System.out.println("Oh no! Rate limit exceeded... going to sleep.");
handleTwitterException(e);
System.out.println("Waking up!");
}
} while (ids.hasNext());
return ids;
}
从 sleep 中唤醒后,程序在此行抛出 NullPointerException
:
} while (ids.hasNext());
有人能看出原因吗?
最佳答案
遇到的错误的原因是可重现的并且非常合乎逻辑。
首先,将 ids
初始化为 null
。
如果发生 RateLimitException
(TwitterException),则不会为以下行中的变量 ids
设置实际值:
ids = twitter.getFollowersIDs(user.getId(), cursor);
然后执行catch
block 中的代码 - 而ids
仍然指向null
。处理完毕后(您会在控制台上看到输出...),该行:
while (ids.hasNext());
产生NullPointerException
。
按如下方式更改 while 条件:
while (ids == null || (id!=null && ids.hasNext()));
请注意,如果出现错误情况,cursor
中的值可能尚未或必须相应更改。
希望这有帮助。
关于java - 为什么此代码会在 sleep 时导致 NPE 以避免 Twitter4J 中的速率限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42179999/
目前,我的经验是,一段利用Google Drive API的代码在没有引入ProGuard的情况下运行得很好。。然而,在引入ProGuard之后,我得到了以下运行时错误。。请注意,崩溃发生在我的代码(
今天早上我遇到了一个非常奇怪的 NPE,并将其简化为一个简单的示例。这是 JVM 错误还是正确的行为? public class Test1 { class Item { In
在 crashlytics 中报告的 NPE 仅适用于 Android O 及更高版本。我只是在 onCreate 方法中 startForegroundService 和服务 startForgro
运行以下内容: public class NPESample { String value; public static void main(String[] args) { NPES
我有一个非常简单的 OpenAPI/Swagger 配置 (openapi.yaml): swagger: '2.0' info: title: My Service version: 1.0
我正在使用 com.sun.media.imageioimpl.plugins.tiff.TIFFPackBitsCompressor 尝试对使用 PackBits 的 tiff 字节数组进行编码。我
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) findViewById returns null
我有一个处理大量数据的批处理作业。该作业基本上从源数据库获取数据并进行 Web 服务调用,然后将数据写入目标数据库。今天我遇到了“NPE”,我在其中检查 XML 是否有“成功”响应。我检查其中一个节点
这个问题已经有答案了: Why does a ternary conditional expression returning null and assigned to a reference typ
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 4 年前。 我正在尝试从 bundle 内的
我试图在我的应用程序中每隔一分钟执行一项任务,我使用以下内容来实现相同的目的。代码位于onCreate方法内部: mTimer.scheduleAtFixedRate(new TimerT
所以我正在使用集合设计模式并使用数组列表。每当我尝试向数组列表中添加某些内容时,我都会收到 NPE。我可能错误地实现了该集合,因此出现了 NPE。 我不想复制我的整个代码,因为它太长了,所以我试图给你
我想将自定义对话框的一个区域设置为所选图像。如果我设置整个应用程序的背景图像,下面的代码可以进行一些重新安排。由于某种原因,当我移动它来设置自定义对话框的区域时,我收到以下错误: 错误: 11-03
所以我觉得自己像个白痴,但我正在尝试实现碰撞检测,并且我需要检查玩家旁边是否有方 block 。当我去检查时,我首先会看看我要找的瓷砖是否真的在那里。如果是,我将继续选择该图 block 并从中创建一
在 OS X 10.11 上,我们的应用程序用户会遇到一些没有堆栈跟踪的 NPE(请参阅 this stackoverflow-question)。我现在想自己创建一个来调试这种情况下的错误处理。 如
我有一个非常简单的 OpenAPI/Swagger 配置 (openapi.yaml): swagger: '2.0' info: title: My Service version: 1.0
我正在尝试为名为 getBestSellers() 的方法编写单元测试。 这里是: package bookstore.scraper.book.scrapingtypeservice; import
为什么我在以下作业中获得 NPE: mPyramid[row][column] = temp; 这是我的代码: Block temp; Block[][] pyramid =
为什么这段代码会导致NPE? Findbugs 给了我提示,这种情况可能会发生,而且有时确实会发生:-) 有什么想法吗? public Integer whyAnNPE() { return
我正在尝试运行客户端并访问字段来设置/获取值。当脚本启动时,我创建一个加载了 URLClassLoader 的客户端类的新实例,并将其分配给 gameApplet。 现在,下一段代码可以正常工作(访问
我是一名优秀的程序员,十分优秀!