| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 编程论坛
免费IT实战开发视频教程合集分享千里之行 始于足下
共有 564 人关注过本帖
标题:空指针异常问题
只看楼主 加入收藏
丘山君
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:87
专家分:154
注 册:2017-11-15
结帖率:54.55%
  已结贴   问题点数:20  回复次数:8   
空指针异常问题
小白开始学,代码出了点问题,求帮忙看看,谢谢
程序代码:
package array;

public class Students {
      private String name;
      private long coure;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public long getCoure() {
        return coure;
    }
    public void setCoure(long coure) {
        this.coure = coure;
    }
      public String toString() {
          return this.name;
      }
}


package array;

import java.util.Scanner;

public class A3 {
    public static void main(String[] args) {
        Students c=new Students();
        System.out.println("请输入学生人数:");
        Scanner scan=new Scanner(System.in);
        int num=scan.nextInt();
        System.out.println(num);
         Students arr[]=new Students[num];
         for(int i=0;i<arr.length;i++) {
             System.out.println("请输入第"+(i+1)+"名孩子的姓名");
             String name=scan.next();
             arr[i].setName(name);
             System.out.println("请输入第"+(i+1)+"名孩子的成绩");
             long coures=scan.nextLong();
             arr[i].setCoure(coures);   
         }
         scan.close();
         for(int i=0;i<arr.length;i++) {
             for(int j=i+1;j<arr.length;j++) {
                 if(arr[j].getCoure()>arr[i].getCoure()) {
                     c=arr[j];
                     arr[j]=arr[i];
                     arr[i]=c;
                 }
             }
         }
         System.out.println("按成绩打印如下");
         for(int i=0;i<arr.length;i++)
             System.out.println(arr[i].getName()+"的成绩是"+arr[i].getName());
    }
}

出错信息以下
附件: 您没有浏览附件的权限,请 登录注册
2018-03-04 16:10
丘山君
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:87
专家分:154
注 册:2017-11-15
  得分:0 
哈哈哈,找到问题了,应该再耐心一点的。
2018-03-04 16:17
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:22
帖 子:380
专家分:1662
注 册:2018-2-6
  得分:7 
package com.xiaoa.array;

import java.util.Scanner;

class Students {
    private String name;
    private long coure;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public long getCoure() {
        return coure;
    }

    public void setCoure(long coure) {
        this.coure = coure;
    }

    public String toString() {
        return this.name;
    }
}

public class A3 {
    public static void main(String[] args) {
        Students c = new Students();
        System.out.println("请输入学生人数:");
        Scanner scan = new Scanner(System.in);
        int num = scan.nextInt();
        System.out.println(num);
        Students arr[] = new Students[num];
        
        //要使用数组,之前,必须赋值,否则当然报空指针
        for (int i = 0; i < arr.length; i++) {
            Students s = new Students();
            arr[i] = s;
        }

        for (int i = 0; i < arr.length; i++) {
            System.out.println("请输入第" + (i + 1) + "名孩子的姓名:");
            String name = scan.next();
            arr[i].setName(name);
            System.out.println("请输入第" + (i + 1) + "名孩子的成绩:");
            long coures = scan.nextLong();
            arr[i].setCoure(coures);
        }
        scan.close();
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j].getCoure() > arr[i].getCoure()) {
                    c = arr[j];
                    arr[j] = arr[i];
                    arr[i] = c;
                }
            }
        }
        System.out.println("按成绩打印如下");
        for (int i = 0; i < arr.length; i++)
            System.out.println(arr[i].getName() + "的成绩是" + arr[i].getCoure());
    }
}

假如人生没有梦想,和咸鱼有什么区别!
2018-03-04 18:49
静水且流深
Rank: 5Rank: 5
等 级:贵宾
威 望:11
帖 子:60
专家分:319
注 册:2017-7-7
  得分:7 
程序代码:

package com.huawei.test0023;

public class Students {
    private String name;
    private int score;
    public Students(String name, int score) {
        this.name = name;
        this.score = score;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
   
}

package com.huawei.test0023;

import java.util.Arrays;
import java.util.Comparator;

import javax.swing.JOptionPane;


public class A3 {
    private static String getInput(String info){
        String result = JOptionPane.showInputDialog(null, info);
        return result;
    }
    private static int getIntVal(String info){
        int num = -1;
        try {
            num = Integer.parseInt(info);
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "输入不合法,不能转换为整形数据,请重新输入");
        }
        return num;
    }
   
    public static void main(String[] args) {
        String numStr = getInput("请输入学生人数:");
        int num = getIntVal(numStr);
        while(num<0){
            numStr = getInput("请输入学生人数:");
            num = getIntVal(numStr);
        }
        JOptionPane.showMessageDialog(null, "学生人数:"+num);
        int score = 0;
        Students arr[]=new Students[num];
        for(int i=0;i<arr.length;i++) {
            String name = getInput("请输入第"+(i+1)+"名孩子的姓名");
            String scoreStr = getInput("请输入第"+(i+1)+"名孩子的成绩");
            score = getIntVal(scoreStr);
            while(score<0){
                scoreStr = getInput("请输入第"+(i+1)+"名孩子的成绩");
                score = getIntVal(scoreStr);
            }
            arr[i]=new Students(name, score);
        }
        Arrays.sort(arr, new Comparator<Students>() {
            @Override
            public int compare(Students o1, Students o2) {
                return o1.getScore()-o2.getScore();
            }
        });
        StringBuilder sb = new StringBuilder("按成绩打印如下\n");
        for(int i=0;i<arr.length;i++)
            sb.append(String.format("%s的成绩是%d\n", arr[i].getName(), arr[i].getScore()));
        JOptionPane.showMessageDialog(null, sb.toString());
    }
}

不过是爱情,又能走多久
2018-03-04 20:06
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:22
帖 子:380
专家分:1662
注 册:2018-2-6
  得分:0 
回复 4楼 静水且流深
代码很专业,不过,好像少了姓名的非空校验.
package com.xiaoa.array;

import java.util.Arrays;
import java.util.Comparator;

import javax.swing.JOptionPane;
class Students {
    private String name;
    private int score;
    public Students(String name, int score) {
        this.name = name;
        this.score = score;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
   
}

public class A3 {
    private static String getInput(String info){
        
        String result = JOptionPane.showInputDialog(null, info);
        try{
            String str = result.trim();
            if(!("".equals(str)||null==str))
                return str;
            JOptionPane.showMessageDialog(null, "输入不合法,姓名不能为空,请重新输入");
        }catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    private static int getIntVal(String info){
        int num = -1;
        try {
            num = Integer.parseInt(info);
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "输入不合法,不能转换为整形数据,请重新输入");
        }
        return num;
    }
   
    public static void main(String[] args) {
        String numStr = getInput("请输入学生人数:");
        int num = getIntVal(numStr);
        while(num<0){
            numStr = getInput("请输入学生人数:");
            num = getIntVal(numStr);
        }
        JOptionPane.showMessageDialog(null, "学生人数:"+num);
        int score = 0;
        Students arr[]=new Students[num];
        for(int i=0;i<arr.length;i++) {
            String name = getInput("请输入第"+(i+1)+"名孩子的姓名");
            while("".equals(name)||null==name){
                name = getInput("请输入第"+(i+1)+"名孩子的姓名");
            }
            
            String scoreStr = getInput("请输入第"+(i+1)+"名孩子的成绩");
            score = getIntVal(scoreStr);
            while(score<0){
                scoreStr = getInput("请输入第"+(i+1)+"名孩子的成绩");
                score = getIntVal(scoreStr);
            }
            arr[i]=new Students(name, score);
        }
        Arrays.sort(arr, new Comparator<Students>() {
            @Override
            public int compare(Students o1, Students o2) {
                return o1.getScore()-o2.getScore();
            }
        });
        StringBuilder sb = new StringBuilder("按成绩打印如下\n");
        for(int i=0;i<arr.length;i++)
            sb.append(String.format("%s的成绩是%d\n", arr[i].getName(), arr[i].getScore()));
        JOptionPane.showMessageDialog(null, sb.toString());
    }
}

假如人生没有梦想,和咸鱼有什么区别!
2018-03-04 20:44
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:101
帖 子:1660
专家分:7215
注 册:2015-3-19
  得分:7 
[quote]以下是引用疯狂的小a在2018-3-4 20:44:53的发言:

代码很专业,不过,好像少了姓名的非空校验.

这个null的情况你是怎么造出来的?请解释一下呢

wechat    PrinceThumb
2018-03-05 19:32
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:22
帖 子:380
专家分:1662
注 册:2018-2-6
  得分:0 
回复 6楼 林月儿
加上比较保险,我觉得。比如windows和linux,ios的换行符都不同的。谁知道识别出来的不输入会是什么?

假如人生没有梦想,和咸鱼有什么区别!
2018-03-05 22:34
丘山君
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:87
专家分:154
注 册:2017-11-15
  得分:0 
回复 3楼 疯狂的小a
谢谢
2018-03-05 22:39
丘山君
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:87
专家分:154
注 册:2017-11-15
  得分:0 
回复 4楼 静水且流深
谢谢。
2018-03-05 22:43







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

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