-1
program class
package application;
import java.util.Locale;
import java.util.Scanner;
import model.entities.Account;
import model.exceptions.DomainException;
public class Program {
public static void main(String[] args) {
Locale.setDefault(Locale.US);
Scanner sc = new Scanner(System.in);
try {
System.out.println("Enter account data");
System.out.print("Number: ");
int number = sc.nextInt();
System.out.print("Holder: ");
sc.nextLine();
String holder = sc.nextLine();
System.out.print("Initial balance: ");
double initialBalance = sc.nextDouble();
System.out.print("Witdraw Limit: ");
double withdrawLimit = sc.nextDouble();
Account account = new Account(number, holder, initialBalance, withdrawLimit);
System.out.println();
System.out.print("Enter amount for withdraw: ");
double amount = sc.nextDouble();
account.withdraw(amount);
System.out.println("New balance: " + String.format("%.2f", account.getBalance()));
} catch (DomainException e) {
System.out.println("Witdraw Error: " + e.getMessage());
}
sc.close();
}
}
account class
package model.entities;
import model.exceptions.DomainException;
public class Account {
private Integer number;
private String holder;
private Double balance;
private Double withdrawLimit;
public Account() {
}
public Account(Integer number, String holder, Double initialBalance, Double withdrawLimit) {
this.number = number;
this.holder = holder;
this.withdrawLimit = withdrawLimit;
deposit(initialBalance);
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public String getHolder() {
return holder;
}
public void setHolder(String holder) {
this.holder = holder;
}
public Double getWithdrawLimit() {
return withdrawLimit;
}
public void setWithdrawLimit(Double withdrawLimit) {
this.withdrawLimit = withdrawLimit;
}
public Double getBalance() {
return balance;
}
public void deposit(Double amount) {
balance += amount;
}
public void withdraw(Double amount) throws DomainException{
if (amount > balance) {
throw new DomainException("Not enough balance");
} else if (amount > withdrawLimit) {
throw new DomainException("The amount exceeds withdraw limit");
}
balance -= amount;
}
}
Domainexception class
package model.exceptions;
public class DomainException extends Exception {
private static final long serialVersionUID = 1L;
public DomainException(String msg) {
super(msg);
}
}
Error message:
Exception in thread "main" java.lang.NullPointerException
at model.entities.Account.deposit(Account.java:51)
at model.entities.Account.<init>(Account.java:19)
at application.Program.main(Program.java:27)
I don’t know why you’re making a mistake, since the mistake is about the method deposit()
. The method deposit()
where it is used to deposit the balance
initial instead of "set" this balance
direct. If I take the variable type of the balance
, deposit()
class Double
for the guy double
works.
And why if I did the direct instantiation:
this.number = number;
this.holder = holder;
this.balance = balance;
this.withdrawLimit = withdrawLimit;
}
Instead of:
public Account(Integer number, String holder, Double initialBalance, Double withdrawLimit) {
this.number = number;
this.holder = holder;
this.withdrawLimit = withdrawLimit;
deposit(initialBalance);
}
It would work?
Hello @Rq, welcome to Sopt, you wanted to say instantiation? But from what I saw in your code your problem is more with the call than with instance.
– Ivan Ferrer