我有一个带有2个列表的类,它是它的数据成员。我想将这些数据成员作为不同类的对象包括在内。
我收到此错误:
“对象引用未设置为对象的实例。”
请告知什么是正确的方法。
moveOrder() {
this.setState({
descending: !this.state.descending
});
}
render() {
let history = this.state.history;
//alert(history.length);
const current = history[this.state.stepNumber];
const winner = calculateWinner(current.squares);
const location = history[history.length - 1].location;
var descending = false
if(this.state.descending)
{
history = this.state.history.slice().reverse();
}
const moves = history.map((step, move) => {
const desc = move ? 'Go to move #' + move + location[move - 1] : 'Go to game start';
var fontStyle = '';
if(move === this.state.stepNumber){
fontStyle = 'bold';
}else{
fontStyle = 'normal'
}
return (
<li key={move}>
<button style={{fontWeight:fontStyle}} onClick={() => this.jumpTo(move)}>
{desc}
</button>
</li>
);
});
return (
<div className="game">
<div className="game-board">
<Board
squares={current.squares}
onClick={(i) => this.handleClick(i)}
/>
</div>
<div className="game-info">
<div>{status}</div>
<ol>{moves}</ol>
<button className="move-order" onClick={() => this.moveOrder()}>
{'Toggle Move Order'}
</button>
</div>
</div>
);
}
}
在dataPoints类中删除了构造函数后,同样的错误仍然存在。
答案 0 :(得分:6)
您已经创建了dataPoints
数组(根据C#的命名标准应称为DataPoints
),但是您尚未创建dataPoints
对象本身。数组中的元素全部为null,因此为null引用异常。
因此,在for循环中,应使用dataPoints
创建new dataPoints()
对象:
for(int i=0;i<Entries;i++)
{
graphDataPoints[i] = new dataPoints();
}
答案 1 :(得分:3)
您实例化了数组,但没有实例化其中的元素。
for(int i = 0; i < Entries; i++)
{
graphDataPoints[i] = new dataPoints();
// I removed the lines below because they are already initialized in the constructor
// graphDataPoints[i].timeStamps = new List<DateTime>();
// graphDataPoints[i].dataValues = new List<double>();
}