Wednesday, October 04, 2006

java.util.NoSuchElementException

java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)


Description from API:
The string tokenizer class allows an application to break a string into tokens.

The tokenization method is much simpler than the one used by the StreamTokenizer class.
The StringTokenizer methods do not distinguish among identifiers, numbers, and quoted strings, nor do they recognize and skip comments. http://java.sun.com/j2se/1.5.0/docs/api/index.html

I got this problem while I was trying to deploy the source code at development server.After that I found out that the sql statement only produce 5 value but inside looping it request for 6 values,that's why NOSUCHELEMENT.So make sure that it's match first,what you ask is what you get.And then lastly, I found out
that one of programmer forgot to commit her coding!!

Solution :Make sure there are matching values.

Vector vt = new Vector();
if(iStudentId != 0) {
vt = Cocurriculum.getCocuApplyList(schoolID,iStudentId);
}
int i = 0;
if(vt.size() > 0) {
i = 0;
}

for (Enumeration e = vt.elements(); e.hasMoreElements(); ) 
{//all return 6 values(if sql is mismatch with this, can produce error)
String strOptionRow = new String ((String) e.nextElement());
StringTokenizer stkTokenizer = new StringTokenizer(strOptionRow, ";");
String sApplyId = stkTokenizer.nextToken();
String sCocuType = stkTokenizer.nextToken();
String sCocuName = stkTokenizer.nextToken();
String sApplyDate = stkTokenizer.nextToken();
String sStatus = stkTokenizer.nextToken();
String sRemark = stkTokenizer.nextToken();


public static Vector getCocuApplyList(int schoolID,int iStudentId)//example of 6 values
{ 
String strQuery = "select * from cocu_program p, cocu_apply a where a.student = ? and a.program = p.program_id"; 
while (rsReturn.next())
{
//it's return 6 values
vt.addElement(iApplyId + ";" + iCocuType + ";" + sName + ";" + dApplyDate + ";" + sStatus + ";" + sRemark); 
}
return vt;
}