老是空指针异常,改了好些遍,都没成功,大家帮我看一下吧
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)
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)
相关文章推荐
- 关于指针详解!转自CSDN论坛,自己学习的同时和大家分享下!
- 安卓开发里面可能遇到的空指针异常
- Android M原生BUG,手机使用过程中SystemServer空指针异常导致手机重启
- Android编程注意事项一常见空指针异常小结
- 在内存不足时,new (std::nothrow)并不抛出异常,而是将指针置NULL
- Zeppelin安装成功后,安装Hive之后启动Zeppelin异常
- (LeetCode)Path Sum空指针异常问题
- maven项目跳转页面报空指针错误 Servlet.service() for servlet 异常
- java中两大异常:空指针异常和数组越界异常
- mapreduce自定义类型-空指针异常之坑NullPointerException
- java异常java.lang.reflect.InvocationTargetException 并且同时带有空指针异常
- IValueConverter转换器,以前没用过,出现了异常,无法创建实例,帮忙看一下。
- 关于如何改变thinkphp中自定义的成功界面、错误界面、异常界面
- Java中避免空指针异常的方法
- FileAttributeView出现空指针异常原因分析
- AndroidStudio 依赖 ButterKnife 出现的空指针异常
- RecyclerView滑动出现空指针异常
- Kotlin最简单的入门教程——关键字,操作符,异常,空指针
- Android绑定服务后出现空指针异常问题
- RecyclerView 使用时空指针异常