Hi, I am running a JSP page, which contains two HTML forms. I have given the part of the code which I am having trouble with below. The forms basically submit a field from a drop down list (call it "dropdown list 1" for explaination purposes), which according to what is selected, populates another drop down list ("dropdown list 2") further down the page. The information is extracted from an LDAP server. The problem is, the first few times I submit a field from "dropdown list 1", it populates "dropdown list 2" fine. Then, for some reason, it decides to go bang - giving me an exception: java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(StringTokenizer.java I am not sure what is going on with my String Tokenizer instance I create in my code below called 'stbox'. I have commented the bit where things go wrong (look at the comment written in capitals). Is there something about the StringTokenizer class that I need to know about??
Kamal, Your code will go worng when variable box has odd pairs in it say if box="one=two=three=four" your while loop will work fine and String box1 = stbox.nextToken(); will fetch values one &three String box2 = stbox.nextToken(); will fetch vales two & four but say box="one=tow=three=four=five" then String box1 will fetch values one String box2 will fetch value tow String box1 will fetch values three String box2 will fetch value four String box1 will fetch values five and now String box2=stbox.nextToken() throws that exception as there is no more element in string hope this helps Cheers Praful
All desirable things in life are either illegal, banned, expensive or married to someone else !!!
.nextToken returns null once it has run out of tokens. so you have run out of tokens in your string. suggest using String s = st.nextToken(); if(s != null) doSomething(); every time you pull a token.
Kim Jong II (North Korea's Dear Leader) said:Nuclear weapons don't kill people, people kill people.
Joined: Feb 10, 2001
Tim, I guess when once the StringTokenizer object runs out of tokens, st.nextToken will throw NoSuchElement Exception,so if if String s = st.nextToken(); throws NoSuchElement Exception,it will never execute if(s != null) doSomething(); I think the one way to avoid exception is if(st.hasNextToken()) String s=st.nextElement(); Kindly let me know if I m going wrong Cheers Praful [ November 12, 2003: Message edited by: Praful Thakare ]