max*_*max 1 java enumeration arraylist
我有一个数组列表我试图在其中添加值,我遇到异常.我已多次使用此代码仍然无法弄清楚创建此错误的原因,下面是您的参考代码.
我正在使用的方法添加方法我进入空指针异常,所有上述值都在控制台中打印出来)
sid = new ArrayList<String>();
Enumeration e = Global.qtutlist.keys();
int qj=0;
//iterate through Hashtable keys Enumeration
while(e.hasMoreElements())
{
System.out.println("sid is key and its value id" );
System.out.println(Integer.parseInt(e.nextElement().toString()));
try
{
sid.add(e.nextElement().toString());
System.out.println("lenght is "+ sid.size());
}
catch(Exception ex)
{
System.out.println("caught exception is"+ex.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
你nextElement()在循环中调用两次并检查一次
如下
while(e.hasMoreElements())
{ String item = e.nextElement().toString()
System.out.println("sid is key and its value id" );
System.out.println(Integer.parseInt(item));
try{
sid.add(item);
System.out.println("lenght is "+ sid.size());
}catch(Exception ex){
System.out.println("caught exception is"+ex.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
如果显示NumberFormatException则其中一个字符串无法解析int