リンクリストの最後にノードを追加しようとしていますが、else文に到達するとnullポインタ例外が発生し、私が追加している新しいノードにproblem.rear.nextを設定します。何らかの理由で、rear.nextポインタを新しいノードに設定しようとすると、例外がポップアップします。リンクリストの終わりにノードを追加して、後方ポインタと開始ポインタを持つ
明確にするために、
BigIntegerListはまた、一緒に個々の整数を連結することによって大きな整数を表すリストをリンクされたノードのリンクされたリストです。大きな整数リストは、 "開始"と "後方"ノードを定義します。BigIntegerノードは、データの "x"とリストの次のノードへのポインタの "次"を定義します。
はまた、
problem.nがで読み込まれるのBigIntegerの数を表し、この方法は、テキストファイルから読み込み、最初の数は、実際のBigIntegerが続く読み取ることしようとしているどのように多くの大きな整数の状態を読みます@daveが示すように
、私は非常にこだわっていてすべてのアイデアは歓迎します....
BigIntegerList problem;
LinkedList x;
BigIntegerNode curr;
problem = new BigIntegerList();
//get value of first line stating #of bigInts to read in
problem.n = LinkedList.readInteger(in);
//read big ints from text file based on first number passed, n
for(int i=0; i<problem.n;i++)
{
x = new LinkedList();
x.readBigInteger(in);
//case that the list is empty
if(problem.n<1)
{
problem.start = new BigIntegerNode(x,null);
problem.rear = problem.start;
//list is not empty, add nodes to rear
}else
{
curr = new BigIntegerNode(x,null);
problem.rear.next = curr; -----> this is where i get a nullpointer....
problem.rear = curr;
}
}
return problem;
あなたのコードを見ると、 'if(problem.n <1)'は常に 'false'と評価されるようです。したがって、 'else'ブロックは常に' problem.rear'で 'null'のまま実行されます。デバッガでステップアップしてみましたか? – dave