|
連結リストを用いて銀行口座のプログラムを書いたので すが、実際に実行するとinsertメソッドのif文の部分 NullPointerExceptionになってしまいます。if文のどの 部分が間違っているのでしょうか。教えていただけませ んか?(getNameのメソッドは他のファイルに作ってあ ります。)
public class AccountList implements AccountListInterface{ private Account customer;//口座 private AccountList next;//次のノードへの参照
public AccountList(){
next = null; }
//挿入処理 public int insert(Account account){ AccountList pre,newnode; pre = new AccountList();
pre = position(account);
if(pre.customer.equals(account.getName())) { return -1; } else{ newnode = new AccountList(); newnode.customer = account; newnode.next = pre.next; pre.next = newnode; } return 0; } //挿入位置の検索処理 private AccountList position(Account account){ AccountList ptr;
ptr = this; while(ptr.next != null){
if(account.getName().compareTo(ptr.next.customer.g etName()) < 0){ return ptr; } else{ ptr = ptr.next; } } return ptr; }
//削除処理 public int remove(String name){ AccountList pre; pre = new AccountList();
pre.customer = find(name); if(pre == null){ return -1; } else{ pre.next = pre.next.next; } return 0; } //ノードの検索処理 public Account find(String name){ AccountList ptr; ptr = this;
while(ptr.next != null){ if(name.compareTo(ptr.next.customer.getName()) == 0){ return ptr.customer; } else{ ptr = ptr.next; } } return null; } }
|