日期:2014-05-20  浏览次数:20955 次

关于java System.in的一个错误?
下面的程序的作用是:给出n个整数,求出这n个整数中前k个最大的整数,比如:5个整数{5.9.1.3.2},前3个最大的整数是:{5.9.3}
然后我的程序出现了这样的错误(控制台输出!):

this is temp constructor!
Input n:
Exception in thread "main" java.lang.NullPointerException
at datastruct.Temp.setN(Temp.java:40)
at datastruct.Temp.<init>(Temp.java:17)
at datastruct.Temp.main(Temp.java:88)

Java code

import java.util.Scanner;

/*
 * 给定n个整数,求出其中k个最大的数
 */
public class Temp {
    private int k;
    private int n;
    private int[] intInputArray;
    private int[] intSomeMaxNumbersArray;
    private Scanner sca;

    public Temp() {
        System.out.println("this is temp constructor!");
        this.setN();
        this.setK();
        this.intInputArray = new int[this.getN()];
        this.intSomeMaxNumbersArray = new int[this.getK()];
        this.initIntInputArray();
        sca = new Scanner(System.in);
    }

    public int getK() {
        return k;
    }

    public void setK() {
        System.out.println("Input k:");
        k = sca.nextInt();
    }

    public int getN() {
        return n;
    }

    public void setN() {
        System.out.println("Input n:");
        n = sca.nextInt();
    }

    private void initIntInputArray() {
        System.out.println("Input IntInputArray:");
        for (int i = 0; i < this.getN(); i++) {
            this.intInputArray[i] = sca.nextInt();
        }
    }

    public void operation() {
        int[] minNumAndIndex = new int[2];
        for (int i = 0; i < this.getN(); i++) {
            if (i < this.getK()) {
                this.intSomeMaxNumbersArray[i] = this.intInputArray[i];
            } else {
                minNumAndIndex = this.getMinNumIndex();
                if (minNumAndIndex[0] < this.intInputArray[i]) {
                    this.intSomeMaxNumbersArray[minNumAndIndex[1]] = this.intInputArray[i];
                }
            }
        }
    }

    private int[] getMinNumIndex() {
        int minIndex;
        int min;
        int[] minNumAndIndex = new int[2];
        min = this.intSomeMaxNumbersArray[0];
        minIndex = 0;
        for (int i = 0; i < this.getK(); i++) {
            if (min >= this.intSomeMaxNumbersArray[i]) {
                min = this.intSomeMaxNumbersArray[i];
                minIndex = i;
            }
        }
        minNumAndIndex[0] = min;
        minNumAndIndex[1] = minIndex;
        return minNumAndIndex;
    }

    private void printResult() {
        for (int i = 0; i < this.getK(); i++) {
            System.out.println(this.intSomeMaxNumbersArray[i] + " ");
        }
    }

    public static void main(String args[]) {
        Temp temp = new Temp();
        temp.operation();
        temp.printResult();
    }
}





------解决方案--------------------
sca = new Scanner(System.in);
这条语句要前移,放在这落后于setN()了。
把它紧跟着放在 System.out.println("this is temp constructor!");
下面。
------解决方案--------------------
出错在:n = sca.nextInt();
这时 sca=null.
sca.netxInt() 出异常了。
------解决方案--------------------
在Temp构造器中调用this.setN时,scan还依然为null。所以在setN方法内用sca.nextInt();会包空指针的错误。
------解决方案--------------------
Java code
this.setN(); //setN用到sca
        this.setK();
        this.intInputArray = new int[this.getN()];
        this.intSomeMaxNumbersArray = new int[this.getK()];
        this.initIntInputArray();
        sca = new Scanner(System.in); //而sca在这里才初始化,所以setN就抛出空指针异常