java - java中char数组到int数组转换产生空指针异常

标签 java arrays char int

我正在尝试运行以下 HillCipher 程序,但它在将 char 数组转换为 int 数组之后终止,并且在编译该代码后它显示空指针异常。如果我用 int 替换 int 数组,它会正常工作变量,但我需要此代码中的 int 数组来加密数据:

        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;
    }

}

最佳答案

您永远不会为 iKey 分配值,因此它的初始默认值为 null - 就这么简单。您需要创建一个新数组,例如

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

我还强烈敦促您不要捕获这样的异常:

catch(Exception e)
{}

关于java - java中char数组到int数组转换产生空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19075416/

相关文章:

java - 代码可以在 Netbeans 中运行,但不能在 TextPad 中运行?

c++ - 在 C++ 中处理字符(没有 std::string)

php - array_values 递归 php

java - 填充已声明的多维数组

java - 如何在java中将二维整数数组转换为图像?

c++ - Constexpr 转换为 const char[]

c - 如果在 C 中使用 scanf 输入字符,如何打印错误

java - 在 Java 中快速实现端口转发

java - 将所有辅助类合并到一个巨大的类中是个好主意吗?

java - eclipse `allow output folders for source folders`