NoSuchElementException 的原因是什麼?如何在 Java 中修復它?
NoSuchElementException 的原因是什麼?如何在 Java 中修復它?
異常是在程式執行期間發生的錯誤(執行時錯誤)。當發生異常時,程式會突然終止,異常行之後的程式碼將不會執行。每個異常都由其相應的類表示。
NoSuchElementException 的原因
這是一個執行時異常,即它在執行時發生。
使用列舉、迭代器或標記器的訪問器方法(例如 next() 或 nextElement())訪問集合、陣列或其他物件的內容時,如果嘗試從空物件獲取元素,或者嘗試在到達物件(集合、陣列或其他)的末尾後獲取下一個元素,則會生成 NoSuchElementException。
例如:
- 如果在空列舉物件上呼叫 Enumeration 類的 nextElement() 方法,或者當前位置位於列舉的末尾,則會在執行時生成 NoSuchElementException。
- 如果在空 StringTokenizer 物件上呼叫 StringTokenizer 類的 nextElement() 和 nextToken() 方法,或者當前位置位於 StringTokenizer 的末尾,則會在執行時生成 NoSuchElementException。
- 如果在空迭代器/列表迭代器上呼叫 Iterator 或 ListIterator 類的 next() 方法,或者當前位置位於末尾,則會在執行時生成 Iterator/ListIterator NoSuchElementException。
- 同樣,如果在空 ListIterator 物件上呼叫 ListIterator 類的 previous() 方法,或者當前位置位於 ListIterator 的開頭,則會在執行時生成 NoSuchElementException。
示例
import java.util.StringTokenizer;
public class StringTokenizerExample{
public static void main(String args[]) {
String str = "Hello how are you";
//Instantiating the StringTokenizer class
StringTokenizer tokenizer = new StringTokenizer(str, " ");
//Printing all the tokens
System.out.println(tokenizer.nextToken());
System.out.println(tokenizer.nextToken());
System.out.println(tokenizer.nextToken());
System.out.println(tokenizer.nextToken());
//Getting the next token after reaching the end
tokenizer.nextToken();
tokenizer.nextElement();
}
}執行時錯誤
Hello how are you Exception in thread "main" java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(Unknown Source) at MyPackage.StringTokenizerExample.main(StringTokenizerExample.java:16)
處理/修復 NoSuchElementException
幾乎所有其訪問器方法會導致 NoSuchElementException 的類都包含各自的方法來驗證物件(集合、標記器等)是否包含更多元素。
例如:
- Enumeration 類包含一個名為 hasMoreElements() 的方法,如果當前物件在當前位置之後包含更多元素,則返回 true(否則返回 false)。
- StringTokenizer 類包含名為 hasMoreTokens() 和 hasMoreElements() 的方法,如果當前物件在當前位置之後包含更多元素,則返回 true(否則返回 false)。
- Iterator 類包含 hasNext() 方法,如果當前迭代器在當前位置之後包含更多元素,則返回 true(否則返回 false)。
- ListIterator 類包含 hasPrevious() 方法,如果當前迭代器在當前位置之前包含更多元素,則返回 true(否則返回 false)。
在 while 迴圈中,使用這些方法驗證相應物件是否包含更多元素,僅當條件為 true 時才打印/訪問元素。這可以防止在物件中沒有元素或到達末尾時使用訪問器方法訪問元素。
Enumeration 類的 hasMoreElements() 方法
import java.util.Enumeration;
import java.util.Vector;
public class EnumExample {
public static void main(String args[]) {
//instantiating a Vector
Vector<Integer> vec = new Vector<Integer>( );
//Populating the vector
vec.add(1254);
vec.add(4587);
//Retrieving the elements using the Enumeration
Enumeration<Integer> en = vec.elements();
while(en.hasMoreElements()) {
System.out.println(en.nextElement());
}
}
}輸出
1254 4587
StringTokenizer 類的 nextMoreTokens() 方法 −
import java.util.StringTokenizer;
public class StringTokenizerExample{
public static void main(String args[]) {
String str = "Hello how are you";
//Instantiating the StringTokenizer class
StringTokenizer tokenizer = new StringTokenizer(str, " ");
//Printing all the tokens
while(tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken());
}
}
}輸出
Hello how are you
Iterator 類的 hasNext() 方法 −
import java.util.ArrayList;
import java.util.Iterator;
public class NextElementExample{
public static void main(String args[]) {
//Instantiating an ArrayList object
ArrayList<String> list = new ArrayList<String>();
//populating the ArrayList
list.add("apples");
list.add("mangoes");
list.add("oranges");
//Getting the Iterator object of the ArrayList
Iterator it = list.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}輸出
apples mangoes oranges
ListIterator 類的 hasPrevious() 方法 −
import java.util.ArrayList;
import java.util.ListIterator;
public class NextElementExample{
public static void main(String args[]) {
//Instantiating an ArrayList object
ArrayList<String> list = new ArrayList<String>();
//populating the ArrayList
list.add("apples");
list.add("mangoes");
list.add("oranges");
//Getting the Iterator object of the ArrayList
ListIterator<String> it = list.listIterator();
while(it.hasNext()) {
it.next();
}
while(it.hasPrevious()) {
System.out.println(it.previous());
}
}
}輸出
oranges mangoes apples
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP