为什么我的“plain1.findPosition(iMsg)”上会出现 NullPointerException?

发布于 2025-01-20 03:07:31 字数 2299 浏览 1 评论 0原文

头等舱给我错误。我要使用字母AZ制作一个数组,然后将用户输入所需的键,并将每个字符在密钥数组中的位置与用户输入的字符串

import java.util.*;
    public class Encrypt{
        private Square plain1;
        private Square plain2;
        private Square Encrypt1;
        private Square Encrypt2;

    public Encryption(String key1, String key2) {
        Square plain1 = new Square();
        Square plain2 = new Square();
        Square Encrypt1= new Square(key1);
        Square Encrypt2= new Square(key2);
    } 


    public String encrypt(String msg) {
        String EmpS = "";
        String STR = "";

        for(int i = 0; i < message.length(); i+=2){
            char iMsg = message.charAt(i);
            char iMsg2 = message.charAt(i+1);
            int[] posRay = plain1.findPosition(iMsg);
            int[] posRay2 = plain2.findPosition(iMsg2);
            String answer = "" + Encrypt1.getChar(posRay[0], posRay2[1]);
            String Combined = "" + answer;
            String answer2 = "" + Encrypt2.getChar(posRay2[0], posRay[1]);
            String Combined2 = "" + answer2;

            String BothCom = Combined + Combined2;
            STR = STR.concat(BothCom);

        return STR;
        }
        return STR;
    } 

2nd类的位置进行比较,该字符串负责该数组

public class Square {
    private char[][] matrix;
public Square() {
        arr= new char[5][5];
        int ascii= 65;
        for (int i = 0; i < 5; i++){
            for(int j = 0; j < 5; j++){
                arr[i][j] = (char) ascii;
                ascii++;
                }
            }
        }
}
public int[] findPosition(char Chart) {
        int[] position= new int[2];
        position[0] = -1;
        popositions[1] = -1;
        for (int i = 0; i < 5; i++){
            for (int j = 0; i < 5; j++){
                if(matrix[i][j] == Chart){
                posistion[0] = i;
                position[1] = j;
                return position;
                }
            }
        }

我将此视为一个错误(其他一切都可以使用),这是什么问题?

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Square.getPos(char)" because "this.plain1" is null
        at findPosition.encrypt(Encrypt.java:45)
        at IO.printResults(IO.java:101)
        at Test.main(Proj8.java:26)

First Class which is giving me the error. I'm to make an array with the letters A-Z, and then have the user input the keys which it takes and compares each character's position in the array of the key with the position of a user inputted string

import java.util.*;
    public class Encrypt{
        private Square plain1;
        private Square plain2;
        private Square Encrypt1;
        private Square Encrypt2;

    public Encryption(String key1, String key2) {
        Square plain1 = new Square();
        Square plain2 = new Square();
        Square Encrypt1= new Square(key1);
        Square Encrypt2= new Square(key2);
    } 


    public String encrypt(String msg) {
        String EmpS = "";
        String STR = "";

        for(int i = 0; i < message.length(); i+=2){
            char iMsg = message.charAt(i);
            char iMsg2 = message.charAt(i+1);
            int[] posRay = plain1.findPosition(iMsg);
            int[] posRay2 = plain2.findPosition(iMsg2);
            String answer = "" + Encrypt1.getChar(posRay[0], posRay2[1]);
            String Combined = "" + answer;
            String answer2 = "" + Encrypt2.getChar(posRay2[0], posRay[1]);
            String Combined2 = "" + answer2;

            String BothCom = Combined + Combined2;
            STR = STR.concat(BothCom);

        return STR;
        }
        return STR;
    } 

2nd class that is responsible for the array

public class Square {
    private char[][] matrix;
public Square() {
        arr= new char[5][5];
        int ascii= 65;
        for (int i = 0; i < 5; i++){
            for(int j = 0; j < 5; j++){
                arr[i][j] = (char) ascii;
                ascii++;
                }
            }
        }
}
public int[] findPosition(char Chart) {
        int[] position= new int[2];
        position[0] = -1;
        popositions[1] = -1;
        for (int i = 0; i < 5; i++){
            for (int j = 0; i < 5; j++){
                if(matrix[i][j] == Chart){
                posistion[0] = i;
                position[1] = j;
                return position;
                }
            }
        }

I'm getting this as an error (everything else works), what's the issue?:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Square.getPos(char)" because "this.plain1" is null
        at findPosition.encrypt(Encrypt.java:45)
        at IO.printResults(IO.java:101)
        at Test.main(Proj8.java:26)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三人与歌 2025-01-27 03:07:31

您不是在分配类变量,而是实际上在方法内分配新的局部变量。更改为:

public Encryption(String key1, String key2) {
        plain1 = new Square();
        plain2 = new Square();
        Encrypt1= new Square(key1);
        Encrypt2= new Square(key2);
    } 

You are not assigning your class variables, you are actually assigning new local variables inside the method. Change to:

public Encryption(String key1, String key2) {
        plain1 = new Square();
        plain2 = new Square();
        Encrypt1= new Square(key1);
        Encrypt2= new Square(key2);
    } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文