1 Replies - 766 Views - Last Post: 08 October 2012 - 05:48 PM Rate Topic: -----

#1 NiftyClown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-October 12

Encryption program in Java - Null pointer exception.

Posted 08 October 2012 - 04:18 PM

Hi there.

I'm somewhat of a beginner to Java Programming and looking for a fer tips and pointers on the forum on how to solve the issue I have with my program.

It, as the title suggests, is an encryption program. It uses two arrays of the alphabet, shuffles one, the assigns each letter from the original to the shuffled one using HashMaps.

However, I keep receiving a NullPointerException error whenever I run the program. I've had a look around and done a little research and I think it's something to do with my use of the Character class? I'm not entirely sure, I think I might be mixing primitive values up etc.

Here is my code:

 
import java.util.Arrays;
import java.util.HashMap;
import java.util.Collections;

public class Encryptor {
	
	// The size of the alphabet and what is contained within the alphabet in an array
	public static final int AlphaSize = 52;
	public static final Character[] alpha = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
		'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };
	public static final Character[] EncAlpha = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
		'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };
	
	/* Hashmaps which will hold the value of the encrypted and original values of the alphabet. */
	protected HashMap<Character, Character> encrypt;
	protected HashMap<Character, Character> decrypt;
	
	
	/** A constructor which initialises the encryption and decryption HashMaps */
	public Encryptor()
	{
		// Shuffles the array.
		Collections.shuffle(Arrays.asList(EncAlpha));
		
		// The initialization of the hashmaps.
		for (int i=0; i < AlphaSize; i++)
		{
			encrypt.put(alpha[i],EncAlpha[i]);
			decrypt.put(EncAlpha[i], alpha[i]);
		}

	}
	
	/** The encryption method */
	public String encrypt(String s)
	{
		char[]mess = s.toCharArray(); 
			for (int i=0; i < mess.length; i++)
			{
				mess[i] = encrypt.get(i); // Encrypts the letter
			}
	return new String(mess);
	}
	
	/** The decryption method */
	public String decrypt(String s)
	{
		char[]mess = s.toCharArray();
			for (int i=0; i < mess.length; i++)
				{
					mess[i] = decrypt.get(i); // Decrypts the letter
				}
	return new String(mess);
	}
	
}




And the error message I received is:

Exception in thread "main" java.lang.NullPointerException
at Encryptor.<init>(Encryptor.java:47)
at Test.main(Test.java:14)

I'd really appreciate any tips or pointers on how to improve or solve the issue I've been having!

Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Encryption program in Java - Null pointer exception.

#2 ihatesegfault  Icon User is offline

  • New D.I.C Head

Reputation: 17
  • View blog
  • Posts: 47
  • Joined: 24-September 12

Re: Encryption program in Java - Null pointer exception.

Posted 08 October 2012 - 05:48 PM

Look at lines 40 and 51. The problem is that you are giving encrypt.get() the current index of the char[]. HashMap.get() takes an Object, not an index, and returns the corresponding Object. so you should do mess[i]=encrypt.get(mess[i]); The reason for the NullPointerException is that 0 or 1 or any integer is not in the "domain" of the hashmap.

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1