您的位置:首页 > 其它

老是空指针异常,改了好些遍,都没成功,大家帮我看一下吧

2014-04-20 11:57 253 查看
Student.java文件

package homework;

import java.util.Map;

public class Student {

private String stuId;

private String stuName;

private Map<String, Double> map;

public String getStuId() {

return stuId;

}

public void setStuId(String stuId) {

this.stuId = stuId;

}

public String getStuName() {

return stuName;

}

public void setStuName(String stuName) {

this.stuName = stuName;

}

public Map<String, Double> getMap() {

return map;

}

public void setMap(Map<String, Double> map) {

this.map = map;

}

}

StudentInfo.java文件

package homework;

import java.io.BufferedWriter;

import java.io.FileWriter;

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

import java.util.Scanner;

public class StudentInfo {

public Student[] getStudentInfo() {

Scanner sc = new Scanner(System.in);

Student stu[] = new Student[5];

BufferedWriter bw = null;

try {

bw = new BufferedWriter(new FileWriter("stu.txt"));

for (int i = 0; i < stu.length; i++) {

String temp = "";

double real = 0;

System.out.println("请输入学号:");

temp = sc.next();

stu[i].setStuId(temp);

bw.write(temp + " ");

System.out.println();

System.out.println("请输入姓名:");

temp = sc.next();

stu[i].setStuName(temp);

bw.write(temp + " ");

System.out.println();

Map<String, Double> map = new HashMap<String, Double>();

System.out.println("请输入语文成绩:");

real = sc.nextDouble();

map.put("chinese", real);

bw.write(temp + " ");

System.out.println();

System.out.println("请输入数学成绩:");

real = sc.nextDouble();

map.put("math", real);

bw.write(temp + " ");

System.out.println();

System.out.println("请输入英语成绩:");

real = sc.nextDouble();

map.put("english", real);

bw.write(temp + " ");

bw.newLine();

stu[i].setMap(map);

System.out.println();

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

try {

if (bw != null) {

bw.flush();

bw.close();

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return stu;

}

public void aveScores(Student[] stu) {

BufferedWriter bw = null;

try {

bw = new BufferedWriter(new FileWriter("stu.txt",true));

System.out.print("语文平均成绩是:");

for (int i = 0; i < stu.length; i++) {

double chineseScore = 0;

chineseScore += stu[i].getMap().get("chinese");

double aveScore = chineseScore/3;

System.out.println(aveScore);

bw.write("语文平均成绩是:" + aveScore);

bw.newLine();

}

System.out.print("数学平均成绩是:");

for (int i = 0; i < stu.length; i++) {

double mathScore = 0;

mathScore += stu[i].getMap().get("math");

double aveScore = mathScore/3;

System.out.println(aveScore);

bw.write("数学平均成绩是:" + aveScore);

bw.newLine();

}

System.out.print("英语平均成绩是:");

for (int i = 0; i < stu.length; i++) {

double englishScore = 0;

englishScore += stu[i].getMap().get("english");

double aveScore = englishScore/3;

System.out.println(aveScore);

bw.write("英语平均成绩是:" + aveScore);

bw.newLine();

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

try {

if (bw != null) {

bw.flush();

bw.close();

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

public static void main(String[] args) {

StudentInfo si = new StudentInfo();

Student stu[] = si.getStudentInfo();

si.aveScores(stu);

}

}

D:\editplus>java homework.StudentInfo

请输入学号:

1000

Exception in thread "main" java.lang.NullPointerException

at homework.StudentInfo.getStudentInfo(StudentInfo.java:23)

at homework.StudentInfo.main(StudentInfo.java:116)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: