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

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

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});
}

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:

确定