簡體   English   中英

Java對象創建中的未知錯誤

[英]Unknown Error in Java Object Creation

我試圖制作一個使用OOP加密字符串的程序,但是由於某些原因,當我嘗試運行僅創建對象之一的程序時,總是會收到此錯誤。

Exception in thread "main" java.lang.NullPointerException
at encryptionproject.Cipher12.<init>(Cipher12.java:8)
at encryptionproject.TestExample.main(TestExample.java:7)
C:\Users\22849\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

對象類別:

    public class Cipher12 {

public String key1;
public String ciphertext;
public String plaintext;
public int[] ikey = new int[key1.length()];
public int[] ictext = new int[plaintext.length()];
public int[] iptext = new int[plaintext.length()];

public void setKey(String key) {
    this.key1 = key;
}

public void setPlaintext(String plaintext) {
    this.plaintext = plaintext;
}

public void setCiphertext(String ciphertext) {
    this.ciphertext = ciphertext;
}

public String getKey() {
    return key1;
}

public String getCiphertext() {
    return ciphertext;
}

public String getPlaintext() {
    return plaintext;
}

public void convert(String s, int[] i) {
    for (int j = 0; j < s.length(); j++) {
        i[j] = (int) s.charAt(j);
    }
}

public void convert(int[] i, String s) {
    for (int j = 0; j < s.length(); j++) {
        s += (char) i[j];
    }
}

public void encrpty() {
    convert(key1, ikey);
    convert(plaintext, iptext);
    ictext = iptext;
    for (int j = 0; j < ictext.length; j++) {
        ictext[j] -= 31;
        ictext[j] -= ikey[1];
        ictext[j] *= ikey[2];
        ictext[j] += 2 * ikey[3];
        ictext[j] -= (ikey[0] + ikey[1] + ikey[2] + ikey[3]);
        ictext[j] += j * j;
        while (ictext[j] > 95) {
            ictext[j] -= 95;
        }
        ictext[j] += 31;
    }
    convert(ictext, ciphertext);
}

public void decrypt() {
    convert(key1, ikey);
    convert(ciphertext, ictext);
    iptext = ictext;
    for (int j = 0; j < iptext.length; j++) {
        iptext[j] -= 31;
        iptext[j] -= j * j;
        iptext[j] += (ikey[0] + ikey[1] + ikey[2] + ikey[3]);
        iptext[j] -= 2 * ikey[3];
        iptext[j] /= ikey[2];
        iptext[j] += ikey[1];
        while (iptext[j] > 95) {
            iptext[j] -= 95;
        }
        iptext[j] += 31;
    }
    convert(iptext, plaintext);
}
}

應用類別:

public class TestExample {

public static void main(String[] args) {

    Cipher12 t = new Cipher12();

}

}

有人可以幫我看看問題是什么嗎? 以及可能如何解決?

key1默認初始化為null。 初始化ikey時,將調用key1.length() ,其中key1為null,從而key1.length() NPE。

public String key1;
public int[] ikey = new int[key1.length()];

請為構造函數提供key1作為參數,並在構造函數中初始化所有屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM