我还有另一个问题: 以下代码执行以下操作
对于文件夹中的每个文件
打开文件并阅读其内容
将每一行划分为令牌
将每个标记(单词)保存在hasMap
准备数据库查询(选择表单字词......)
对于令牌与数据库Write 1.0中包含的单词之间的每个匹配,如果为true,否则为0.0;
此时出现问题:
try{
while (rs_mail.next()) {
if(result_m.contains(rs_mail.getString("voc_w").toString())) //HERE I GET THE ERROR! java.lang.NullPointerException
out_final.print("1.0;");
else
out_final.print("0.0;");
}//Close While
} //Close TRY
finally{
rs_mail.close();
//result_m.clear();
mail.clear(); //Clear MAP
}
完整代码下方:
String path ="C:/Users/.../file";
File currentDIR = new File("C:/Users/.../file");
File files_mail[]=currentDIR.listFiles();
String tmp_mail="";
// prepares the file tmpTraning.txt to receive value 1.0, 0.0 obtained by comparison with database
PrintWriter out_final=null;
File ff=new File("C:/Users/.../tmpTraning.txt");
//Seach for File in DIR
for( File fX : files_mail ){
String name_Filex = fX.getName();
FileReader fr = null;
BufferedReader fINx = null;
String sx;
//Create MAP
Map<String, Set<String>> mail = new HashMap<String, Set<String>>();
//Open File
try{
Set<String> sq = new HashSet<String>();
fr = new FileReader(path+"/"+name_Filex);
fINx = new BufferedReader(fr);
sx = fINx.readLine();
//scroll the file
while(sx != null) {
StringTokenizer stq = new StringTokenizer(sx);
while(stq.hasMoreTokens()) { //Extract form line the single word
tmp_mail = stq.nextToken();
sq.add(tmp_mail.toString().toLowerCase()); //add the word to sq -> HashMap
mail.put(nome_Filex, sq);
}// Close st.hasMoreTokens()
sx = fINx.readLine();
} //Close while for scroll File
fr.close(); //Close fileReader
sq.clear(); //Clear HasSet
} //Close il TRAY
catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
Set<String> result_m = mail.get(name_Filex);
ResultSet rs_mail = stmt.executeQuery("SELECT DISTINCT voc.words as voc_w FROM voc_words as voc");
//Prepare for writing on the file " tmpTraning.txt "
OutputStreamWriter fout_f = new OutputStreamWriter(new FileOutputStream(ff,true));
out_final = new PrintWriter(fout_f);
try{
while (rs_mail.next()) {
//If the word extract from the database is in MAP (name_Filex) then print 1.0; on the file tmpTraning.txt
if(result_m.contains(rs_mail.getString("voc_w").toString())) //HERE I GET THE ERROR! java.lang.NullPointerException
out_final.print("1.0;");
else
//else print 0.0;
out_final.print("0.0;");
}
} //Close TRY
finally{
rs_mail.close();
//result_m.clear();
mail.clear(); //Clear MAP
}
out_final.println(""); //Send CR char ASCII to set the coursor for the next file on the new line
out_final.close();
out_final.flush();
} // End SCAN DIR
感谢您的任何建议!
代码更改 - 打印result_m的内容:
String path ="...";
File currentDIR = new File("...");
File files_mail[]=currentDIR.listFiles();
String tmp_mail="";
// prepares the file tmpTraning.txt to receive value 1.0, 0.0 obtained by comparison with database
PrintWriter out_final=null;
File ff=new File("...");
//Seach for File in DIR
for( File fX : currentDIR.listFiles() ){
String name_Filex = fX.getName();
String sx;
//Create MAP
Map<String, Set<String>> mail = new HashMap<String, Set<String>>();
//Open File
try{
Set<String> sq = new HashSet<String>();
BufferedReader fINx = new BufferedReader(new FileReader(fX));
sx = fINx.readLine();
//scroll the file
while(sx != null) {
StringTokenizer stq = new StringTokenizer(sx);
while(stq.hasMoreTokens()) { //Extract form line the single word
tmp_mail = stq.nextToken();
sq.add(tmp_mail.toString().toLowerCase()); //add the word to sq -> HashMap
mail.put(name_Filex, sq);
}// Close st.hasMoreTokens()
sx = fINx.readLine();
} //Close while for scroll File
fr.close(); //Close fileReader
sq.clear(); //Clear HasSet
} //Close il TRAY
catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
/*
* print the contents of result_m
*/
System.out.println("----- START FILE -----");
Set<String> result_m = mail.get(name_Filex);
Object[] toArray_m = mail.get(name_Filex).toArray();
for (int ncc=0; ncc<result_m.size();ncc++){
System.out.println(toArray_m[ncc]);
}
System.out.println("----- END FILE -----");
} // End SCAN DIR
如果程序读取的文件包含空行(没有字符,没有字符串),则保存空值
答案 0 :(得分:0)
你是否有充分理由在字符串上调用toString()
?如果getString("voc_w")
为null
,则会导致您的例外。
正如manji在评论中指出的那样,它是我提到的电话,或result_m
Set
null
。
答案 1 :(得分:0)
遗憾的是,您的代码存在很多问题。这是一个,立刻引起了我的注意。
您正在使用字符串操作来创建要传递给FileReader
构造函数的文件名,尽管据我所知,正在打开的文件是从目录列表返回的文件。那只是在惹麻烦。相反,代码会更好地写得更像......
// Lots of things are omitted; this is just a sketch...
String path = "...";
File currentDIR = new File(path);
for (File fX : currentDIR.listFiles()) {
try {
BufferedReader fINx = new BufferedReader(new FileReader(fX));
// ...
} catch (IOException e) {
e.printErrorStack();
}
}
然而,你有更多的问题。例如,sq.clear()
的使用具有错误的代码气味。您刚刚在Set
中存储了对Map
的引用;为什么要再次删除其内容?变量超出范围;你可以简单地留下那些代码。 clear()
之后的result_m
的下游结果将是一个空集,因此其contains
测试将始终返回false。我不能随便告诉你这是否是你的流氓null的原因,但根据你声称要做的事情,它必须是错误的。
尝试将该代码重构为几个更容易验证的小块。我建议作为第一个剪切:一个私有方法从File获取一组单词(作为参数提供),一个私有方法来比较一组单词与数据库,以及一个方法,将这两个与一些循环结合起来实现你的总体目标。