我总是在 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);
}