类和文件的异常错误

时间:2016-12-02 17:30:11

标签: java arrays class loops exception

我的计算机上有一个名为records.txt的文件,上面有几行。每行的格式为“firstname - lastname - address - age - 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

}

}

CLASS:

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");
}
    }

1 个答案:

答案 0 :(得分:0)

我创建了一个类的实例来避免静态上下文。

基本上学生[i]没有价值,我已将其初始化为新的学生对象

public class HW6_Hamilton_Daniel {

public static void main(String[] args) {
    new HW6_Hamilton_Daniel().run();
}

public void run() {
Student[] students = new Student[5];
    int i = 0;
            String line = "Adam - Michael - Gainesville florida - 16 - 75";
            String[] Split = line.split(" - ");

    students[i] = new Student();
            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();
}

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");
}
    }

}