英文:
Exception in thread "main" java.lang.NullPointerException when calling from different class
问题
Class 1:
public class CurrencyConverter {
double[] exchangeRates;
void setExchangeRates(double[] rates) {
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);
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);
}
}
英文:
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);
}
}
答案1
得分: 1
你需要调用 setExchangeRates
方法;否则,double[]
将为 null。
CurrencyConverter cc = new CurrencyConverter();
{
cc.setExchangeRates(new double[]{65.0, 3.0, 3.0, 595.5, 18.0, 107.0, 2.0});
}
英文:
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});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论