- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我目前有以下代码
while (!visibleTiles.isEmpty())) {
tile = visibleTiles.keySet().iterator().next();
if (tile != null){
bitmap = visibleTiles.remove(tile);
if(bitmap != null && !containsKey(tile)){ //safe to recycle if tile cache is not actively holding it
bitmap.recycle();
}
}
}
但是,我在线上遇到了 NoSuchElementException 崩溃
tile = visibleTiles.keySet().iterator().next();
使用 isEmpty() 方法和调用 hasNext() 调用有很大区别吗?我知道 HashMap 没有 hasNext() 调用,所以我执行了以下操作:
while (visibleTiles.keySet().iterator().hasNext()) {
tile = visibleTiles.keySet().iterator().next();
if (tile != null){
bitmap = visibleTiles.remove(tile);
if(bitmap != null && !containsKey(tile)){ //safe to recycle if tile cache is not actively holding it
bitmap.recycle();
}
}
}
显然,我知道我应该只运行该应用程序并查看它是否崩溃,但问题是很难重现该问题。
谢谢!
最佳答案
visibleTiles.isEmpty()
只是检查 map 是否为空(或)是否有任何元素。
ile = visibleTiles.keySet().iterator().next();
从迭代器中检索下一个元素。
在对迭代器执行 next()
之前,您需要执行 hasNext()
检查。
hasNext()
javadoc说
Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)
因此,如果没有可用元素并在迭代器上调用 next()
将返回 NoSuchElementException
。
最重要的是,我想你真的不想做空检查
while (!visibleTiles.isEmpty())) {
....
}
关于java - 迭代器 isEmpty() 调用导致 noSuchElement 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13053195/
我创建了一个读取这样的文件集的方法(//...是注释,忽略它们): 5 // n jobs 2 // n tools 1 4 5 6 2 1 5 4 2 3 矩阵代表每项工作使用的工具,但在这里并不重
我正在编写读取用户输入的代码,因此我编写了一个程序来执行此操作,读取并返回字符串。当从另一个过程调用它两次时,返回一个 NoSuchElementException 错误,我不知道为什么。 输入阅读器
I have declared two strings and reading the input using Scanner(System.in). 在此之后,当我关闭扫描仪并再次使用扫描仪读取另一
有人可以帮忙解释一下为什么会抛出 NoSuchElement 异常吗?它似乎发生在最后一行 scan.nextInt(); 我正在尝试从文件中读入名称并将它们排序到数组中,然后在打印后从用户那里读入一
对于一个类,我必须创建一个程序,该程序有一个菜单,用户可以根据自己的选择从菜单中选择运行某些方法。我遇到的问题是在我调用我的方法之后,程序在第 30 行抛出 NoSuchElement 异常(当我将代
我们正在一个大项目中使用 GWT。在某些情况下,此代码可以生成 NoSuchElementException : for (Object o: myList) { } 内部没有对ArrayList进行
我已经分享了下面的代码,请让我知道更正 import org.openqa.selenium.By; import org.openqa.selenium.ie.InternetExplorerDri
我有一个 JSP 页面,用户可以在其中选择多个复选框(最多 10 个)。有时,如果选中多个复选框,则会引发 NoSuchElement 异常。 Servlet 代码: Iterator iterat
这个问题已经被一遍又一遍地问了——尽管尝试了所有的技巧,但我似乎仍然无法弄清楚问题出在哪里。 我尝试将 implicitly_wait 增加到 30(甚至增加到 100)- 但它没有用。 用例 -:我
这个问题已经有答案了: Selenium "selenium.common.exceptions.NoSuchElementException" when using Chrome (1 个回答) 已
我正在尝试自动化网络数据表测试。我在用于定位的每个 WebElement 对象中都收到此 NullPointerException。我的代码尽可能简单: public class tableTestS
我有这个方法,应该从 txt 文件中读取,然后在交互 Pane 中打印出读取的信息。该方法有效,我获得了文件中的所有数据,但最终我还打印出了 java.util.NoSuchElementExcept
我试图在程序中使用 Scanner 再次从控制台获取输入,但是当在另一种方法中调用第二个 Scanner 时,它会出现 NoSuchElement异常(exception)。 如果我从运行 fileR
出于某种原因,在 Selenium WebDriver 中单击链接对我来说似乎不起作用,而在 Selenium IDE 中却工作得很好。 我在 Selenium IDE 上构建到 Selenium W
我有以下代码用于从给定列表中选择一个选项,它通常有效,但有时它会失败并在第二个 if 上出现 NoSuchElement 异常。我的印象是,如果它找不到元素,它就会再次返回循环。我相信解释很简单...
我目前有以下代码 while (!visibleTiles.isEmpty())) { tile = visibleTiles.keySet().iterator().next();
我正在编写一个使用 Selenium 的代码。为了测试 find_element_by_css_selector 函数,我编写了以下代码: self.browserProfile = webdrive
我的 Java 1.7 程序执行各种数学函数。我已将函数分成方法,它们可以单独工作。我还制作了一个提示方法,询问用户是否要继续。但是,在输入数字后,我不断收到 NoSuchElement 异常。打印“
当我访问类来测试方法时,每次尝试定义新对象并使用新定义的对象时都会遇到异常。 测试错误: UserInformationControllerTest.deleteUser:83 » NullPoi
我必须创建一个程序,对用户进行 10 个多项选择题的测验,这些问题必须从 .txt 文件读入主类。 到目前为止,为了做到这一点,我已使用 while 循环(在 hasNextLine 条件下)将 .t
我是一名优秀的程序员,十分优秀!