我正在尝试显示数据库中的所有结果,但我在结果循环中收到以下错误: 无法从Vector转换为Integer 无法从Vector转换为String
<%
Vector<Object> Results = scoreBean.getAllResults();
String homeTeam;
String awayTeam;
int homeScore;
int awayScore;
final int NUM_FIELDS = 4;
for (int i=0;i<Results.size()/NUM_FIELDS;i++)
{
homeTeam = (String)Results.elementAt(i*NUM_FIELDS); \\\error here
awayTeam = (String)Results.elementAt(i*NUM_FIELDS + 1); \\\error here
homeScore = (Integer)Results.elementAt(i*NUM_FIELDS + 2);\\\error here
awayScore = (Integer)Results.elementAt(i*NUM_FIELDS + 3);\\\error here
%>
<TR>
<TD><%= homeTeam %></TD>
<TD><%= awayTeam %></TD>
<TD><%= homeScore %></TD>
<TD><%= awayScore %></TD>
</TR>
<%
}
答案 0 :(得分:1)
从错误中,Vector
中存储的值可能实际上不是整数:
homeScore = (Integer)Results.elementAt(i*NUM_FIELDS + 2);\\\error here
awayScore = (Integer)Results.elementAt(i*NUM_FIELDS + 3);\\\error here
因此ClassCastException
。检查集合中存储的值的类型。
一些注意事项:
Vector
集合已过时。请改用ArrayList
。forEach
来迭代集合。