| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 670 人关注过本帖
标题:老是空指针异常,改了好些遍,都没成功,大家帮我看一下吧
取消只看楼主 加入收藏
actionevent
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-4-7
收藏
 问题点数:0 回复次数:0 
老是空指针异常,改了好些遍,都没成功,大家帮我看一下吧
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
import
import
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)
搜索更多相关主题的帖子: private package public return import 
2014-04-07 20:27
快速回复:老是空指针异常,改了好些遍,都没成功,大家帮我看一下吧
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019530 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved