java – Create an object from another class


I have the Endereco class and the Cliente class, I build 2 constructors for the Cliente , and in one of them, the name of the client and an object of the Endereco type can be inserted, which works perfectly, but in the other constructor it is to be inserted the name of the Cliente and create an object of type Endereco together, the problem is that after typing the data of the Endereco , I give inspect and Endereco is considered null .

Essential part (2 constructors) of the Address class:

public class Endereco{
    private String logradouro;
    private String numero;
    private String complemento;
    private String telefone;
    private String celular;
    private String email;

    public Endereco(String log, String num, String comp, String tel, String cel, String mail){
        logradouro = log;
        numero = num;
        complemento = comp;
        telefone = tel;
        celular = cel;
        email = mail;
    public Endereco(){
        Teclado t = new Teclado();
        logradouro = t.leString("Informe o logradouro: ");
        numero = t.leString("Informe o numero: ");
        complemento = t.leString("Informe o complemento: ");
        telefone = t.leString("Informe o telefone: ");
        celular = t.leString("Informe o celular: ");
        email = t.leString("Informe o e-mail: ");

Essential part of the Customer class:

public class Cliente{

    private String nome;
    private Endereco endereco;
    private int pontos;

    public Cliente(String nm, Endereco ed){
        nome = nm;
        endereco = ed;
        pontos = 0;
    public Cliente(String nm){
        nome = nm;
        Endereco e = new Endereco();



Endereco e = new Endereco();

Do it

this.endereco = new Endereco();

You forgot to assign the new instance to the class attribute.

Scroll to Top