2016-09-17 36 views
0

我有兩個csv文件。
CSV File1 = csvFrom,//它有兩列,column1 = Email(只有5封電子郵件),Column2 =密碼
CSV File2 = csvSendTo //它只有一列= Email。 (數千封電子郵件)。在java中讀取並解析CSV文件

我想讀取csvFrom文件,我想要第一個電子郵件ID和它的密碼從第一個文件。然後從第二個csvSendTo文件發送20封電子郵件。想要將第一封電子郵件ID和密碼的電子郵件發送到這20封電子郵件。我可以用一個電子郵件ID手動發送電子郵件。但是當我試圖從csv文件讀取它時,它給了我一個NullPointerException。下面是我的代碼:我只是粘貼了這裏給我一個錯誤的部分。有誰可以在這裏指導我嗎?這個for循環在這裏是錯誤的,但我有點困惑,所以無法用精確的循環代替。

BufferedReader br1=null; 
    BufferedReader br2=null; 
    String line1="",line2=""; 
    String csvSplitBy=","; 
    String strMailFrom="",strPassword=""; 
    int countCSVFrom=0,countCSVSendTo=0; 
    System.out.println("strCSVFrom=" + strCSVFrom + ", strcsvSendTo=" + strCSVSendTo); 
    try{ 
     br1=new BufferedReader(new FileReader(strCSVFrom)); 
     br2=new BufferedReader(new FileReader(strCSVSendTo)); 
     String[] strarrFromEmail; 
     while((line1=br1.readLine())!=null){ 
      countCSVFrom+=1; 
      strarrFromEmail=line1.split(csvSplitBy); 
     // for(int i=countCSVFrom-1;i<=countCSVFrom;i++){ 
     //  strMailFrom=strarrFromEmail[i]; 
     //  strPassword=strarrFromEmail[i+1]; //While here its ArrayIndexOutOfBounds Exception 
     // } 
     //what is the correct thing to write it over here instead of for loop? 
     } 
     System.out.println("countcsvfrom="+countCSVFrom + ", line1=" + line1.toString()); //Giving me an error of NullPointerException over here. 

     System.out.println("strFrom="+strMailFrom + ", strPassword="+strPassword); 
     while((line2=br2.readLine())!=null){ 
      countCSVSendTo+=1; 
     } 
     System.out.println("countcsvsendto="+countCSVSendTo); 
    }catch(FileNotFoundException fnfe){ 
     fnfe.printStackTrace(); 
    }catch(IOException ioe){ 
     ioe.printStackTrace(); 
    } 
+0

堆棧跟蹤或行號錯誤發生在這裏是必需的 – Sanka

+0

我已更新我的問題。 –

回答

0

你可以寫:

//your code here and try catch block below 
try { 
    List<String[]> csvLines = new ArrayList<String[]>(); 
    Files.lines(Paths.get(strCSVFrom)).map(e -> e.split(csvSplitBy)).forEach(csvLines.add(e)); 
} catch (Exception) { 
    //exception handling 
} 
1

第二System.out.println,只是第while環的右括號後,給人的NPE。它使用line1,所述while環路保證的終止條件將在該點處爲null

+0

謝謝你的幫助。是的,我得到了,但沒有得到如何解決它。 –