For loop For循环导致java.lang.NullPointerException
我正在开发一个Java应用程序。当我尝试运行某个任务时,它会导致java.lang.NullPointerException。以下是错误:For loop For循环导致java.lang.NullPointerException,for-loop,nullpointerexception,For Loop,Nullpointerexception,我正在开发一个Java应用程序。当我尝试运行某个任务时,它会导致java.lang.NullPointerException。以下是错误: java.lang.NullPointerException at com.greenapple.program.core.TesterScript.buildStudentFileset(TesterScript.java:120) at com.greenapple.program.core.TesterScript$1.run(TesterScript
java.lang.NullPointerException
at com.greenapple.program.core.TesterScript.buildStudentFileset(TesterScript.java:120)
at com.greenapple.program.core.TesterScript$1.run(TesterScript.java:83)
以下是第117-124行:
public Set<StudentFiles> buildStudentFileset(String foo) {
Set<StudentFiles> set = new HashSet<StudentFiles>();
File[] studentFolders = new File(foo).listFiles(DirectoryFileFilter.instance().asFileFilter());
for (int i = 0; i < studentFolders.length; i++) {
set.add(new StudentFiles(studentFolders[i]));
}
return set;
}
我不知道为什么是NullPointerException,有人能帮我吗
全部代码:
package com.greenapple.program.core;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
@Immutable
public class TesterScript implements Testable {
private final String name;
private final String resultsFile;
private final String studentFolder, tester, testerName;
private final String addDep;
private final int millisPerTest, millisTotal;
private final UUID id;
private Thread thread = null;
public TesterScript(String scriptName, String folderOfStudentFolders, String testerPath, String testerBinaryName, String addDeps, int maxMillisPerTest, int maxMillisTotal, UUID ID, String writeResultsTo) {
name = scriptName;
resultsFile = writeResultsTo;
id = ID;
studentFolder = folderOfStudentFolders;
tester = testerPath;
testerName = testerBinaryName;
addDep = addDeps;
millisPerTest = maxMillisPerTest;
millisTotal = maxMillisTotal;
}
public synchronized void test(Map<String, String> replace, final OneArgumentFunctor<Void, Progress> progressFunct, final TwoArgumentFunctor<Void, Testable, TesterResults> finishedFunct) {
if (thread != null)
return;
final String resultsFile = doReplacement(this.resultsFile, replace);
final String studentFolder = doReplacement(this.studentFolder, replace);
final String tester = doReplacement(this.tester, replace);
final String addDep = doReplacement(this.addDep, replace);
final AtomicReference<TesterResults> res = new AtomicReference<TesterResults>();
thread = new Thread() {
Object fooLock = new Object();
TesterTask task = null;
ProgressTesterTaskRunner runner = null;
public void run() {
synchronized (fooLock) {
if (progressFunct != null)
progressFunct.invoke(new Progress("building task", 0, 1));
task = new TesterTask(buildStudentFileset("studentFolder"), new File(tester), testerName, addDep, name, millisPerTest, millisTotal);
runner = new ProgressTesterTaskRunner(task, progressFunct);
}
runner.compile();
runner.run();
res.set(runner.getResults());
}
public void interrupt() {
synchronized (fooLock) {
if (runner != null)
runner.stop();
}
}
};
thread.start();
while (thread.isAlive()) {
try {
thread.join();
} catch (InterruptedException ex) {
thread.interrupt();
continue;
}
}
if (res.get() != null)
res.get().writeResultsToFile(new File(resultsFile), 3, true);
ExecuteAfterThisThreadThread after = new ExecuteAfterThisThreadThread(Thread.currentThread(), new ZeroArgumentFunctor<Void>() {
public Void invoke() {
finishedFunct.invoke(TesterScript.this, res.get());
return null;
}
});
thread = null;
after.start();
}
public Set<StudentFiles> buildStudentFileset(String foo) {
Set<StudentFiles> set = new HashSet<StudentFiles>();
File[] studentFolders = new File(foo).listFiles(DirectoryFileFilter.instance().asFileFilter());
for (int i = 0; i < studentFolders.length; i++) {
set.add(new StudentFiles(studentFolders[i]));
}
return set;
}
private Set<File> buildAddDepFileset(Set<String> foo) {
return new HashSet<File>();
}
public UUID id() {
return id;
}
public String name() {
return name;
}
public void write(String filename) {
write(new File(filename));
}
private void write(File file) {
if (file.exists() && !file.isFile())
throw new IllegalArgumentException("not a regular file: "+file);
if (file.exists() && !FileUtils.isEmpty(file)) {
new File(file.getPath()+".old").delete();
file.renameTo(new File(file.getPath()+".old"));
file.delete();
}
final String NEWLINE = "\n";
try {
FileWriter w = new FileWriter(file);
w.write("JTTNG-Script/1.0"+NEWLINE);
w.write("Script-ID: "+id().toString()+NEWLINE);
w.write("Script-Name: "+name()+NEWLINE);
w.write("Folder-of-Student-Folders: "+folderOfStudentFolders()+NEWLINE);
w.write("Tester-Path: "+testerFile()+NEWLINE);
w.write("Tester-Binary-Name: "+testerBinaryName()+NEWLINE);
w.write("Additional-Dependencies-Length: "+"1"+NEWLINE);
w.write("Additional-Dependencies: "+addDep+NEWLINE);
w.write("Results-Path: "+resultsFile+NEWLINE);
w.write("Per-Test-Timeout: "+millisPerTest+NEWLINE);
w.write("Total-Timeout: "+millisTotal+NEWLINE);
w.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public boolean hasSubtests() {
return false;
}
public List<Testable> subtests() {
return Collections.emptyList();
}
public synchronized void stop() {
if (thread != null && thread.isAlive())
thread.interrupt();
}
public String resultFile() {
return resultsFile;
}
public String folderOfStudentFolders() {
return studentFolder;
}
public String testerFile() {
return tester;
}
public String testerBinaryName() {
return testerName;
}
public String additionalDependencies() {
return addDep;
}
public int millisPerTest() {
return millisPerTest;
}
public int millisTotal() {
return millisTotal;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof TesterScript))
return false;
return id().equals(((TesterScript)o).id());
}
@Override
public int hashCode() {
return id().hashCode();
}
@Override
public String toString() {
return name();
}
@Override
public TesterScript clone() {
return new TesterScript(name(), folderOfStudentFolders(), testerFile(), testerBinaryName(), additionalDependencies(), millisPerTest(), millisTotal(), id(), resultFile());
}
private String doReplacement(String foo, Map<String, String> replace) {
for (Map.Entry<String, String> e : replace.entrySet())
foo = foo.replace(e.getKey(), e.getValue());
return foo;
}
public boolean canWriteResults(Map<String, String> replace) {
FileOutputStream os = null;
boolean retval = true;
try {
os = new FileOutputStream(doReplacement(resultFile(), replace));
} catch (IOException e) {
e.printStackTrace();
retval = false;
} finally {
if (os != null)
try {
os.close();
} catch (IOException ex) {}
}
return retval;
}
}
package com.greenapple.program.core;
导入java.io.File;
导入java.io.FileOutputStream;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.util.Collections;
导入java.util.HashSet;
导入java.util.List;
导入java.util.Map;
导入java.util.Set;
导入java.util.UUID;
导入java.util.concurrent.AtomicReference;
@不变的
公共类TesterScript实现了可测试的{
私有最终字符串名;
私有最终字符串结果文件;
私有最终字符串学生文件夹、测试员、测试员名称;
私有最终字符串addDep;
私人最终int millisPerTest,millisTotal;
专用最终UUID id;
私有线程线程=null;
公共TesterScript(字符串脚本名、字符串文件夹文件夹、字符串测试路径、字符串测试路径、字符串测试路径、字符串addDeps、int-maxMillisPerTest、int-maxMillisTotal、UUID ID、字符串writeResultsTo){
名称=脚本名称;
resultsFile=writeResultsTo;
id=id;
学生文件夹=学生文件夹的文件夹;
tester=testerPath;
testerName=testerBinaryName;
addDep=addDeps;
millisPerTest=最大millisPerTest;
millisTotal=maxMillisTotal;
}
公共同步无效测试(映射替换、最终一个ArgumentFunctor progressFunct、最终两个ArgumentFunctor finishedFunct){
if(线程!=null)
返回;
最终字符串resultsFile=doReplacement(this.resultsFile,replace);
最终字符串studentFolder=doReplacement(this.studentFolder,replace);
最终管柱测试器=D更换(此测试器,更换);
最终字符串addDep=doReplacement(this.addDep,replace);
最终原子引用res=新原子引用();
线程=新线程(){
Object-whoolock=新对象();
TesterTask任务=null;
ProgressTesterTaskRunner=null;
公开募捐{
同步(傻瓜){
如果(progressFunct!=null)
调用(新进度(“构建任务”,0,1));
任务=新测试任务(buildStudentFileset(“studentFolder”)、新文件(tester)、testerName、addDep、name、millisPerTest、millisTotal);
runner=新的ProgressTesterTaskRunner(任务,progressFunct);
}
runner.compile();
runner.run();
res.set(runner.getResults());
}
公共空间中断(){
同步(傻瓜){
if(runner!=null)
停止();
}
}
};
thread.start();
while(thread.isAlive()){
试一试{
thread.join();
}捕获(中断异常例外){
thread.interrupt();
继续;
}
}
如果(res.get()!=null)
res.get().writeResultsToFile(新文件(结果文件),3,true);
ExecuteAftherThisThreadThread after=新的ExecuteAftherThisThreadThread(Thread.currentThread(),new ZeroArgumentFunctor()){
公共无效调用(){
finishedFunct.invoke(TesterScript.this,res.get());
返回null;
}
});
线程=null;
在.start()之后;
}
公共集buildStudentFileset(字符串foo){
Set=newhashset();
File[]studentFolders=新文件(foo).listFiles(DirectoryFileFilter.instance().asFileFilter());
对于(int i=0;ipackage com.greenapple.program.core;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
@Immutable
public class TesterScript implements Testable {
private final String name;
private final String resultsFile;
private final String studentFolder, tester, testerName;
private final String addDep;
private final int millisPerTest, millisTotal;
private final UUID id;
private Thread thread = null;
public TesterScript(String scriptName, String folderOfStudentFolders, String testerPath, String testerBinaryName, String addDeps, int maxMillisPerTest, int maxMillisTotal, UUID ID, String writeResultsTo) {
name = scriptName;
resultsFile = writeResultsTo;
id = ID;
studentFolder = folderOfStudentFolders;
tester = testerPath;
testerName = testerBinaryName;
addDep = addDeps;
millisPerTest = maxMillisPerTest;
millisTotal = maxMillisTotal;
}
public synchronized void test(Map<String, String> replace, final OneArgumentFunctor<Void, Progress> progressFunct, final TwoArgumentFunctor<Void, Testable, TesterResults> finishedFunct) {
if (thread != null)
return;
final String resultsFile = doReplacement(this.resultsFile, replace);
final String studentFolder = doReplacement(this.studentFolder, replace);
final String tester = doReplacement(this.tester, replace);
final String addDep = doReplacement(this.addDep, replace);
final AtomicReference<TesterResults> res = new AtomicReference<TesterResults>();
thread = new Thread() {
Object fooLock = new Object();
TesterTask task = null;
ProgressTesterTaskRunner runner = null;
public void run() {
synchronized (fooLock) {
if (progressFunct != null)
progressFunct.invoke(new Progress("building task", 0, 1));
task = new TesterTask(buildStudentFileset("studentFolder"), new File(tester), testerName, addDep, name, millisPerTest, millisTotal);
runner = new ProgressTesterTaskRunner(task, progressFunct);
}
runner.compile();
runner.run();
res.set(runner.getResults());
}
public void interrupt() {
synchronized (fooLock) {
if (runner != null)
runner.stop();
}
}
};
thread.start();
while (thread.isAlive()) {
try {
thread.join();
} catch (InterruptedException ex) {
thread.interrupt();
continue;
}
}
if (res.get() != null)
res.get().writeResultsToFile(new File(resultsFile), 3, true);
ExecuteAfterThisThreadThread after = new ExecuteAfterThisThreadThread(Thread.currentThread(), new ZeroArgumentFunctor<Void>() {
public Void invoke() {
finishedFunct.invoke(TesterScript.this, res.get());
return null;
}
});
thread = null;
after.start();
}
public Set<StudentFiles> buildStudentFileset(String foo) {
Set<StudentFiles> set = new HashSet<StudentFiles>();
File[] studentFolders = new File(foo).listFiles(DirectoryFileFilter.instance().asFileFilter());
for (int i = 0; i < studentFolders.length; i++) {
set.add(new StudentFiles(studentFolders[i]));
}
return set;
}
private Set<File> buildAddDepFileset(Set<String> foo) {
return new HashSet<File>();
}
public UUID id() {
return id;
}
public String name() {
return name;
}
public void write(String filename) {
write(new File(filename));
}
private void write(File file) {
if (file.exists() && !file.isFile())
throw new IllegalArgumentException("not a regular file: "+file);
if (file.exists() && !FileUtils.isEmpty(file)) {
new File(file.getPath()+".old").delete();
file.renameTo(new File(file.getPath()+".old"));
file.delete();
}
final String NEWLINE = "\n";
try {
FileWriter w = new FileWriter(file);
w.write("JTTNG-Script/1.0"+NEWLINE);
w.write("Script-ID: "+id().toString()+NEWLINE);
w.write("Script-Name: "+name()+NEWLINE);
w.write("Folder-of-Student-Folders: "+folderOfStudentFolders()+NEWLINE);
w.write("Tester-Path: "+testerFile()+NEWLINE);
w.write("Tester-Binary-Name: "+testerBinaryName()+NEWLINE);
w.write("Additional-Dependencies-Length: "+"1"+NEWLINE);
w.write("Additional-Dependencies: "+addDep+NEWLINE);
w.write("Results-Path: "+resultsFile+NEWLINE);
w.write("Per-Test-Timeout: "+millisPerTest+NEWLINE);
w.write("Total-Timeout: "+millisTotal+NEWLINE);
w.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public boolean hasSubtests() {
return false;
}
public List<Testable> subtests() {
return Collections.emptyList();
}
public synchronized void stop() {
if (thread != null && thread.isAlive())
thread.interrupt();
}
public String resultFile() {
return resultsFile;
}
public String folderOfStudentFolders() {
return studentFolder;
}
public String testerFile() {
return tester;
}
public String testerBinaryName() {
return testerName;
}
public String additionalDependencies() {
return addDep;
}
public int millisPerTest() {
return millisPerTest;
}
public int millisTotal() {
return millisTotal;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof TesterScript))
return false;
return id().equals(((TesterScript)o).id());
}
@Override
public int hashCode() {
return id().hashCode();
}
@Override
public String toString() {
return name();
}
@Override
public TesterScript clone() {
return new TesterScript(name(), folderOfStudentFolders(), testerFile(), testerBinaryName(), additionalDependencies(), millisPerTest(), millisTotal(), id(), resultFile());
}
private String doReplacement(String foo, Map<String, String> replace) {
for (Map.Entry<String, String> e : replace.entrySet())
foo = foo.replace(e.getKey(), e.getValue());
return foo;
}
public boolean canWriteResults(Map<String, String> replace) {
FileOutputStream os = null;
boolean retval = true;
try {
os = new FileOutputStream(doReplacement(resultFile(), replace));
} catch (IOException e) {
e.printStackTrace();
retval = false;
} finally {
if (os != null)
try {
os.close();
} catch (IOException ex) {}
}
return retval;
}
}
File[] studentFolders = new File(foo).listFiles(DirectoryFileFilter.instance().asFileFilter());
for (int i = 0; i < studentFolders.length; i++) {
File[] studentFolders = new File(foo).listFiles(DirectoryFileFilter.instance().asFileFilter());