I've been attempting to create a basic odd/even shift cypher and so far I have no visible errors in my code before I run it however after I try to run it I'm told that there's a nullpointer exception.
Exception in thread "main" java.lang.NullPointerException
at Encryption.cypher(Encryption.java:18)
at EncryptionDemo.main(EncryptionDemo.java:10)
Supposedly it's because I haven't initialized some variable or another however I believe I have already. Here's my code and thanks for any advice given.
import java.util.Scanner;
public class Encryption {
protected int shift = 3;
protected int shift2 = 5;
protected char c = 'a';
protected String ms;
protected int len;
protected void InputMessage() {
Scanner kb = new Scanner(System.in);
System.out.println("Enter your plaintext.");
String ms = kb.nextLine();
}
protected String cypher() {
**int len = ms.length();**
for (len = 0; len < ms.length(); len++) {
c = (char) (ms.charAt(len));
if ((boolean) (ms.charAt(len) % 2 == 0)) {
c = (char) (ms.charAt(len + shift));
} else {
c = (char) (ms.charAt(len + shift2));
}
c = (char) ms.charAt(len);
}
return ms;
}
protected String decypher() {
int len = ms.length();
for (len = 0; len < ms.length(); len++) {
c = (char) (ms.charAt(len));
if ((boolean) (ms.charAt(len) % 2 == 0)) {
c = (char) (ms.charAt(len - shift));
} else {
c = (char) (ms.charAt(len - shift2));
}
c = (char) ms.charAt(len);
}
return ms;
}
protected void output() {
System.out.println("" + (ms));
}
}
import java.util.Scanner;
public class EncryptionDemo {
public static void main(String[] args) {
char[] array = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz_"
.toCharArray();
Encryption message = new Encryption();
message.InputMessage();
**message.cypher();**
message.output();
}
}