2017-02-21 2 views
0

Ich habe versucht, ein Programm zum Verschlüsseln einer Zeichenfolge mit OOP, aber aus irgendeinem Grund bekomme ich immer diesen Fehler, wenn ich versuche, ein Programm nur eines der Objekte zu erstellen.Unbekannter Fehler bei der Java-Objekterstellung

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) 

Objektklasse:

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

Anwendungsklasse:

public class TestExample { 

public static void main(String[] args) { 

    Cipher12 t = new Cipher12(); 

} 

} 

Kann jemand bitte helfen Sie mir, um zu sehen, was das Problem ist? Und möglicherweise, wie man es repariert?

+0

Mögliche Duplikate von [Was ist eine NullPointerException, und wie kann ich es beheben?] (Http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix -es) –

Antwort

1

Schlüssel1 ist standardmäßig auf Null initialisiert. Bei der Initialisierung von ikey wird key1.length() aufgerufen, wobei key1 null ist und NPE auslöst.

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

Bitte geben Sie einen Konstruktor mit key1 als Parameter, und initialisieren alle Attribute in den Konstruktor.

Verwandte Themen