【发布时间】:2013-04-15 05:51:49
【问题描述】:
我总是在 main 方法中的 validateCarPlate(nStr) 行和 if(y.matches(rex)) 行上得到 nullPointerException。我应该如何编辑以删除 nullPointerException?
import javax.swing.*;
import java.lang.Exception;
public class Q2{
public static void main(String[]args){
boolean loop = true;
while(loop){
String nStr = JOptionPane.showInputDialog("Enter car plate number: ");
try{
validateCarPlate(nStr);
}
catch(InvalidCarPlateException e){
}
}
}
public static void validateCarPlate(String y)throws InvalidCarPlateException{
String rex = "[a-zA-Z]{3}[0-9]{1,4}";
if(y.matches(rex)){
computeCheckDigit(y);
}else{
throw new InvalidCarPlateException();
}
}
public static void computeCheckDigit(String x){
char [] arr = new char[x.length()];
for(int i=0; i<x.length();i++){
arr[i] = x.charAt(i);
}
【问题讨论】:
-
computeCheckDigit(y) 在哪里; ?
-
你检查 y 是否为空?
-
在哪一行出现错误
-
请粘贴
computeCheckDigit()方法和异常。 -
对代码块使用一致且符合逻辑的缩进。代码的缩进是为了帮助人们理解程序流程。
标签: java exception null nullpointerexception