0

I am trying to call methods of one class in another class but every time I am getting the same Exception in thread "main" java.lang.NullPointerException error...

Please look into the code below and guide me....

Class: 1

public class CurrencyConverter {
    double[] exchangeRates;
    
    void setExchangeRates(double[] rates) { //set instance methods
        exchangeRates = rates;
    }
    
    void updateExchangeRates(int arrayIndex, double newVal) {
        exchangeRates[arrayIndex] = newVal;
    }
    
    double getExchnageRates(int arrayIndex) {
        return exchangeRates[arrayIndex];
    }
    
    double computeTransferAmount(int arrayIndex, double amount) {
        return amount * getExchnageRates(arrayIndex);
    }
    
    void printCurrencies() {
        System.out.println("\nrupee: " + exchangeRates[0]);
        System.out.println("dirham: " + exchangeRates[1]);
        System.out.println("real: " + exchangeRates[2]);
        System.out.println("chilean_peso: " + exchangeRates[3]);
        System.out.println("mexican_peso: " + exchangeRates[4]);
        System.out.println("_yen: " + exchangeRates[5]);
        System.out.println("$australian: " + exchangeRates[exchangeRates.length-1]);
    }
            
    public static void main(String[] args){
        CurrencyConverter cc = new CurrencyConverter();
        double[] rates = {63.0, 3.0, 3.0, 595.5, 18.0, 107.0, 2.0};
        cc.setExchangeRates(rates);     //calling setExchangeRates methods
        rates = new double[]{65.0, 3.0, 3.0, 595.5, 18.0, 107.0, 2.0};
        rates = new double[]{65.0, 3.0, 3.0, 595.5, 18.0, 107.0, 2.0};
        cc.setExchangeRates(rates);
        cc.printCurrencies();
        cc.updateExchangeRates(0, 66.0);
        cc.printCurrencies();
        double amount = cc.computeTransferAmount(0, 1000);
        System.out.println("\nTransferred Amount: " + amount);
    }
}

Class 2 :

public class MoneyTransferService {
    CurrencyConverter cc = new CurrencyConverter();
    
    double computeTransferAmount(int countryIndex, double amount) {
        return cc.computeTransferAmount(countryIndex, amount);
    }
    
    double computeTransferFee(int countryIndex, double amount) {
        double transferAmount =  computeTransferAmount(countryIndex,amount);
        double transferFee = transferAmount * 0.02;
        return transferFee;
    }
    
    public static void main(String[] args) {
        MoneyTransferService transferService = new MoneyTransferService();
        
        double transferAmount = transferService.computeTransferAmount(0, 1000);
        double transferFee = transferService.computeTransferFee(0, 1000);
        System.out.println("transferAmount: " + transferAmount);
        System.out.println("transferFee: " + transferFee);
    }
}
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
MANAS
  • 1
  • 1
  • 1
    You never set `double[] exchangeRates;` to a value in `MoneyTransferService` – JCWasmx86 Aug 05 '20 at 16:28
  • 1
    Can you read stack trace? If you do, can you elaborate on which part of the code that raised `NullPointerException`? – dee cue Aug 05 '20 at 16:29
  • Here is the error: Exception in thread "main" java.lang.NullPointerException at CurrencyConverter.getExchnageRates(CurrencyConverter.java:41) at CurrencyConverter.computeTransferAmount(CurrencyConverter.java:45) at MoneyTransferService.computeTransferAmount(MoneyTransferService.java:6) at MoneyTransferService.main(MoneyTransferService.java:18) – MANAS Aug 05 '20 at 16:39

1 Answers1

1

You need to call setExchangeRates; otherwise, the double[] will be null.

CurrencyConverter cc = new CurrencyConverter();
{
   cc.setExchangeRates(new double[]{65.0, 3.0, 3.0, 595.5, 18.0, 107.0, 2.0});
}
Unmitigated
  • 76,500
  • 11
  • 62
  • 80