0

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();

}
}
Eggwind
  • 1
  • 3
  • 4
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Andy Turner Sep 27 '15 at 22:23
  • Are you intentionally not changing `ms` in your `cypher` and `decypher` methods? You only update `c`. – Andy Turner Sep 27 '15 at 22:29
  • Tip: if you give a stack trace to show the line numbers on which problems occur, it is very helpful to indicate which line those lines are, e.g. `// This is line 18` or `// NPE occurs here!`. – Andy Turner Sep 27 '15 at 22:33
  • Ty I'll get on to marking those lines right now Andy. – Eggwind Sep 28 '15 at 01:25
  • Also Yes the unchanged ms is intentional. I'm just making a very simple Caesar Cypher that'll shift the characters forward or back w=in the message without changing the composition of the message itself. – Eggwind Sep 28 '15 at 01:40

2 Answers2

0

In InputMessage, you declare a local variable called ms and don't use it:

String ms = kb.nextLine();

I presume that you meant to assign kb.nextLine() to the member variable ms. If so, you should write:

ms = kb.nextLine();

You are similarly not using the len member variable; however, you are using the member variable c, but resetting it to a fixed value at the start and end of the for loops: you might as well just declare that as a local variable.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

I see that you're setting a value of ms with

String ms = kb.nextLine();

which doesn't set the class property. This instead creates a new variable in the method scope rather than using the class one. If you remove the type declaration there then that should work properly.

If you want to define the type there as well (which is not necessary) then you need to access the class level property by being explicit:

String this.ms = kb.nextLine();
thenaterhood
  • 167
  • 6