Java博士中的空指针异常

时间:2014-09-18 21:58:13

标签: java pointers exception null

    import java.util.Scanner;
    public class Question_11 {

        public void main(String[] args) {


        double RATE = 35.75;
        double Wages;
        double Hours;

        Scanner keyboard = new Scanner(System.in);


        System.out.print("Please enter the number of hours worked: ");
        Hours = keyboard.nextDouble();

        //calculate wages
        Wages = Hours * RATE;

        System.out.println("The calculated wages = " + Wages);

    }

}

这是我的代码,我不断收到此错误:

java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

它编译但不会运行。

提前谢谢!

1 个答案:

答案 0 :(得分:5)

这是Java博士烦人的非描述性堆栈跟踪,当VM找不到main方法时会发生这种情况。

I.e。:将static关键字添加到main方法,以便应用程序具有有效的入口点

public static void main(String[] args) {