方法

时间:2015-09-25 03:00:32

标签: java

导致我的错误是什么???

public class Keyboard {
    private Scanner keyboard;

    /**
     * Default contructor
     */
    public void keyboard() {
        keyboard = new Scanner(System.in);
    }

    /**
     * get and return an int
     * @return an int
     */
    public int getInt() {
        int myInt;
        myInt = keyboard.nextInt();
        keyboard.nextLine();

        return myInt;
    }
}

public class TestKeyboard {
    public static void main(String[] args) {
        System.out.println("enter an int: ");
        Keyboard keyboard = new Keyboard();
        keyboard.getInt();
    }
}

我目前遇到以下异常:

  

Keyboard.getInt上的java.lang.NullPointerException(Keyboard.java:27)       在TestKeyboard.main(TestKeyboard.java:6)

1 个答案:

答案 0 :(得分:0)

没有默认构造函数。构造函数应为public KeyBoard()。 Keyboard类有一个keyboard()方法,您正在考虑将其作为构造函数。