import java.lang.*;
import java.util.*;
import java.io.*;
import javax.swing.event.*;
class OnlyExt implements FilenameFilter {
String ext;
public OnlyExt(String ext) {
this.ext = "." + ext;
}
public boolean accept(File dir, String name) {
return name.endsWith(ext);
}
}
class WuHouCX{
public static void main(String args[]) throws Exception,IOException{
String screport="/u1/report";
String path1="u1/temp/tmp1";
String path2="u1/temp/tmp2";
String path3="u1/temp/tmp3";
String dirname = "/home/date";
File f1 = new File(dirname);
FilenameFilter only = new OnlyExt("Z");
String s1[] = f1.list(only);
for (int i=0; i < s1.length; i++) {
String filename=String.valueOf(s1[i]);
String filecp="cp "+screport+"/"+filename+" "+path2;
Process p1 = Runtime.getRuntime().exec(filecp);
String gunfile="gunzip "+path2+"/"+filename;
Process pgunzip = Runtime.getRuntime().exec(gunfile);
BufferedReader input = new BufferedReader(
new InputStreamReader(pgunzip.getInputStream()));
while((gunfile = input.readLine()) != null){
}
input.close();
}
}
}
运行后报这样的错
Exception in thread "main" java.lang.NullPointerException at WuHouCX.main(WuHouCX.java:41)
请大哥们指点迷经,因为我初学,实在搞不好。
import java.util.*;
import java.io.*;
import javax.swing.event.*;
class OnlyExt implements FilenameFilter {
String ext;
public OnlyExt(String ext) {
this.ext = "." + ext;
}
public boolean accept(File dir, String name) {
return name.endsWith(ext);
}
}
class WuHouCX{
public static void main(String args[]) throws Exception,IOException{
String screport="/u1/report";
String path1="u1/temp/tmp1";
String path2="u1/temp/tmp2";
String path3="u1/temp/tmp3";
String dirname = "/home/date";
File f1 = new File(dirname);
FilenameFilter only = new OnlyExt("Z");
String s1[] = f1.list(only);
for (int i=0; i < s1.length; i++) {
String filename=String.valueOf(s1[i]);
String filecp="cp "+screport+"/"+filename+" "+path2;
Process p1 = Runtime.getRuntime().exec(filecp);
String gunfile="gunzip "+path2+"/"+filename;
Process pgunzip = Runtime.getRuntime().exec(gunfile);
BufferedReader input = new BufferedReader(
new InputStreamReader(pgunzip.getInputStream()));
while((gunfile = input.readLine()) != null){
}
input.close();
}
}
}
运行后报这样的错
Exception in thread "main" java.lang.NullPointerException at WuHouCX.main(WuHouCX.java:41)
请大哥们指点迷经,因为我初学,实在搞不好。
Exception in thread "main" java.lang.NullPointerException at WuHouCX.main(WuHouCX.java:41)
可以看出:
该错误是由于使用了值为null的对象。可能是在打开流或是进程的时候失败了,失败后返回null给等号左边的对象,然后你又使用它或用这个对象生成下一个对象,在这时就会报这种错误提示。可以看出程序在运行到类WuHouCX的main方法中的第41行的时候出现的。具体原因还是请楼主自已定位吧:
如果使用的IDE工具可以单步跟踪,各对象的值。
也可以使用System.out.println("内容");打印出被怀疑对象值。
也可以使用try()catch{}捕获并处理这些异常
先确定f1这个路近的文件存不存在。