由于我的代码导致出现此错误。
这是我的代码:
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);
}
}
这就是结果。错误:输入模型:car.main(car.java:10)的线程“ main”中的异常java.lang.NullPointerException
问题在于以下行:
model = System.console().readLine();
在您调用readLine()时,System.console()为null-因此,为什么会收到NullPointerException。您需要做的就是使用Scanner.nextLine()方法,即用以下代码替换此行:
model = v.nextLine();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句