0

I am trying to run the following HillCipher program but it is terminated after the line of conversion of char array to int array and after compilation of that code it shows me null pointer exception.It works fine if I replace int array with int variable,but I need int array in this code to encrypt the data:

        try{
            do//key
            {
                System.out.println("Enter Key of length 4 character : ");
                sKey=(new BufferedReader(new InputStreamReader(System.in))).readLine();
                cKey=new char[2][2];
            }while(!checkKey());
        }
        catch(Exception e)
        {}
    }
    boolean checkKey()
   {
       boolean flag=true;
       if(sKey.length()!=4)
            flag=false;
        int k=0;
        int temp;
        for(int i=0;i<2;i++)
        {
            for(int j=0;j<2;j++)
            {
                cKey[i][j]=sKey.charAt(k);
                k++;
                iKey[i][j]=(int)cKey[i][j]; //program is terminated after this line
                iKey[i][j]-=97;
                if(cKey[i][j]<97 || cKey[i][j]>122)
                {
                    flag=false;
                    break;
                }
            }
            if(flag==false)
            {
                System.out.println("flag: "+flag);
                break;
            }
        }

        int d;
        if((d=iKey[0][0]*iKey[1][1]-iKey[1][0]*iKey[0][1])==0)
            flag=false;
        if(flag==false)
            System.out.println("Invalid Key!! ");
        else
             keygen(d);
        return flag;
    }
    void keygen(int d)
    {
        if (d<0)
            d*=-1;
        for(int i=0;i<2;i++)
        {
            for(int j=0;j<2;j++)
            {
                if(i==0 && j==0)
                    iDKey[i][j]=iKey[1][1]/d;
                else if(i==1 && j==1)
                    iDKey[i][j]=iKey[0][0]/d;
                else
                    iDKey[i][j]=-iKey[i][j]/d;
            }
        }
     }

    String encrypt()
    {
        int l;
        if(sPlainTxt.length()%2==0)
            l=sPlainTxt.length();
        else
            l=sPlainTxt.length()+1;
        int temp1,temp2,ans;
        for(int i=0;i<l;i+=2)
        {
            temp1=(int)cPlainTxt[i]-97;
            temp2=(int)cPlainTxt[i+1]-97;
            ans=iKey[0][0]*temp1+iKey[0][1]*temp2;
            System.out.println(ans);
            ans%=26;            
            ans+=65;
            cCipherTxt[i]=(char)ans;
            cCipherTxt[i+1]=(char)((iKey[1][0]*temp1+iKey[1][1]*temp2)%26+65);
        }
        sCipherTxt=new String(cCipherTxt);
        return sCipherTxt;
    }

}
2
  • 1
    You've given a lot of code. In future, please construct a short but complete program demonstrating the problem. Commented Sep 29, 2013 at 7:09
  • I'd rather have the full code and the line were the exception occurs, that makes debugging a lot easier. The class is obvisouly truncated at the beginning. Commented Sep 29, 2013 at 7:30

1 Answer 1

3

You never assign a value to iKey, so it has its initial default value of null - it's as simple as that. You need to create a new array, e.g.

// Given that you've hard-coded the length of cKey as well...
iKey = new int[2][2];

I'd also strongly urge you not to catch exceptions like this:

catch(Exception e)
{}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.