下面这段程序,编译没问题,但运行时候会报错
Exception in thread "main" java.lang.NullPointerException
at operation.GetFileInfo.getFileNum(GetFileInfo.java:20)
at operation.ExtractOperation.extraction(ExtractOperation.java:11)
at operation.ExtractOperation.main(ExtractOperation.java:30)
报错定位在下面红色的语句上,我知道这是说有空指针存在,可是我找不到。
麻烦大家帮我找找错误!!此外,我的程序是从html源代码中匹配文字,然后输入到文件parameter.txt中,有一次运行成功了,建立了txt文件,却没内容,麻烦大家帮个忙!谢谢!
public class ExtractOperation {
static String filePath = "./source/";
public void extraction(String filePath) throws IOException{
GetFileInfo gfn = new GetFileInfo();
filePath=filePath+gfn.getFileName(filePath)+"/";
long num = gfn.getFileNum(filePath); for(int i=1;i<=num;i++){
char c[] = new char[3000]; //创建可容纳3000个字符的数组
FileReader fr = new FileReader(filePath+String.valueOf(i)+"/"+"index"+String.valueOf(i)+".txt");
int number = fr.read(c); //将数据读入字符数组c内,并返回读取的字符数
String str=new String(c,0,number); //将字符串数组转换成字符串
Extraction extra = new Extraction();
try{
extra.getAttri(str);
}
catch(IOException e){
e.printStackTrace();
}
}
}
public static void main(String args[])
{
ExtractOperation eo = new ExtractOperation();
try {
eo.extraction(filePath); } catch (IOException e) {
e.printStackTrace();
}
}
}public class GetFileInfo{
public String getFileName(String filePath)
{
String fileName=null;
int lastNum=0;
File file=new File(filePath);
String[] test=file.list();
lastNum=file.list().length;
fileName=test[lastNum-1];
return fileName;
}
public long getFileNum(String filePath)
{
long lastNum;
File file=new File(filePath);
lastNum=file.list().length; return lastNum;
}
}public class Extraction{
public void getAttri(String str)throws IOException
{
File file = new File("./source/Parameter.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
String s1="(?<=<td class=(\\u0022)tdTitle(\\u0022)>)\\w+(?=</td>)";//匹配参数名称
Pattern p1=Pattern.compile(s1);
Matcher m1=p1.matcher(str);
while(m1.find())
{
String s=m1.group();
bw.write(s);
}
bw.close();
}
}
Exception in thread "main" java.lang.NullPointerException
at operation.GetFileInfo.getFileNum(GetFileInfo.java:20)
at operation.ExtractOperation.extraction(ExtractOperation.java:11)
at operation.ExtractOperation.main(ExtractOperation.java:30)
报错定位在下面红色的语句上,我知道这是说有空指针存在,可是我找不到。
麻烦大家帮我找找错误!!此外,我的程序是从html源代码中匹配文字,然后输入到文件parameter.txt中,有一次运行成功了,建立了txt文件,却没内容,麻烦大家帮个忙!谢谢!
public class ExtractOperation {
static String filePath = "./source/";
public void extraction(String filePath) throws IOException{
GetFileInfo gfn = new GetFileInfo();
filePath=filePath+gfn.getFileName(filePath)+"/";
long num = gfn.getFileNum(filePath); for(int i=1;i<=num;i++){
char c[] = new char[3000]; //创建可容纳3000个字符的数组
FileReader fr = new FileReader(filePath+String.valueOf(i)+"/"+"index"+String.valueOf(i)+".txt");
int number = fr.read(c); //将数据读入字符数组c内,并返回读取的字符数
String str=new String(c,0,number); //将字符串数组转换成字符串
Extraction extra = new Extraction();
try{
extra.getAttri(str);
}
catch(IOException e){
e.printStackTrace();
}
}
}
public static void main(String args[])
{
ExtractOperation eo = new ExtractOperation();
try {
eo.extraction(filePath); } catch (IOException e) {
e.printStackTrace();
}
}
}public class GetFileInfo{
public String getFileName(String filePath)
{
String fileName=null;
int lastNum=0;
File file=new File(filePath);
String[] test=file.list();
lastNum=file.list().length;
fileName=test[lastNum-1];
return fileName;
}
public long getFileNum(String filePath)
{
long lastNum;
File file=new File(filePath);
lastNum=file.list().length; return lastNum;
}
}public class Extraction{
public void getAttri(String str)throws IOException
{
File file = new File("./source/Parameter.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
String s1="(?<=<td class=(\\u0022)tdTitle(\\u0022)>)\\w+(?=</td>)";//匹配参数名称
Pattern p1=Pattern.compile(s1);
Matcher m1=p1.matcher(str);
while(m1.find())
{
String s=m1.group();
bw.write(s);
}
bw.close();
}
}
首先你要判断这个路径下面有没有这个文件。
这个路径完全就是错误的,把它改了就不会报错