-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 ?
– user6026
If it is in this constructor that the error occurs, then there are two possibilities: or
cliente
is void, ors
is void.– luiscubal
line String s = client.getName(); is the source of the error.
– leonardokbruksch
and at the same time blueJ opens the terminal window and sends the following error: java.lang.Nullpointerexception at Purchase.<init>(Purchase.java:31)
– leonardokbruksch
client should be a class without instantiation (
new
)... another location that can happen errors is s.substring first checks if the lenght is>= 4
...– user6026
@user3511983, edit the post and enter the code in full!
– user6026
posted the complete code
– leonardokbruksch
Error:
public Compra(double pO, double ds, double pF, Cliente client)
and has aprivate Cliente cliente
, the namesclient
is different fromcliente
in the local class variable ...– user6026
@Fulvius the error remains the same...
– leonardokbruksch
@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)
– user6026