Null Pointer Exception, because when sending values to the parent class Encryption, then the child classes do not receive them to perform the methods

0

class Encryption {     protected String wordM;     protected String phrase;

public Encriptacion(){
}

public Encriptacion(String palabraM, String frase){
    this.palabraM=palabraM;
    this.frase=frase;
}

public boolean setPalabraM(String palabraM){
    if(palabraM.length()>=4 || palabraM.length()<=6){
        this.palabraM=palabraM;
        return true;
    }else{
        System.out.println("Palabra erronea");
    }
    return false;
}

public String getPalabraM(){
    return palabraM;
}

public void setFrase(String frase){
        this.frase= frase;
}

public String getFrase(){
    return frase;
}   

}

class Encrypt extends Encryption {

private String palabraM;
private String frase;

public Encriptar(){
}

public Encriptar(String palabraM, String frase){
    super(palabraM, frase);
}

public void hacerEncriptacion(){
    int cont = 0;
    char arreglo [][]=null;
    System.out.println(frase.length());
    System.out.println(palabraM.length());

    int div=((int)(frase.length()/palabraM.length()));
    System.out.println(div);

    if(frase.length()%palabraM.length()!=0){
        div++;
    }
    System.out.println(div);

    arreglo = new char [palabraM.length()][div];


    for(int j=0;j<div; j++){
    for(int i=0; i<palabraM.length();i++){

            if(cont<frase.length()){
                arreglo[i][j]=frase.charAt(cont);
            }else{
                arreglo[i][j]='*';
            }
            cont++;
        }


    }

    for(int i=0;i<arreglo.length;i++ ){
        for(int j=0; j<div;j++){
            System.out.print(arreglo[i][j]);
        }
        System.out.println();

    }           
}

}

import java.util.Scanner; class FARGPIA {     public static void main (String args []) {         int option, option2;         String wordM;         String phrase;

    Scanner sc = new Scanner(System.in);
    Encriptacion encriptacion =new Encriptacion();
    Desencriptar desc = new Desencriptar();
    Encriptar enc= new Encriptar();

    do{
    System.out.println("Ingrese la opcion deseada");
    System.out.println("1. Desencriptar");
    System.out.println("2. Encriptar");
    System.out.println("3. Salir");

    opcion = sc.nextInt();

    switch (opcion){

        case 1:
            System.out.println("Ingrese la frase");
            sc.nextLine();
            frase= sc.nextLine();
            encriptacion.setFrase(frase);

            System.out.println();

            do{
                System.out.println("Ingrese la palabra magica");
                palabraM= sc.next();
                encriptacion.setPalabraM(palabraM);

            }while(desc.setPalabraM(palabraM)!=true);
            System.out.println();


            desc.hacerDesencriptacion();

            break;

        case 2:
            System.out.println("Ingrese la frase");
            sc.nextLine();
            frase= sc.nextLine();
            encriptacion.setFrase(frase);

            System.out.println();

            do{
                System.out.println("Ingrese la palabra magica");
                sc.nextLine();
                palabraM= sc.next();

            }while(enc.setPalabraM(palabraM)!=true);
            encriptacion.setPalabraM(palabraM);
            System.out.println();

            enc.hacerEncriptacion();

            break;

        case 3:
            break;
        }       

        System.out.println();
    }while(opcion!=3);
}

}

    
asked by Ali Rdz 09.11.2018 в 21:33
source

1 answer

0

Conceptually, inheritance is not well applied. When you want to use inheritance, you define some attributes in the parent class that the children will inherit, without needing to redefine them in the children. In the case that you expose, I would put the word and phrase attributes in the Encryption class, and then we would put them in the daughter class Encrypt. Regarding the main method, I would make an instance of the daughter class, which by inheritance will inherit the attributes of the parent. The code you have set is not very complete, because I see that a decrypt class is used that is not implemented. I put you here below an approach that compiles, executes and does not generate NPE. I hope it serves you. A greeting.

import java.util.Scanner;
class Encriptacion { protected String palabraM; protected String frase;
public Encriptacion(){
}

public Encriptacion(String palabraM, String frase){
    this.palabraM=palabraM;
    this.frase=frase;
}

public boolean setPalabraM(String palabraM){
    if(palabraM.length()>=4 || palabraM.length()<=6){
        this.palabraM=palabraM;
        return true;
    }else{
        System.out.println("Palabra erronea");
    }
    return false;
}

public String getPalabraM(){
    return palabraM;
}

public void setFrase(String frase){
        this.frase= frase;
}

public String getFrase(){
    return frase;
}   
}
class Encriptar extends Encriptacion{
//private String palabraM;
//private String frase;

public Encriptar(){
}

public Encriptar(String palabraM, String frase){
    super(palabraM, frase);
}

public void hacerEncriptacion(){
    int cont = 0;
    char arreglo [][]=null;
    System.out.println(frase.length());
    System.out.println(palabraM.length());

    int div=((int)(frase.length()/palabraM.length()));
    System.out.println(div);

    if(frase.length()%palabraM.length()!=0){
        div++;
    }
    System.out.println(div);

    arreglo = new char [palabraM.length()][div];


    for(int j=0;j<div; j++){
    for(int i=0; i<palabraM.length();i++){

            if(cont<frase.length()){
                arreglo[i][j]=frase.charAt(cont);
            }else{
                arreglo[i][j]='*';
            }
            cont++;
        }


    }

    for(int i=0;i<arreglo.length;i++ ){
        for(int j=0; j<div;j++){
            System.out.print(arreglo[i][j]);
        }
        System.out.println();

    }           
}
}
class Main{ 
    public static void main(String args []){ 
        int opcion, opcion2; String palabraM; String frase;
    Scanner sc = new Scanner(System.in);
    //Encriptacion enc =new Encriptar();
    //Desencriptar desc = new Desencriptar();
    Encriptar enc= new Encriptar();

    do{
    System.out.println("Ingrese la opcion deseada");
    System.out.println("1. Desencriptar");
    System.out.println("2. Encriptar");
    System.out.println("3. Salir");

    opcion = sc.nextInt();

    switch (opcion){

        case 1:
            System.out.println("Ingrese la frase");
            sc.nextLine();
            frase= sc.nextLine();
            enc.setFrase(frase);

            System.out.println();

           /* do{
                System.out.println("Ingrese la palabra magica");
                palabraM= sc.next();
                encriptacion.setPalabraM(palabraM);

            }while(desc.setPalabraM(palabraM)!=true);*/
            System.out.println();


 //           desc.hacerDesencriptacion();

            break;

        case 2:
            System.out.println("Ingrese la frase");
            sc.nextLine();
            frase= sc.nextLine();
            enc.setFrase(frase);

            System.out.println();

            do{
                System.out.println("Ingrese la palabra magica");
                sc.nextLine();
                palabraM= sc.next();

            }while(enc.setPalabraM(palabraM)!=true);
            enc.setPalabraM(palabraM);
            System.out.println();

            enc.hacerEncriptacion();

            break;

        case 3:
            break;
        }       

        System.out.println();
    }while(opcion!=3);
}
}
    
answered by 09.11.2018 в 23:25