• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

StringTokenizer error on JSP

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??

<%@ page contentType="text/html" %>

<%--//Page Directives --%>
<%@ page language="java" %>
<%@ page import="javax.xml.parsers.*" %>
<%@ page import="org.w3c.dom.Document" %>
<%@ page import="org.w3c.dom.Element" %>
<%@ page import="org.w3c.dom.DOMException" %>
<%@ page import="java.net.URL" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.io.IOException" %>
<%@ page import="logonBean.*" %>
<%@ page import="java.security.*" %>
<%@ page import="java.util.Hashtable" %>
<%@ page import="java.util.Enumeration" %>
<%@ page import="java.util.StringTokenizer" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.naming.*" %>
<%@ page import="javax.naming.directory.*" %>
<%@ page import="javax.servlet.*" %>
<%! public String routeURL="logon.jsp";%>
<%! static String[] appnames = new String[40];%>
<%! static String[] boxnames = new String[40];%>
<%! int n=0; int flag=0; String name1 = ""; String name2 = ""; String parameterValue = ""; String parameterName="";%>
<%! int flag1=0; int m=0; String parameterValue1 = "";%>
<%-- //HTML Header --%>

<html>
<head>
<title>Main Page</title>
</head>
<h1>LDAP Server Connection</h1>
<body bgcolor="white">
<script language='javascript'>
function post(){
document.forms[0].submit();
}
</script>
<FORM name=form2 id=form2 ACTION=main.jsp METHOD=POST>
<% Hashtable env1 = new Hashtable(5, 0.75f);
env1.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env1.put(Context.PROVIDER_URL, "ldap://boxname/dc=db,dc=com");
try {
DirContext ctx1 = new InitialDirContext(env1);
SearchControls constraints1 = new SearchControls();
constraints1.setSearchScope(SearchControls.SUBTREE_SCOPE);
//instead of ou=WebSSO, take user input to determine value into variable
NamingEnumeration box_results = ctx1.search(ou + ", ou=Application", "(boxname=*)", constraints1);
m=0;
while(box_results != null && box_results.hasMore()) {
SearchResult search = (SearchResult)box_results.next();
String box = search.getName();
StringTokenizer stbox = new StringTokenizer(box, "=");
while(stbox.hasMoreElements()){
//GOING WRONG DOWN HERE!!! WORKS FOR A WHILE THEN CAUSES AN ERROR!!!
String box1 = stbox.nextToken();
String box2 = stbox.nextToken();
boxnames[m] = box2;
System.out.println(boxnames[m]+" , ");
m++;
}
}
//Exception Handling
}//end of try

catch(NamingException e)
{
e.printStackTrace();
}
parameterValue1 = request.getParameter("App");
%>
<br><br><br>
<TR> <TD> Choose a Unix Box for the Application selected</TD>
<TD> <SELECT onSelect='javascript ost()' NAME=Box>
<% if(parameterValue1==null){%>
<OPTION selected>-------Select Value-------</OPTION>
<%}else {%>
<OPTION>-------Select Value-------</OPTION>
<%}
int l=0;
while(boxnames[l]!=null){
if(boxnames[l].equals(parameterValue1)){
%>
<OPTION selected><%=boxnames[l]%></OPTION>
<%}else{%>
<OPTION><%=boxnames[l]%></OPTION>
<%}
l++;
}
%>
</SELECT></TD>
</TR>
<INPUT TYPE=SUBMIT VALUE="Submit BOXNAME">
</FORM>
<HR>
<B>Box Selected</B><BR>
<TABLE>
<%
Enumeration parameter1 = request.getParameterNames();
parameterValue1 = request.getParameter("Box");
%>
<TR>
<TD><%=parameterValue1%></TD>
</TR>
<%
%>
</TABLE>
</body>
</html>

Thankyou for your help guys.
Kamal.
 
Ranch Hand
Posts: 645
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
.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.
 
Praful Thakare
Ranch Hand
Posts: 645
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nope you're right i wasn't remembering right
 
Kamal Patel
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Praful and Tim - it worked!
reply
    Bookmark Topic Watch Topic
  • New Topic