Java 它的包含测试将始终返回false。我不能马上判断这是否是你的流氓空的原因,但根据你声称想要做的事情,这肯定是错误的

Java 它的包含测试将始终返回false。我不能马上判断这是否是你的流氓空的原因,但根据你声称想要做的事情,这肯定是错误的,java,database,sqlite,map,Java,Database,Sqlite,Map,尝试将代码重构成几个更小的片段,以便更容易验证其正确性。我建议作为第一个切入点:使用私有方法从文件中获取一组单词(作为参数提供),使用私有方法将一组单词与数据库进行比较,以及使用循环将这两种方法结合起来以实现总体目标。您没有调试器吗?检查result\m或rs\u mail.getString(“voc\u w”)中是否有一个(或两个!)为空。是否有任何原因导致mail.put(nome\u Filex,sq)正在使用nome\u Filex而不是name\u Filex?如果您有一个nome\

尝试将代码重构成几个更小的片段,以便更容易验证其正确性。我建议作为第一个切入点:使用私有方法从文件中获取一组单词(作为参数提供),使用私有方法将一组单词与数据库进行比较,以及使用循环将这两种方法结合起来以实现总体目标。

您没有调试器吗?检查
result\m
rs\u mail.getString(“voc\u w”)
中是否有一个(或两个!)为空。是否有任何原因导致
mail.put(nome\u Filex,sq)
正在使用
nome\u Filex
而不是
name\u Filex
?如果您有一个
nome\u Filex
隐藏在某处,那么这可能就是
mail.get(name\u Filex)
将返回
null
(使
result\m
null
)的原因。这是一个打字错误。我没有直接从IDE复制代码,很抱歉,我的错误似乎是
选择不同的voc。作为voc\u w来自voc\u words
的单词应该是
选择不同的单词作为voc\u w来自voc\u words
。至少
voc
没有定义。我重新测试了代码,注意到一些读取的文件是空的(例如:文件有两行,但这些是空的,没有字符或字符串可读取),这似乎是导致错误的原因,将数据库中的字与空值进行比较会返回错误-当然-所以我想知道,如果文件为空(有更多的行,但没有字符串/单词),如何避免在HashMap中写入内容?我在result\m之后添加了一个System.out.println来打印一些值​​正确地说,之后它返回null,但我不明白为什么。顺便说一下,我删除了toString();在getString中,但错误再次出现在重新测试代码时,我注意到一些读取的文件是空的(例如:文件有两行,但这些是空的,没有要读取的字符或字符串),这似乎是导致错误的原因,将数据库中的字与空值进行比较会返回错误-当然-所以我想知道,如果文件为空(有更多行,但没有字符串/单词),如何避免在HashMap中写入内容?从文件中提取数据时,不要添加空格<代码>字符串=…readFromFile。。。;如果(!string.isEmpty()){sq.add(string);}我继续重写代码,并将代码分成小块我重新测试了代码,我注意到一些读取的文件是空的(例如:文件有两行,但这些是空的,没有字符或字符串可读取),这似乎是导致错误的原因,将数据库中的单词与空值进行比较会返回一个错误-当然-因此我想知道,如果文件为空(有更多行,但没有字符串/单词),如何避免在HashMap中写入?
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
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
// 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();
    }
}