Why is the description variable leaving null?

Asked

Viewed 106 times

0

I don’t understand why the variable descricao not producing the values. In this case, it should go out like this:

Old charger connecting two-pin connector socket

But it’s coming off:

Old charger connecting NULL socket

Follows the class TomadaDeDoisPinos:

package Adapter;

public class TomadaDeDoisPinos implements TomadaDeDoisPinosIF{

    @Override 
    public void conectar(ConectorDeDoisPinos conector) {
        System.out.println(conector.getDescricao());  
    } 
}

Follows the class ConectorDeDoisPinos:

package Adapter;

public class ConectorDeDoisPinos {

    protected String descricao;    

    public String getDescricao(){  
        this.descricao =" conector de dois pinos."; 
        return this.descricao;   
    } 

}

Follows the class CarregadorAntigo:

package Adapter;

public class CarregadorAntigo extends ConectorDeDoisPinos{


    public String getDescricao(){  
        return "Carregador antigo conectando a tomada de "+descricao;   
    }    
} 

Follows the interface TomadaDeDoisPinosIF:

package Adapter;

public interface TomadaDeDoisPinosIF { 

    public void conectar(ConectorDeDoisPinos conector);

} 

Follows the class of Teste:

package Adapter;

public class Teste {

    public static void main(String[] args) {
        ConectorDeDoisPinos cAntigo = new CarregadorAntigo();
        TomadaDeDoisPinos tomadaDeDoisPinos = new TomadaDeDoisPinos();

        tomadaDeDoisPinos.conectar(cAntigo);       

    }   
}
  • With the current code it is not possible to reproduce the problem. I made an adaptation to compile and presented another result. Give us more information so we can help you. http://ideone.com/mTi9PP

  • I put the old Loader class, which was missing

  • You need to initialize the member, how you want to do this?

  • I want the variable of the class Connector of two pins to be initialized in the method getDescription within the class that is, but it is null as it is,

  • Take care of implementation inheritances, especially when you overwrite inherited code: This is a good way to turn your code into a bug-infested spaghetti and clogged with gambiarras. Your code implies that you don’t quite understand how method overwriting works and even when you should or should not apply it, so I recommend studying this topic well. Ah, and by the way, altering fields of objects inside getters is opening a pandora’s box, will only give you trouble and headaches.

1 answer

2


This code seems to be quite wrong. But I don’t know, it can be an initial exercise. Anyway you will be learning in a way that I consider wrong.

To fix the problem presented, simply initialize the class member so that it is not null. The fact that you inherit the structure of one class in another does not mean that you inherit everything that happens to it. Then just change the method of the class that creates the object like this:

class CarregadorAntigo extends ConectorDeDoisPinos {

    @Override 
    public String getDescricao() {  
        this.descricao =" conector de dois pinos."; 
        return "Carregador antigo conectando a tomada de " + descricao;   
    }    
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.