标题:老是空指针异常,改了好些遍,都没成功,大家帮我看一下吧
只看楼主
actionevent
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-4-7
 问题点数:0 回复次数:5 
老是空指针异常,改了好些遍,都没成功,大家帮我看一下吧
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
java小蚂蚁
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:上海
等 级:贵宾
威 望:18
帖 子:558
专家分:2186
注 册:2013-7-2
得分:0 
就这个异常找啊,Exception in thread "main" java.lang.NullPointerException
        at homework.StudentInfo.getStudentInfo(StudentInfo.java:23)
        at homework.StudentInfo.main(StudentInfo.java:116)
空指针,无非就是你要进行运算的变量值为null,找找,打几个断点,debug一下啊

学海无涯#¥%……&*(
2014-04-08 09:40
szgg520
Rank: 5Rank: 5
等 级:职业侠客
威 望:3
帖 子:79
专家分:307
注 册:2011-6-13
得分:0 
debug一下吧...向上找

[url=http://www.]深圳复印机出租[/url]
2014-04-09 21:15
日知己所无
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:38
帖 子:427
专家分:2071
注 册:2014-3-22
得分:0 
Student stu[] = new Student[5];
上面这句运行完,stu[0]至stu[4]都是null的

所以执行下面这句时
stu[i].setStuId(temp);

相当于在执行null.setStuId(temp);
结果跑出了异常
2014-04-20 10:55
w123012306
Rank: 9Rank: 9Rank: 9
来 自:湖南
等 级:蜘蛛侠
威 望:4
帖 子:307
专家分:1180
注 册:2010-4-22
得分:0 
数据与业务一定要严格检查数据来源!  记得多加if(!null)

楼上,楼下的一定要幸福开心哦!
2014-04-21 12:20
日知己所无
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:38
帖 子:427
专家分:2071
注 册:2014-3-22
得分:0 
不知道是否和下面的帖子2、3、4楼的问题是一样的,
暂时贴上来供楼主参考一下
https://bbs.bccn.net/thread-430853-1-1.html

解决办法1)在所有NextXX后面加上nextLine调用
解决办法2)统一使用nextLine,然后在程序里转数据类型

如果还没解决的话,再帮你想一些办法
2014-05-04 18:21



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-429782-1-1.html




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

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