我的電腦上有一個名爲records.txt的文件,上面有幾行文字。每行的格式爲 「姓名 - 姓氏 - 地址 - 年齡 - GPA」類和文件異常錯誤
亞當 - 邁克爾 - 佛羅里達州蓋恩斯維爾 - 16 - 75
愛麗絲 - 巴里 - 佛羅里達州傑克遜維爾 - 17 - 40
凱文 - 亞當 - 佛羅里達州奧蘭多 - 20 - 65
雅各布 - 邁克爾 - 佛羅里達州邁阿密 - 16 - 88
塞布麗娜 - 馬修 - 佛羅里達州蓋恩斯維爾 - 18 - 90
我的程序應該將這些信息分開,並使用一組學生來顯示信息。 cgpa是由我的gpa代碼中顯示的界限給出的字母等級。這是我的代碼給我的錯誤。
Exception in thread "main" java.lang.NullPointerException
at hw6_hamilton_daniel.HW6_Hamilton_Daniel.main(HW6_Hamilton_Daniel.java:41)
C:\Users\dhami\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
這裏是我的編碼:
MAIN:
package hw6_hamilton_daniel;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
*
* @author dhami
*/
public class HW6_Hamilton_Daniel {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
File records = new File("C:\\Users\\dhami\\Desktop\\records.txt");
Scanner input = new Scanner(records);
int count = 0;
while (input.hasNextLine()) {
count = count + 1;
break;
}
input.close();
student[] students = new student[count];
Scanner input2 = new Scanner(records);
for (int i = 0; i < count; i++) {
String line = input2.nextLine();
String[] Split = line.split(" - ");
students[i].set_firstname(Split[0]);
students[i].set_lastname(Split[1]);
students[i].set_address(Split[2]);
students[i].set_age(Split[3]);
students[i].set_gpa(Split[4]);
students[i].Display_info();
break;
}
input2.close();
} catch (FileNotFoundException ex) {
System.out.println("File Not Found");
}//IF FILE IS NOT ON MACHINE
}
}
類:
public class student {
private String firstname;
private String lastname;
private String address;
private int age;
private int gpa;
private char cgpa;
student() {
firstname = "Default_FName";
lastname = "Default_LName";
address = "Default_Address";
age = 0;
gpa = 0;
cgpa = 'X';
}//Default constructor
void set_firstname (String new_name) {
firstname = new_name;
}
String get_firstname() {
return firstname;
}//firstname-------------
void set_lastname (String new_lastname) {
lastname = new_lastname;
}
String get_lastname() {
return lastname;
}//lastname--------------
void set_address (String new_address) {
address = new_address;
}
String get_address() {
return address;
}//address---------------
void set_age (String new_age) {
age = Integer.parseInt(new_age);
}
int get_age() {
return age;
}//age-------------------
void set_gpa (String new_gpa) {
gpa = Integer.parseInt(new_gpa);
if (gpa >= 85 && gpa <= 100) {
cgpa = 'A';
}
if (gpa >=60 && gpa <= 85) {
cgpa = 'B';
}
else {
cgpa = 'C';
}
}
int get_gpa() {
return gpa;
}//gpa-------------------
char get_cgpa() {
return cgpa;
}
void Display_info() {
System.out.println("First Name: "+firstname);
System.out.println("Last Name: "+lastname);
System.out.println("Address: "+address);
System.out.println("Age: "+age);
System.out.println("GPA: "+gpa+" ("+cgpa+")\r\n");
}
}
請谷歌NullPointerException異常,因爲這是一個常見的問題和常見並回答了這個網站。 –
您可以創建學生數組,但在嘗試使用它之前從未用對象填充它。再次,因爲它被問了很多次,所以首先在這裏進行搜索。解決方案:在嘗試使用新學生之前先填寫新學生。您可以在for循環的開始處執行此操作。 –
[這是一個類似的問題](http://stackoverflow.com/questions/14677075/null-pointer-exception-for-array-of-objects) –