因此,我正在制作一个程序,该程序具有多项式函数,并且可以执行多项操作。
现在,我正在尝试创建一种方法来检查输入的多项式(将其存储为<input
type="number"
id="num_of_players"
name="Number of Players"
min="0" value= {this.props.points} onChange={evt =>
this.props.updateInputValue(evt)}/>
<button
width={`${BUTTON_WIDTH}px`}
fontSize={`${BUTTON_FONTSIZE}px`}
color='red'
onClick= {()=> this.props.handlePoints('increment')}><ImageContainer>
<Image><img src={ plusIconSVG } alt='plus button' /></Image>
</ImageContainer></button>
)并进行压缩,以便将具有相同指数的所有内容加在一起。
每次运行程序时,我都会不断收到ArrayList
,但不确定为什么会收到此消息。我知道错误的含义,但是我检查了一下代码,看不到为什么多项式被视为NullPointer Exception
。
这是我的构造函数
null
在主类中,我的public ArrayList<PolyTerm> terms;
public Poly(ArrayList<PolyTerm> terms)
{
sort(terms);
condenseExponents();
this.terms = terms;
}
对象是这样创建的:
Poly
PolyTerm poly1 = new PolyTerm(4, 3);
PolyTerm poly2 = new PolyTerm(-3, 1);
PolyTerm poly3 = new PolyTerm(15, 3);
PolyTerm poly4 = new PolyTerm(7, 2);
PolyTerm poly5 = new PolyTerm(14, 12);
PolyTerm poly6 = new PolyTerm(2, 0);
PolyTerm poly7 = new PolyTerm(19, 2);
ArrayList<PolyTerm> terms = new ArrayList<>();
terms.add(poly1);
terms.add(poly2);
terms.add(poly3);
terms.add(poly4);
ArrayList<PolyTerm> terms1 = new ArrayList<>();
terms1.add(poly5);
terms1.add(poly6);
terms1.add(poly7);
Poly test = new Poly(terms);
Poly test2 = new Poly(terms1);
接受的第一项是系数,而指数接受的第二项。
回到Poly类。
在我的构造函数中,我调用PolyTerm
,这意在浓缩为condenseExponents()
并合并所有相似指数的系数。这就是方法
ArrayList
在该方法中,private void condenseExponents()
{
for(int i = 0; i < terms.size() - 1; i++)
{
for(int j = i + 1; j < terms.size(); j++)
{
if(terms.get(i).getExponent() == terms.get(j).getExponent())
terms.set(i, new PolyTerm(terms.get(i).getCoef() + terms.get(j).getCoef(), terms.get(i).getExponent()));
terms.remove(j);
}
}
}
一直被扔在NullPointer Exception
的第一行。
有人知道为什么吗?
答案 0 :(得分:0)
在函数调用之前分配术语;
public ArrayList<PolyTerm> terms;
public Poly(ArrayList<PolyTerm> terms)
{
sort(terms);
this.terms = terms;
condenseExponents();
}