[英]array.length throwing NullPointerException in junit but not when the application is run
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.我的问题是这样的:我试图在我的应用程序中为一些数组编写单元测试,我在其中调用 arr.length 然后抛出一个 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.最让我困惑的是,当我在运行应用程序时在同一个数组上调用 arr.length 时,我得到了一个值。
Is there something about JUnit I'm missing?有什么关于 JUnit 的东西我想念了吗?
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.当我开始遇到 NPE 时,我正在编写它,所以我实际上从未接触过将值分配给数组的代码。 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 // 这个方法调用会抛出 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.我要求进一步加深我的理解,以便如果我再次遇到这个问题,我知道需要做什么。
The String-argument constructor is used by you, while creating Stock object in test case.在测试用例中创建Stock对象时,您会使用 String-argument 构造函数。
--> Initialize cashFlow in that.. --> 在那初始化现金流..
public Stock(String ticker) {
this.ticker = ticker;
cashFlow = new BigInteger[3]; // add this
}
OR或者
--> Use the other HashMap argument constructor while initializing Stock object in test case. --> 在测试用例中初始化Stock对象时使用另一个 HashMap 参数构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.