java.lang.nullPointerException Error

-1

I'm doing a Java exercise and it requires me to create a note of the product sold, containing random numbers plus the first three letters of the customer as product code, but when I try to generate the winning note this error .

public Compra(){
     String s = cliente.getNome();
     String sub = s.substring(0, 4);
     String num = String.valueOf((int)(Math.random()*10000+100));
     numero = num + sub;
}

EDIT Class Purchase Complete

public class Compra{
    private String numero;
    private Cliente cliente;
    private Vendedor vendedor;
    private double precoOriginal;
    private double desconto;
    private double preco;

    public Compra(double pO, double ds, double pF, Cliente client){        

        String s = cliente.getNome();
        String sub = s.substring(0, 4);
        String num = String.valueOf((int)(Math.random()*10000+100));
        numero = num + sub;

        precoOriginal = pO;
        desconto = ds;
        preco = pF;
        cliente = client;
    }
    public Compra(double prO, double dst, double prF, Cliente client, Vendedor vend){

        precoOriginal = prO;
        desconto = dst;
        preco = prF;
        cliente = client;
        vendedor = vend;
    }
    // CLASSE PARA TESTAR O NUMERO(CODIGO)
    public Compra(){
        String s = cliente.getNome();
        String sub = s.substring(0, 4);
        String num = String.valueOf((int)(Math.random()*10000+100));
        numero = num + sub;
    }

    public void adDesconto(double desct){
        preco = preco - (preco * desct);

    }
    public void setVendedor(Vendedor vend){
        vendedor = vend;
    }
}
    
asked by anonymous 12.04.2014 / 02:19

2 answers

2

Solution:

public class Compra{
    private String numero;
    private Cliente cliente;
    private Vendedor vendedor;
    private double precoOriginal;
    private double desconto;
    private double preco;

    public Compra(double pO, double ds, double pF, Cliente client){        
        cliente = client;
        String s = cliente.getNome();
        String sub = s.substring(0, 4);
        String num = String.valueOf((int)(Math.random()*10000+100));
        numero = num + sub;

        precoOriginal = pO;
        desconto = ds;
        preco = pF;
        cliente = client;
    }
    public Compra(double prO, double dst, double prF, Cliente client, Vendedor vend){
        cliente = client;
        vendedor = vend;
        precoOriginal = prO;
        desconto = dst;
        preco = prF;
        cliente = client;
        vendedor = vend;
    }
    // CLASSE PARA TESTAR O NUMERO(CODIGO)
    public Compra(){
        cliente = new Cliente();            
           String s = cliente.getNome();
        if (s != null){  
           String sub = s.substring(0, 4);
        }
        String num = String.valueOf((int)(Math.random()*10000+100));
        numero = num + sub;
    }

    public void adDesconto(double desct){
        preco = preco - (preco * desct);

    }
    public void setVendedor(Vendedor vend){
        vendedor = vend;
    }
}
    
12.04.2014 / 02:46
1

Missing you instantiate the seller classes, since only the reference to the object and not the object itself it still does not exist until you instantiate it with the new command.
Just do the same as the example

vendedor = new Vendedor();
    
12.04.2014 / 04:34