I have a null error in my Decrypt program

This is code for all three of my classes--

Page 1 of 1

5 Replies - 2074 Views - Last Post: 13 August 2008 - 08:07 PM Rate Topic: -----

#1 qwertyness   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 02-August 08

I have a null error in my Decrypt program

Post icon  Posted 12 August 2008 - 05:33 PM

public class Encrypt
{
	//instance field
	String keyword; //"elvispresley"
	String alphabet;
	String plaintext; //"all your base are belong to us"
	int encrypted[];
	int length;
	public char ch;
	//constructor
   public Encrypt(String k, String p)
	{   //assigning the Strings
		keyword = "elvispresley";
		putKeyword();
		String alphabet = "e l v i s p r y a b c d f g h j k m n o p q r t u w x z";
		buildAlphabet();
		plaintext = "all your base are belong to us";
		encrypted = new int[plaintext.length()];
		encrypt();	  
	}

  
	public void putKeyword()
	{
		StringBuffer alpha = new StringBuffer();
		alpha.append(keyword.charAt(0));
		for(int i = 1; i < keyword.length(); i++ )
		{
			boolean found = false;
			for(int j = 0; j < i; j++)
			{
				if(keyword.charAt(j) == keyword.charAt(i))
				{
					 found = true;			
				}
			}
			if(!found)
			{
				alpha.append(keyword.charAt(i));
			}
			
		}
		alpha.toString();  
	   alphabet = alpha.toString();	  
	}
	 
	public void buildAlphabet()
	{
		StringBuffer alpha = new StringBuffer();
		int len = alpha.length();
		for(char ch = 'a'; ch <= 'z'; ch++)
		{	  
		   boolean found = false;
			for(int j = 0; j < len; j++)
			{
				if(alpha.charAt(j) == ch)
				{
				found = true;
				}
				else
				{
				 alpha.append(keyword.charAt(ch));
				}
				alphabet = alpha.toString();		   
				}
			}
		}
	 
	 public void encrypt()
	 {
		 for(int i = 0; i < plaintext.length(); i++)
		 {
			encrypted[i] = getIndex(plaintext.charAt(i));
		 }
	 }
	 public int getIndex(char ch)
	 {
		 int o = 0;
		 for(int i = 0; i < alphabet.length(); i++)
		 {			

			 if(ch == alphabet.charAt(i))
			 o = i;
		 }
		 return o;
	 }		   
	 public String getAlphabet()
	 {
		 return alphabet;
	 }
	
	 public int[] getEncrypted()
	 {
		 return encrypted;
	 }
		   
	 public void printEncrypted()
	 {
		 final StringBuffer buf = new StringBuffer();
		 buf.append("Array");
		 for (int i = 0; i < length; i++)
			{
			  if (i != 0) buf.append(", ");			 
			   buf.append(encrypted[i]);
			}
	 }
}






public class Decrypt 
{
	String alphabet;
	String plaintext;
	int encrypted[];
	int length;
	
	
	public Decrypt(String a, int[] e, int l)
	{
		a = alphabet;
		encrypted = e;
		length = l;
		decrypt();		
	}
	
	public void decrypt()
	{
		StringBuffer qwerty = new StringBuffer();
		for(int i = 0; i < encrypted.length; i++)
		{
		   qwerty.append(alphabet.charAt(encrypted[i]));  
		}
		
	}
		
	public String getPlaintext()
	{
		return plaintext;
	}
}






/**
 * Driver for Lab 5.
 * 
 * @author Anthony W. Smith
 * @version 6/15/2009
 */
public class Driver
{
	public static void main(String arg[])
	{
		String k = "elvispresley";
		String p = "all your base are belong to us";
		
		Encrypt encrypt = new Encrypt(k, p);
		System.out.println("Alphabet String:");
		String a = encrypt.getAlphabet();
		System.out.println(a);
		System.out.println("Encrypted message:");
		encrypt.printEncrypted();
		int e[] = encrypt.getEncrypted();
		
		Decrypt decrypt = new Decrypt(a, e, p.length());
		System.out.println("\nDecrypted message:");
		System.out.println(decrypt.getPlaintext());
	}
}



In my Decrypt class, the line after I declare my String Buffer object qwerty.

Is This A Good Question/Topic? 0
  • +

Replies To: I have a null error in my Decrypt program

#2 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 550
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: I have a null error in my Decrypt program

Posted 12 August 2008 - 06:07 PM

Umm, what kind of error are you getting?

Some exception has got to be being thrown, or you wouldn't have an error. I'm sure I (or someone else) can help you if you tell us that.
Was This Post Helpful? 0
  • +
  • -

#3 qwertyness   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 02-August 08

Re: I have a null error in my Decrypt program

Posted 12 August 2008 - 06:37 PM

I get a
NullPointerException:
null


I don't know why and I'm pretty sure the program is almost done because Alphabet is finally printing elvispry but not the rest of the alphabet.
Was This Post Helpful? 0
  • +
  • -

#4 qwertyness   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 02-August 08

Re: I have a null error in my Decrypt program

Posted 12 August 2008 - 07:07 PM

By the way this happens when I run my code
Was This Post Helpful? 0
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: I have a null error in my Decrypt program

Posted 13 August 2008 - 04:31 PM

	String alphabet;
	String plaintext;
	int encrypted[];
	int length;
	
	
	public Decrypt(String a, int[] e, int l)
	{
		a = alphabet;
		encrypted = e;
		length = l;
		decrypt();		
	}
	
	public void decrypt()
	{
		StringBuffer qwerty = new StringBuffer();
		for(int i = 0; i < encrypted.length; i++)
		{
		   qwerty.append(alphabet.charAt(encrypted[i]));  
		}
		
	}



And where is "alphabet" initialized ?
Was This Post Helpful? 0
  • +
  • -

#6 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 550
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: I have a null error in my Decrypt program

Posted 13 August 2008 - 08:07 PM

AHA!

In your Decrypt constructor...you need to flip a line around.

public Decrypt(String a, int[] e, int l)
{
    alphabet = a; // flip flop this!
    encrypted = e;
    length = l;
    decrypt();
}


Hope this fixed it! :D

pbl made me figure this out with his last comment...

Quote

where is "alphabet" initialized


I tend to look at the constructor(s) when that happens. :D

This post has been edited by Locke37: 13 August 2008 - 08:08 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1