About method chaining. Why are you giving nullPointerException?

Asked

Viewed 383 times

3

I got the class Request:

package teste;

import java.util.ArrayList;
import java.util.List;

   public class Pedido {
     List<Item> lista = new ArrayList<Item>();;
     Cliente cliente;

     public Pedido adicionarPedido(int quantidade, String nome){

       lista.add(new Item(nome, quantidade));
       return this;
     }

     public Pedido paraOCliente(String nome){
        Cliente cliente = new Cliente(nome);
        return this;
     }

       public void fechar(){
           System.out.println("Nome " + this.cliente.getNome() + " " +  lista.get(0));
       }
}

The Customer class:

package teste;

   public class Cliente {

     private String nome;

     public String getNome() {
        return nome;
     }

     public void setNome(String nome) {
       this.nome = nome;
     }

     public Cliente(String nome){
       this.nome = nome;
     }
}

The class Item: test package;

   public class Item {

      private String nomeProduto;
      private int quantidade;

      public Item(String nome, int quantidade){
        this.nomeProduto = nome;
        this.quantidade = quantidade;
      }
  }

And the Main class:

package teste;

    public class Teste {

      public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Pedido().adicionarPedido(1, "tênis schutz")
                 .adicionarPedido(2, "iphone 7")
                 .paraOCliente("Aline Gonzaga")
                 .fechar(); 
    }
}

Is giving this problem:

Exception in thread "main" java.lang.NullPointerException
at teste.Pedido.fechar(Pedido.java:21)
at teste.Teste.main(Teste.java:10)

Could someone tell me what the problem is I can’t see where it is...

  • 1

    I see kind of bad eyes this way of using the resource, sounds like abuse of its use.

  • ??????????????????

  • 1

    The @diegofm is absolutely right, my answer speaks of this.

2 answers

5


This is because the variable cliente has not been initialized. The palliative solution must be something like this:

Cliente cliente = new Cliente();

The ideal is that the class Cliente was done differently, but that’s another problem. The class Pedido also allows this kind of thing to happen. Ideally the object is never in invalid state. For this there is the builder.

This method besides not doing what you want, is not suitable, because the object only works if it is called:

public Pedido paraOCliente(String nome){
    Cliente cliente = new Cliente(nome);
    return this;
}

So it works, but it keeps creating a problem of design class:

public Pedido paraOCliente(String nome){
    cliente = new Cliente(nome);
    return this;
}

I put in the Github for future reference.

You are trying to use a pattern that is not suitable for this case. That’s what I always say, follow rules without knowing why. Use the constructor to have a correct object. Letting members initialize in parts is asking to have several problems. If you don’t fix this, the mistake could keep happening. And as it will probably not be tested under faulty conditions (almost no programmer does this, people just want to see it working) the error will only be detected too late, since it will not appear right away.

  • 1

    In case the Cliente It’s over, it had to be cliente = new Cliente(); or this.cliente = new Cliente();

  • 1

    @Sorack I hadn’t finished the answer.

  • Susse, I saw later and gave +1 already ^^

  • You slapped me in the face. Rs. ok

  • @gonz not, but you want to learn or do wrong?

  • I want to learn right..... ok.

Show 1 more comment

4

public Pedido paraOCliente(String nome){
    Cliente cliente = new Cliente(nome);
    return this;
 }

should be

  public Pedido paraOCliente(String nome){
        this.cliente = new Cliente(nome);

    return this;
 }

Browser other questions tagged

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