java
1import readData.*; 2 3public class Main { 4 public static void main(String[] args) { 5 readData[] rd = new readData[6]; 6 readDataBook rdbook = new readDataBook(); 7 readAndWrite rad = new readAndWrite(); 8 //data.csvからデータを読み込む 9 rad.read(rd, rdbook); 10 11 //何人分のデータが格納されているかの取得 12 int num = rdbook.getNum(); 13 14 //out.txtに書き込み 15 rad.write(num, rdbook); 16 } 17}
java
1import java.io.*; 2import java.util.*; 3import java.nio.file.*; 4import readData.*; 5 6public class readAndWrite { 7 public void read(readData[] rd, readDataBook rdbook) { 8 try(FileReader fr = new FileReader("data.csv")) { 9 BufferedReader reader = new BufferedReader(fr); 10 int i = 0; 11 String line; 12 String[] data; 13 line = reader.readLine(); 14 while (line != null) { 15 System.out.println(line); 16 data = line.split(","); 17 rd[i].setReadDataName(data[0]); 18 rd[i].setReadDataJapanese(data[1]); 19 rd[i].setReadDataMath(data[2]); 20 rd[i].setReadDataEnglish(data[3]); 21 rd[i].setReadDataScience(data[4]); 22 rd[i].setReadDataSocial(data[5]); 23 rdbook.addReadDataBook(rd[i]); 24 line = reader.readLine(); 25 i++; 26 } 27 }catch(FileNotFoundException e) { 28 System.out.println("File not found"); 29 }catch(IOException e) { 30 System.out.println(e); 31 } 32 } 33 34 public void write(int num, readDataBook book) { 35 try (PrintWriter w = new PrintWriter( 36 new BufferedWriter( 37 new FileWriter("out.txt")))) { 38 //各教科の平均点を書き出す 39 w.println(book.japaneseAverage()); 40 w.println(book.mathAverage()); 41 w.println(book.englishAverage()); 42 w.println(book.scienceAverage()); 43 w.println(book.socialAverage()); 44 45 //5教科平均点を書き出す 46 w.println(book.fiveAverage()); 47 48 //5教科合計の1位を書き出す 49 w.println("1位 " + book.fiveWinPerson() + " " + book.fiveWinScore()); 50 51 52 } catch(IOException e) { 53 System.out.println(e); 54 } 55 } 56 57} 58 59
java
1package readData; 2 3public class readData { 4 5 public String name; 6 public int japanese; 7 public int math; 8 public int english; 9 public int science; 10 public int social; 11 12 public void setReadDataName(String n) { 13 this.name = n; 14 } 15 16 public void setReadDataJapanese(String j) { 17 this.japanese = Integer.parseInt(j); 18 } 19 20 public void setReadDataMath(String m) { 21 this.math = Integer.parseInt(m); 22 } 23 24 public void setReadDataEnglish(String e) { 25 this.english = Integer.parseInt(e); 26 } 27 28 public void setReadDataScience(String sc) { 29 this.science = Integer.parseInt(sc); 30 } 31 32 public void setReadDataSocial(String so) { 33 this.social = Integer.parseInt(so); 34 } 35 36 public String getName() { 37 return this.name; 38 } 39 40 public int getJapanese() { 41 return this.japanese; 42 } 43 44 public int getMath() { 45 return this.math; 46 } 47 48 public int getEnglish() { 49 return this.english; 50 } 51 52 public int getScience() { 53 return this.science; 54 } 55 56 public int getSocial() { 57 return this.social; 58 } 59 60 public int getSum() { 61 int sum; 62 sum = this.japanese + this.math + this.english + this.science + this.social; 63 return sum; 64 } 65 66 public double getAverage() { 67 int sum; 68 double ave; 69 sum = this.getSum(); 70 ave = (double)sum / 5; 71 return ave; 72 } 73} 74
java
1package readData; 2 3public class readDataBook { 4 readData[] list = new readData[10]; 5 int num = 0; 6 7 //読み込んだデータを配列に格納する 8 public void addReadDataBook(readData r) { 9 list[num] = r; 10 num++; 11 } 12 //何人分のデータが格納されているかを返す 13 public int getNum() { 14 return this.num; 15 } 16 17 //各教科の平均点を返す 18 public double japaneseAverage() { 19 int sum = 0; 20 double ave; 21 for(int i=0; i<num; i++) { 22 sum += list[i].getJapanese(); 23 } 24 return ave = sum / num; 25 } 26 27 public double mathAverage() { 28 int sum = 0; 29 double ave; 30 for(int i=0; i<num; i++) { 31 sum += list[i].getMath(); 32 } 33 return ave = sum / num; 34 } 35 36 public double englishAverage() { 37 int sum = 0; 38 double ave; 39 for(int i=0; i<num; i++) { 40 sum += list[i].getEnglish(); 41 } 42 return ave = sum / num; 43 } 44 45 public double scienceAverage() { 46 int sum = 0; 47 double ave; 48 for(int i=0; i<num; i++) { 49 sum += list[i].getScience(); 50 } 51 return ave = sum / num; 52 } 53 54 public double socialAverage() { 55 int sum = 0; 56 double ave; 57 for(int i=0; i<num; i++) { 58 sum += list[i].getSocial(); 59 } 60 return ave = sum / num; 61 } 62 63 //5教科の平均点を返す 64 public double fiveAverage() { 65 int sum = 0; 66 double fiveave; 67 for(int i=0; i<num; i++) { 68 sum += list[i].getSum(); 69 } 70 fiveave = (double)sum / num; 71 return fiveave; 72 } 73 74 //5教科の1位の人を返す 75 public String fiveWinPerson() { 76 int winner = 0, rt = 0; 77 for(int i=0; i<num; i++) { 78 if(winner < list[i].getSum()) { 79 winner = list[i].getSum(); 80 rt = i; 81 } 82 } 83 return list[rt].name; 84 } 85 86 //5教科の1位の得点を返す 87 public int fiveWinScore() { 88 int winner = 0, rt = 0; 89 for(int i=0; i<num; i++) { 90 if(winner < list[i].getSum()) { 91 winner = list[i].getSum(); 92 rt = i; 93 } 94 } 95 return list[rt].getSum(); 96 } 97 98}
csv
1masaki,86,75,94,73,83 2nobuyuki,91,89,96,88,94 3hironobu,60,54,36,59,43 4syuji,71,68,69,71,71 5masayasu,42,31,56,42,38 6toshiaki,66,57,63,59,68
txt
166.0 257.0 363.0 459.0 568.0 6313.0 71位 toshiaki 313
やりたいこと
csvファイルから6人分のデータを読み込み、readDataクラスのフィールドに値を代入したのちに、readDataBookクラスの配列に格納したいです。
起きている問題
masaki,86,75,94,73,83
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "readData.readData.setReadDataName(String)" because "<parameter1>[<local5>]" is null
at readAndWrite.read(readAndWrite.java:17)
at Main.main(Main.java:9)
一人目のデータはおそらく格納できているかと思うのですが、2行目のデータ読み込みのところでStringがnullだと言われています。何が原因でしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。