0
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;

/**
   This class encrypts files using the Caesar cipher.
   For decryption, use an encryptor whose key is the 
   negative of the encryption key.
*/
public class CaesarCipher
{
   private String keyWordString;
   private String removeDuplicates(String word){

    for (int i = 0; i < word.length(); i++) {
        if(!keyWordString.contains(String.valueOf(word.charAt(i)))) {
            keyWordString += String.valueOf(word.charAt(i));
        }
    }
    return(keyWordString);
}

   /**
      Constructs a cipher object with a given key word.
      @param aKey the encryption key
   */
   public CaesarCipher(String aKeyWord)
   {  
      keyWordString = removeDuplicates(keyWordString);
      keyWordString = aKeyWord;
      //create the mapping string
      //keyWordString = removeDuplicates(keyWordString);
      int moreChar = 26 - keyWordString.length();
      char ch = 'Z';
      for(int j=0; j<moreChar; j++)
      {
         keyWordString += ch;
         ch -= 1;
      }
      System.out.println("The mapping string is: " + keyWordString);
   }

   /**
      Encrypts the contents of a stream.
      @param in the input stream
      @param out the output stream
   */      
   public void encryptStream(InputStream in, OutputStream out)
         throws IOException
   {
      boolean done = false;
      while (!done)
      {
         int next = in.read();
         if (next == -1) 
         { 
            done = true; 
         }
         else
         {
            //int encrypted = encrypt(next);
            int encrypted = encryptWordKey(next);
            System.out.println((char)next + " is encrypted to " + (char)encrypted);
            out.write(encrypted);
         }
      }
   }

   /**
      Encrypts a value.
      @param b the value to encrypt (between 0 and 255)
      @return the encrypted value
   */
   public int encryptWordKey(int b)
   {
      int pos = b % 65;
      return keyWordString.charAt(pos);
   }
}

When I am going to run this code it gives me a run time error that says:

Exception in thread "main" java.lang.NullPointerException
    at CaesarCipher.removeDuplicates(CaesarCipher.java:15)
    at CaesarCipher.<init>(CaesarCipher.java:29)
    at CaesarEncryptor.main(CaesarEncryptor.java:29)

Let's say I enter JJJJJJJavvvvvaaaaaaa and I want it to yield Java and give me the encrypted code. In order to differentiate from the other question that was already asked I need the removeDuplicates method to execute and have the output to print JavaZYWW.....etc. Any help or suggestions? I help would be greatly appreciated

user3416645
  • 23
  • 1
  • 8
  • 1
    Hint: such code is **perfect** for doing TDD and JUnit/unittests. You want to first write testcases that specify exactly what you expect some method to "output"; and then you write the production code to "implement" that testcase. That allows you to figure immediately when some code isn't working as expected; and you are just one click away from running a debugger for the failing testcase. – GhostCat Apr 25 '16 at 14:33
  • Oh I didn't realize. But the output should be JavaZYXW......etc by the time it's finished. I tried using the debugger with different placement of the removeDuplicates method but it skipped right over it and did not execute it. – user3416645 Apr 25 '16 at 14:36

1 Answers1

0

You forget to initiate your variable keyWordString.

Do it for example in the static field of your class (or in the constructor).

private String keyWordString="";
Loic Mouchard
  • 1,121
  • 7
  • 22