【问题标题】:Error: Exception in thread "main" java.lang.NullPointerException错误:线程“主”java.lang.NullPointerException 中的异常
【发布时间】:2015-09-30 02:07:00
【问题描述】:

我的代码出现此错误。

这是我的代码:

import java.util.*;
public class car{
public static void main(String[]args) throws java.io.IOException{
Scanner v = new Scanner(System.in);

String model = new String(); 
double cost=0;

System.out.print("Enter model: ");
model = System.console().readLine();

if(model == "GL"){
    cost = 420000;
    }

if (model == "XL"){
    cost = 3398000;
    }






System.out.print("Car phone: ");
char phone = (char)System.in.read();

if(phone == 'W'){
cost = cost + 40000;
}

System.out.print("Full or installment: ");
char paid = (char)System.in.read();

if(paid == 'F'){
cost = cost - 0.15 * cost;
}

System.out.print("Cost: " + cost); 

}
}

这就是结果。一个错误: 输入模型:线程“main”中的异常 java.lang.NullPointerException 在 car.main(car.java:10)

【问题讨论】:

    标签: java exception nullpointerexception main


    【解决方案1】:

    您已经定义了 Scanner 对象。使用 Scanner 对象的实例并设置模型的值。而不是做 System.console() 你可以试试 v.next()

    import java.util.*;
    
    public class car {
        public static void main(String[] args) throws java.io.IOException {
            Scanner v = new Scanner(System.in);
    
            String model = new String();
            double cost = 0;
    
            System.out.print("Enter model: ");
            //model = System.console().readLine();
            model = v.next();
    
            if (model == "GL") {
                cost = 420000;
            }
    
            if (model == "XL") {
                cost = 3398000;
            }
    
            System.out.print("Car phone: ");
            char phone = (char) System.in.read();
    
            if (phone == 'W') {
                cost = cost + 40000;
            }
    
            System.out.print("Full or installment: ");
            char paid = (char) System.in.read();
    
            if (paid == 'F') {
                cost = cost - 0.15 * cost;
            }
    
            System.out.print("Cost: " + cost);
    
        }
    }
    

    输出:

    Enter model: GL
    Car phone: W
    Full or installment: Cost: 40000.0
    

    【讨论】:

    • 非常感谢!模型 = v.next();工作。但我没有得到我想要的输出,但我现在可以处理它。谢谢谢谢!
    【解决方案2】:

    问题出在以下行:

    model = System.console().readLine(); 
    

    当您调用 readLine() 时,System.console() 为 null - 因此您会收到 NullPointerException。您需要做的就是使用 Scanner.nextLine() 方法,即将这一行替换为:

    model = v.nextLine();
    

    【讨论】:

      【解决方案3】:

      好像这个是空的:

      System.console()
      

      所以在它上面调用 readLine() 意味着在 null 上调用一个方法。

      您可能希望在 System.in 上使用 Scanner 来代替 I/O。

      【讨论】:

        【解决方案4】:

        System.console() 可以返回 null。见javadoc

        当您通过IDE运行java程序时,控制台将不可用,在这种情况下System.console()返回null。当从 termainl 运行 java 程序时,System.console() 不会返回 null

        所以最好检查null

        【讨论】:

        • 答案解释了问题的原因,但可以通过为用户提供一些关于如何修复它的指导来扩展......
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-06
        • 1970-01-01
        • 2012-08-30
        • 1970-01-01
        • 2013-08-25
        • 1970-01-01
        相关资源
        最近更新 更多