我正在制作一个程序,它将单词加密成某种秘密消息,但我不断收到错误消息。是的,我知道代码很丑……但我是初学者。这是我的主要内容。
public class Main {
public static void main (String args []){
String message;
message = JOptionPane.showInputDialog("Give me something to crypt!");
Crypt secret = new Crypt();
secret.CyptedMessage(message);
}
}
这是我的课。
public class Crypt {
String letter[];
String message;
public Crypt(){
message = "";
letter[0]="A";
letter[1]="B";
letter[2]="C";
letter[3]="D";
letter[4]="E";
letter[5]="F";
letter[6]="G";
letter[7]="H";
letter[8]="I";
letter[9]="J";
letter[10]="K";
letter[11]="L";
letter[12]="M";
letter[13]="N";
letter[14]="O";
letter[15]="P";
letter[16]="Q";
letter[17]="R";
letter[18]="S";
letter[19]="T";
letter[20]="U";
letter[21]="V";
letter[22]="W";
letter[23]="X";
letter[24]="Y";
letter[25]="Z";
letter[26]=" ";
}
int getRandomCrypt(){
//Random number 1 to 25
int x;
x=(int)(1 + Math.random()*25);
return x;
}
int checkLetter(String subMessage){
//Checks letters
boolean b = false;
int i=0;
while (b = false){
if (i == 27){
i=0;
if (subMessage == letter[i])
return i;
else
i++;
}
}
return 0;
}
void CyptedMessage(String message){
String CyptedMessage = null;
String message1;
for (int i = 0; i < message.length(); i++){
int number = checkLetter(message.substring(i, i+1));
message1 = letter[number + getRandomCrypt()];
if (number + getRandomCrypt()>26){
message1 = letter[i-27];
}
CyptedMessage += message1;
}
System.out.print(CyptedMessage);
}
}
当我运行这个时,我得到...
Exception in thread "main" java.lang.NullPointerException
at Crypt.<init>(Crypt.java:9)
at Main.main(Main.java:9)