为什么我的Java代码导致netbeans冻结
我一直在做一件工作,当我试图运行它时,我的IDE冻结,我不得不使用任务管理器来关闭它 知道为什么吗?我知道这与迭代器有关,而我使用的是循环 有人能发现吗?下面是servlet代码的一部分:为什么我的Java代码导致netbeans冻结,java,jsp,netbeans,Java,Jsp,Netbeans,我一直在做一件工作,当我试图运行它时,我的IDE冻结,我不得不使用任务管理器来关闭它 知道为什么吗?我知道这与迭代器有关,而我使用的是循环 有人能发现吗?下面是servlet代码的一部分: try { List<FileItem> fields = upload.parseRequest(request); Iterator<FileItem> it = fields.iterator(); //starting
try {
List<FileItem> fields = upload.parseRequest(request);
Iterator<FileItem> it = fields.iterator();
//starting conversion from file to sample
//creating a container to hold the samples
ArrayList<sample> samplelist = new ArrayList();
while (it.hasNext()) {
sample c = new sample();
FileItem fileItem = it.next();
c.setFileName(fileItem.getName());
c.setPayload(fileItem.getString());
samplelist.add(c);
}
//looping through uploaded samples to split code into lines
for (int i = 0; i < samplelist.size(); i++) {
sample current = new sample();
current = samplelist.get(i);
//splitting whole code block into lines based on ;
String[] temp;
temp = current.getPayload().split(";");
//going through split lines and adding them to the sample's rawcode
//arraylist after checking for for loops
for (int j = 0; j < temp.length; j++) {
// current.Raw_Code.add(temp[j]);
String current_line = temp[j];
current.Raw_Code.add(current_line);
}
}
//testing
System.out.print("starting testing");
for (int i = 0; i < samplelist.size(); i++) {
sample current = new sample();
current = samplelist.get(i);
System.out.print("File name <br/>");
current.getFileName();
System.out.print("lines detected <br/>");
Iterator itr = current.Raw_Code.iterator();
//this while loop started to cause the problem
while(itr.hasNext()){
System.out.print("x");
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
out.close();
return;
试试看{
列表字段=upload.parseRequest(请求);
迭代器it=fields.Iterator();
//开始从文件到示例的转换
//创建用于保存样本的容器
ArrayList samplelist=新的ArrayList();
while(it.hasNext()){
样本c=新样本();
FileItem FileItem=it.next();
c、 setFileName(fileItem.getName());
c、 setPayload(fileItem.getString());
样本列表。添加(c);
}
//通过上传的示例循环,将代码拆分为行
对于(int i=0;i ”);
current.getFileName();
System.out.print(“检测到的行
”);
迭代器itr=current.Raw_Code.Iterator();
//这个while循环开始导致问题
while(itr.hasNext()){
系统输出打印(“x”);
}
}
}捕获(文件上载异常){
e、 printStackTrace();
}
out.close();
返回;
就在这里:
while(itr.hasNext()){
System.out.print("x");
}
您没有调用itr.next(),因此迭代器没有进行迭代。:)在
while
循环中添加itr.next()
将纠正此问题。在该位置缺少itr.next()while是一个无限循环-迭代器总是有next,因为它从不迭代。。。所以它一直在继续
另一方面,每当IDE崩溃时,您应该考虑“无限循环,因为这是编程问题的常见副作用。使用分步调试程序这是其中之一:“让cpu烧坏!“问题:)