I am running into an odd problem. I am still new to coding so this may stem from my misunderstanding the nature of arrays. My problem is this: I am attempting to write unit tests for some arrays in my application in which I call arr.length which then throws an NPE. The arrays are initialized in the constructor of the class so that's not the problem. I have tried initializing them with values at each index to see if that changed anything but it does not. What is confusing me the most is that when I call arr.length on the same array when just running the application I get a value.
Is there something about JUnit I'm missing?
The method being tested is incomplete. I was in the middle of writing it when I started encountering the NPE so I never actually got to the code that assigns the values to the array. Here is the test and the class being tested:
@Test
void testCFArray() throws IOException {
WebReaderFilter adder = new WebReaderFilter();
Stock stock = new Stock("INTC");
stock.setProfile(adder.getStockDetails(stock.getTicker()));
// This method call throws the NPE
stock.setArrays(stock.getProfile());
BigInteger[] arr = stock.getCashFlowArray();
assertEquals(BigInteger.valueOf(33145000000L), arr[0]);
}
package companyValueModel;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
public class Stock {
private String ticker;
private BigDecimal currentPrice;
private BigInteger[] freeCashFlow;
private BigInteger[] cashFlow;
private BigInteger[] debt;
private BigDecimal[] divPerShare;
/*
* Keys: [companyName], [EBITDA], [Enterprise Value], [description], [industry],
* [yearHigh], [price], [Total shareholders equity], [Goodwill and Intangible
* Assets], [Capital Expenditure], [sector], [yearLow], [marketCap], [Dividend
* payments], [ticker], [Revenue Growth], [Cash and short-term investments],
* [Net Income], [Revenue]
*
* Array keys: [Long-term debt-1], [Free Cash Flow0], [Long-term debt-2],
* [Long-term debt0], [Free Cash Flow-2], [Dividend per Share0], [Free Cash
* Flow-1], [Dividend per Share-1], [Operating Cash Flow0], [Operating Cash
* Flow-1], [Operating Cash Flow-2]
*
* keys with numbers at the end represent (0) = this year, (-1) = year prior,
* etc.
*/
private HashMap<String, String> profile;
public Stock(String ticker) {
this.ticker = ticker;
}
public Stock(HashMap<String, String> profile) {
this.profile = profile;
this.ticker = profile.get("ticker");
freeCashFlow = new BigInteger[3];
cashFlow = new BigInteger[3];
debt = new BigInteger[3];
divPerShare = new BigDecimal[2];
}
public HashMap<String, String> getProfile() {
return profile;
}
public void setProfile(HashMap<String, String> profile) {
this.profile = profile;
}
public String getTicker() {
return ticker;
}
public void setCurrentPrice(BigDecimal price) {
currentPrice = price;
}
public BigDecimal getCurrentPrice() {
return currentPrice;
}
public void setArrays(HashMap<String, String> profile) throws NumberFormatException {
int j = 0;
// this line throws the NPE. It disappears if I replace cashFlow.length with 3
for (int i = 0; i < cashFlow.length; i++) {
// This line was to verify that the key existed (it does)
System.out.println(profile.get("Operating Cash Flow" + j));
// also as an aside if anybody could tell me whether I'm parsing this string properly to bigInt I would appreciate it.
double flow = Double.parseDouble(profile.get("Operating Cash Flow" + j));
BigInteger cf = BigInteger.valueOf((long) flow);
j--;
// Here is where the assigning code would have gone
}
}
public BigInteger[] getCashFlowArray() {
return cashFlow;
}
public BigInteger[] getFreeCashFlowArray() {
return freeCashFlow;
}
public BigInteger[] getDebtArray() {
return debt;
}
public BigDecimal[] getDivPerShare() {
return divPerShare;
}
}
It has occurred to me that I could write a similar method with constants for the unit test and use another for the program but that would leave that method without a proper unit test which does not tickle my fancy. In the end the arrays may not be necessary but I think they be convenient later. I'm asking to further my understanding so that if I run into this issue again I know what needs to be done.