weixin_41731967
weixin_41731967
采纳率12.5%
2018-02-15 10:15

java空指针异常问题 为何会出现空指针异常

10

public class Student {
private int on;
private String name;
private int age;
private int java;
private int c;
private int html;
private int sql;
private int sum;
private int avg;
public int getOn() {
return on;
}
public void setOn(int on) {
this.on = on;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getJava() {
return java;
}
public void setJava(int java) {
this.java = java;
}
public int getC() {
return c;
}
public void setC(int c) {
this.c = c;
}
public int getHtml() {
return html;
}
public void setHtml(int html) {
this.html = html;
}
public int getSql() {
return sql;
}
public void setSql(int sql) {
this.sql = sql;
}
public int getSum() {
return sum;
}
public void setSum() {
this.sum = this.java+this.c+this.sql+this.html;
}
public int getAvg() {
return avg;
}
public void setAvg() {
this.avg = this.sum/4;
}
public String toString(){
String msg="\t"+this.on+"\t"+this.name+"\t"+this.age+"\t"+this.java
+"\t"+this.c+"\t"+this.html+"\t"+this.sql+"\t"+this.sum+"\t"
+this.age;
return msg;
}
}

public class Admin {//管理学生类
public void addStudent(String name,int age,Student[] stuArr){
Student stu=new Student();
int i=this.getIndex(stuArr);
stuArr[i]=stu;
stu.setName(name);
stu.setAge(age);
this.show(stuArr);
}
public int getIndex(Student[] stuArr){
for(int i=0;i<stuArr.length;i++){
if(stuArr[i]==null){
return i;
}
}
return 99999;
}
public void show(Student[] stuArr){
for(int i=0;i<stuArr.length;i++){
System.out.println(stuArr[i].toString());
}
}

}

import java.util.*;

public class Test {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner in=new Scanner(System.in);
    System.out.println("------请输入学生人数------");
    int studentNum=in.nextInt();
    Admin admin=new Admin();
    Student[] stuArr=new Student[studentNum];
    int c=0;
    do{
        System.out.println("请选择你要执行的功能:");
        System.out.println("10:添加一个学生");
        System.out.println("11:查找一个学生");
        System.out.println("12:根据学生编号跟新学生信息");
        System.out.println("13:根据学生编号删除学生");
        System.out.println("14:根据编号输入学生各门成绩");
        System.out.println("15:根据某门成绩进行排序");
        System.out.println("16:根据总分进行排序");
        System.out.println("99:退出系统");
        c=in.nextInt();
        if(c==10){
            System.out.println("请输入学生姓名:");
            String name=in.next();
            System.out.println("请输入学生年龄:");
            int age=in.nextInt();
            admin.addStudent(name,age,stuArr);
        }
    }while(c!=99);
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • caozhy 回答这么多问题就耍赖把我的积分一笔勾销了 3年前

    public void show(Student[] stuArr){
    for(int i=0;i<stuArr.length;i++){
    if (stuArr[i] != null) //加上
    System.out.println(stuArr[i].toString());
    }

    如果问题解决,麻烦点下我回答右边的采纳,谢谢。

    点赞 3 评论 复制链接分享
  • weixin_35221883 geng_027 3年前

    if(stuArr!= null && stuArr.length > 0){

    }
    
    点赞 评论 复制链接分享

为你推荐