java.lang.nullPointerException Error

Asked

Viewed 318 times

-1

I’m doing an exercise in Java and it requires me to create a product note sold, containing random numbers plus the first three letters of the customer as product code, but when I try to generate the note earned that 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 Full purchase

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;
    }
}
  • Where does the error occur, on which line ??? or what part of the code ?

  • If it is in this constructor that the error occurs, then there are two possibilities: or cliente is void, or s is void.

  • line String s = client.getName(); is the source of the error.

  • and at the same time blueJ opens the terminal window and sends the following error: java.lang.Nullpointerexception at Purchase.<init>(Purchase.java:31)

  • client should be a class without instantiation (new)... another location that can happen errors is s.substring first checks if the lenght is >= 4 ...

  • @user3511983, edit the post and enter the code in full!

  • posted the complete code

  • Error: public Compra(double pO, double ds, double pF, Cliente client) and has a private Cliente cliente, the names client is different from cliente in the local class variable ...

  • @Fulvius the error remains the same...

  • @user3511983 I made a slight change in your class, because, you have to go from the constructor into it, and in the constructor without parameter you have to give a new client ... (even so I believe that the logic must be another)

Show 5 more comments

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;
    }
}
  • Thank you @Fulvio , helped me a lot, but now an error appears when compiling, in the client line = new Client(); "No suitable constructor found for Client()

  • This would be my client-class builder, if it helps. public Client(String nm){ this.name = nm; this.address = new Address(); }

  • So since I didn’t see the Client class it must have constructor with parameter so there if you have to put the data ... the Error is because Client(here are parameters), please @user3511983 note this

  • client = new Client("passes here a string value") which can be "" or "your name" or "your value" ... etc ...

  • Okay Fulvio, thank you so much for your help

1

Missing you instantiate the seller classes, because only the reference to the object and not the object itself it still doesn’t exist until you instantiate it with the new command.
Just make the same example

vendedor = new Vendedor();

Browser other questions tagged

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