---
title: "遍历取值异常（Hashtable Enumerator）"
author: "JsonLiangyoujun"
date: "2017-03-08T17:21:55Z"
tags: []
categories: ["编程语言"]
slug: "hashtable-enumerator"
draft: false
lastmod: "2017-03-08T17:23:44Z"
id: 2360983
url: "https://jsonliangyoujun.iteye.com/blog/2360983"
---

用迭代器取值时抛出的异常：java.util.NoSuchElementException: Hashtable Enumerator
    代码示例：
```java
//使用迭代器遍历
			Iterator<String> it = tableProper.stringPropertyNames().iterator();			
			sqlMap = new HashMap<String,String>();
			while(it.hasNext()){
				sqlMap.put(it.next(), tableProper.getProperty(it.next()));
			}
```

    这是一个枚举异常，是因为在还没来得及执行it.next()时就开始引用它。我们可以用如下方式解决此问题：

```java
//使用迭代器遍历
			Iterator<String> it = tableProper.stringPropertyNames().iterator();			
			sqlMap = new HashMap<String,String>();
			String key;
			while(it.hasNext()){
				key = it.next();
				sqlMap.put(key, tableProper.getProperty(key));
			}
```

    原文永久地址：<http://jsonliangyoujun.iteye.com/blog/2360983>