Exception in thread "main" java.lang.NullPointerException when calling from different class

huangapple go评论87阅读模式
英文:

Exception in thread "main" java.lang.NullPointerException when calling from different class

问题

Class 1:

  1. public class CurrencyConverter {
  2. double[] exchangeRates;
  3. void setExchangeRates(double[] rates) {
  4. exchangeRates = rates;
  5. }
  6. void updateExchangeRates(int arrayIndex, double newVal) {
  7. exchangeRates[arrayIndex] = newVal;
  8. }
  9. double getExchnageRates(int arrayIndex) {
  10. return exchangeRates[arrayIndex];
  11. }
  12. double computeTransferAmount(int arrayIndex, double amount) {
  13. return amount * getExchnageRates(arrayIndex);
  14. }
  15. void printCurrencies() {
  16. System.out.println("\nrupee: " + exchangeRates[0]);
  17. System.out.println("dirham: " + exchangeRates[1]);
  18. System.out.println("real: " + exchangeRates[2]);
  19. System.out.println("chilean_peso: " + exchangeRates[3]);
  20. System.out.println("mexican_peso: " + exchangeRates[4]);
  21. System.out.println("_yen: " + exchangeRates[5]);
  22. System.out.println("$australian: " + exchangeRates[exchangeRates.length-1]);
  23. }
  24. public static void main(String[] args){
  25. CurrencyConverter cc = new CurrencyConverter();
  26. double[] rates = {63.0, 3.0, 3.0, 595.5, 18.0, 107.0, 2.0};
  27. cc.setExchangeRates(rates);
  28. rates = new double[]{65.0, 3.0, 3.0, 595.5, 18.0, 107.0, 2.0};
  29. rates = new double[]{65.0, 3.0, 3.0, 595.5, 18.0, 107.0, 2.0};
  30. cc.setExchangeRates(rates);
  31. cc.printCurrencies();
  32. cc.updateExchangeRates(0, 66.0);
  33. cc.printCurrencies();
  34. double amount = cc.computeTransferAmount(0, 1000);
  35. System.out.println("\nTransferred Amount: " + amount);
  36. }
  37. }

Class 2:

  1. public class MoneyTransferService {
  2. CurrencyConverter cc = new CurrencyConverter();
  3. double computeTransferAmount(int countryIndex, double amount) {
  4. return cc.computeTransferAmount(countryIndex, amount);
  5. }
  6. double computeTransferFee(int countryIndex, double amount) {
  7. double transferAmount = computeTransferAmount(countryIndex, amount);
  8. double transferFee = transferAmount * 0.02;
  9. return transferFee;
  10. }
  11. public static void main(String[] args) {
  12. MoneyTransferService transferService = new MoneyTransferService();
  13. double transferAmount = transferService.computeTransferAmount(0, 1000);
  14. double transferFee = transferService.computeTransferFee(0, 1000);
  15. System.out.println("transferAmount: " + transferAmount);
  16. System.out.println("transferFee: " + transferFee);
  17. }
  18. }
英文:

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

  1. public class CurrencyConverter {
  2. double[] exchangeRates;
  3. void setExchangeRates(double[] rates) { //set instance methods
  4. exchangeRates = rates;
  5. }
  6. void updateExchangeRates(int arrayIndex, double newVal) {
  7. exchangeRates[arrayIndex] = newVal;
  8. }
  9. double getExchnageRates(int arrayIndex) {
  10. return exchangeRates[arrayIndex];
  11. }
  12. double computeTransferAmount(int arrayIndex, double amount) {
  13. return amount * getExchnageRates(arrayIndex);
  14. }
  15. void printCurrencies() {
  16. System.out.println("\nrupee: " + exchangeRates[0]);
  17. System.out.println("dirham: " + exchangeRates[1]);
  18. System.out.println("real: " + exchangeRates[2]);
  19. System.out.println("chilean_peso: " + exchangeRates[3]);
  20. System.out.println("mexican_peso: " + exchangeRates[4]);
  21. System.out.println("_yen: " + exchangeRates[5]);
  22. System.out.println("$australian: " + exchangeRates[exchangeRates.length-1]);
  23. }
  24. public static void main(String[] args){
  25. CurrencyConverter cc = new CurrencyConverter();
  26. double[] rates = {63.0, 3.0, 3.0, 595.5, 18.0, 107.0, 2.0};
  27. cc.setExchangeRates(rates); //calling setExchangeRates methods
  28. rates = new double[]{65.0, 3.0, 3.0, 595.5, 18.0, 107.0, 2.0};
  29. rates = new double[]{65.0, 3.0, 3.0, 595.5, 18.0, 107.0, 2.0};
  30. cc.setExchangeRates(rates);
  31. cc.printCurrencies();
  32. cc.updateExchangeRates(0, 66.0);
  33. cc.printCurrencies();
  34. double amount = cc.computeTransferAmount(0, 1000);
  35. System.out.println("\nTransferred Amount: " + amount);
  36. }
  37. }

Class 2 :

  1. public class MoneyTransferService {
  2. CurrencyConverter cc = new CurrencyConverter();
  3. double computeTransferAmount(int countryIndex, double amount) {
  4. return cc.computeTransferAmount(countryIndex, amount);
  5. }
  6. double computeTransferFee(int countryIndex, double amount) {
  7. double transferAmount = computeTransferAmount(countryIndex,amount);
  8. double transferFee = transferAmount * 0.02;
  9. return transferFee;
  10. }
  11. public static void main(String[] args) {
  12. MoneyTransferService transferService = new MoneyTransferService();
  13. double transferAmount = transferService.computeTransferAmount(0, 1000);
  14. double transferFee = transferService.computeTransferFee(0, 1000);
  15. System.out.println("transferAmount: " + transferAmount);
  16. System.out.println("transferFee: " + transferFee);
  17. }
  18. }

答案1

得分: 1

你需要调用 setExchangeRates 方法;否则,double[] 将为 null。

  1. CurrencyConverter cc = new CurrencyConverter();
  2. {
  3. cc.setExchangeRates(new double[]{65.0, 3.0, 3.0, 595.5, 18.0, 107.0, 2.0});
  4. }
英文:

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

  1. CurrencyConverter cc = new CurrencyConverter();
  2. {
  3. cc.setExchangeRates(new double[]{65.0, 3.0, 3.0, 595.5, 18.0, 107.0, 2.0});
  4. }

huangapple
  • 本文由 发表于 2020年8月6日 00:26:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63269478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定