コードのエラーが解けません。
Q&A
Closed
解決したいこと
コードのエラーを解くことができません。
コードのエラーを解こうとして、深みにハマっています。
このコードは
Bank,Branch, Customer の三つのクラスと
Main クラスから構成されています。
AlleyListの学習をしています。
模範回答を見てみたのですが、同じように書けていると思うのです。
何が問題で動かないかわかりません。
もし、なにか気がついかことがあったら教えてください。
発生している問題・エラー
branch Ade has added successfully
Ade
Exception in thread "main" java.lang.NullPointerException
at Main.main(Main.java:10)
該当するソースコード Main Class
public class Main {
public static void main(String[] args) {
Bank bank = new Bank("National Australia Bank");
bank.addBranch("Ade");
bank.showBranches();
System.out.println(bank.findBranch("Ade").getName());
// bank.addCustomer("Ade", "Tim", 50.05);
// bank.listCustomers("Ade", true);
// bank.addCustomer("Ade", "Mike", 175.34);
// bank.addCustomer("Ade", "Percy", 220.12);
//
// bank.addCustomerTransaction("Ade","Tim", 42.22);
// bank.addCustomerTransaction("Ade","Tim", 12.44);
// bank.addCustomerTransaction("Ade","Tim", 1.65);
//
// bank.listCustomers("Ade", true);
//
// bank.addBranch("Sydney");
// bank.addCustomer("Sydney", "Tim", 50.05);
//
//
}
}
該当するソースコード Bank Class
import java.util.ArrayList;
public class Bank {
private String name;
private ArrayList<Branch> branches;
public Bank(String name) {
this.name = name;
this.branches= new ArrayList<Branch>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<Branch> getBranches() {
return branches;
}
public void setBranches(ArrayList<Branch> branches) {
this.branches = branches;
}
public void addBranch(String name) {
if(findBranch(name)==null) {
Branch branch = Branch.creatNewBranch(name);
branches.add(branch);
System.out.println("branch " + name + " has added successfully");
}
}
public Branch findBranch(String bname) {
for(int i =0 ; i>branches.size(); i++ ) {
if(branches.get(i).getName().equals(bname)) {
return branches.get(i);
}
} return null;
}
public boolean listCustomers(String bName ,boolean pTransaction ) {
if(findBranch(bName)!=null) {
System.out.println("Customer list for branch : " + bName);
for(int i =0 ; i < findBranch(bName).getCustomers().size() ; i++) {
System.out.println(findBranch(bName).getCustomers().get(i).getName() + "[" + (i+1) + "]");
if(pTransaction) {
findBranch(bName).getCustomers().get(i).showTransactions();
}
}
return true;
}
return false;
}
public boolean addCustomer(String nBranch, String nCustomer, double iTransaction) {
if(findBranch(nBranch)!= null) {
return findBranch(nBranch).newCustomer(nCustomer, iTransaction);
} else {
System.out.println("The branch could not found on the list");
return false;
}
}
public boolean addCustomerTransaction(String nBranch, String nCustomer, double transaction) {
if(findBranch(nBranch).addCustomerTransaction(nCustomer, transaction)) {
System.out.println("The customer was added successfully");
return true;
}
System.out.println("The transaction was not added");
return false;
}
public void showBranches() {
for(int i=0 ; i <branches.size(); i++ ) {
System.out.println(branches.get(i).getName());
}
}
}
該当するソースコード Branch Class
import java.util.ArrayList;
public class Branch {
private static final String String = null;
private String name;
private ArrayList<Customer> customers;
public Branch(String name) {
this.name = name;
this.customers = new ArrayList<Customer>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<Customer> getCustomers() {
return customers;
}
public void setCustomers(ArrayList<Customer> customers) {
this.customers = customers;
}
public boolean newCustomer(String name, double transaction ) {
Customer newCustomer = findCustomer(name);
if (newCustomer==null) {
customers.add(Customer.creatCustomer(name, transaction));
System.out.println("new customer has added successfully");
return true;
}else {
System.out.println("this customer exisits");
return false;
}
}
private Customer findCustomer(String name) {
for(int i=0 ; i < customers.size() ; i++) {
if(name.equals(customers.get(i).getName())) {
return customers.get(i);
}
} return null;
}
public boolean addCustomerTransaction(String name,double transaction) {
if(findCustomer(name)!=null) {
findCustomer(name).getTransactions().add(transaction);
System.out.println("Transaction was added successfully");
return true;
}
return false;
}
public static Branch creatNewBranch(String name) {
Branch branch = new Branch(name);
return branch;
}
}
該当するソースコード Customer Class
import java.util.ArrayList;
public class Customer {
private String name;
private ArrayList<Double> transactions;
public Customer(String name, double transaction) {
this.name = name;
this.transactions =new ArrayList<Double>();
transactions.add(transaction);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<Double> getTransactions() {
return transactions;
}
public void setTransactions(ArrayList<Double> transactions) {
this.transactions = transactions;
}
public void addTransaction(double transaction) {
transactions.add(transaction);
}
public static Customer creatCustomer(String name, double transaction) {
Customer customer = new Customer(name, transaction);
return customer;
}
public void showTransactions() {
for(int i=0 ; i < transactions.size(); i++) {
System.out.println("Customer name"+ name);
System.out.println("transaction"+ transactions.get(i));
}
}
}
該当するソースコード 模範回答 Bank Class
package com.timbuchalka;
import java.util.ArrayList;
/**
* Created by dev on 4/09/15.
*/
public class Bank {
private String name;
private ArrayList<Branch> branches;
public Bank(String name) {
this.name = name;
this.branches = new ArrayList<Branch>();
}
public boolean addBranch(String branchName) {
if(findBranch(branchName) == null) {
this.branches.add(new Branch(branchName));
return true;
}
return false;
}
public boolean addCustomer(String branchName, String customerName, double initialAmount) {
Branch branch = findBranch(branchName);
if(branch != null) {
return branch.newCustomer(customerName, initialAmount);
}
return false;
}
public boolean addCustomerTransaction(String branchName, String customerName, double amount) {
Branch branch = findBranch(branchName);
if(branch != null) {
return branch.addCustomerTransaction(customerName, amount);
}
return false;
}
private Branch findBranch(String branchName) {
for(int i=0; i<this.branches.size(); i++) {
Branch checkedBranch = this.branches.get(i);
if(checkedBranch.getName().equals(branchName)) {
return checkedBranch;
}
}
return null;
}
public boolean listCustomers(String branchName, boolean showTransactions) {
Branch branch = findBranch(branchName);
if(branch != null) {
System.out.println("Customer details for branch " + branch.getName());
ArrayList<Customer> branchCustomers = branch.getCustomers();
for(int i=0; i<branchCustomers.size(); i++) {
Customer branchCustomer = branchCustomers.get(i);
System.out.println("Customer: " + branchCustomer.getName() + "[" + (i+1) + "]");
if(showTransactions) {
System.out.println("Transactions");
ArrayList<Double> transactions = branchCustomer.getTransactions();
for(int j=0; j<transactions.size(); j++) {
System.out.println("[" + (j+1) + "] Amount " + transactions.get(j));
}
}
}
return true;
} else {
return false;
}
}
}
該当するソースコード 模範回答 Branch Class
package com.timbuchalka;
import java.util.ArrayList;
/**
* Created by dev on 4/09/15.
*/
public class Branch {
private String name;
private ArrayList<Customer> customers;
public Branch(String name) {
this.name = name;
this.customers = new ArrayList<Customer>();
}
public String getName() {
return name;
}
public ArrayList<Customer> getCustomers() {
return customers;
}
public boolean newCustomer(String customerName, double initialAmount) {
if(findCustomer(customerName) == null) {
this.customers.add(new Customer(customerName, initialAmount));
return true;
}
return false;
}
public boolean addCustomerTransaction(String customerName, double amount) {
Customer existingCustomer = findCustomer(customerName);
if(existingCustomer != null) {
existingCustomer.addTransaction(amount);
return true;
}
return false;
}
private Customer findCustomer(String customerName) {
for(int i=0; i<this.customers.size(); i++) {
Customer checkedCustomer = this.customers.get(i);
if(checkedCustomer.getName().equals(customerName)) {
return checkedCustomer;
}
}
return null;
}
}
該当するソースコード 模範回答 Customer Class
package com.timbuchalka;
import java.util.ArrayList;
/**
* Created by dev on 4/09/15.
*/
public class Customer {
private String name;
private ArrayList<Double> transactions;
public Customer(String name, double initialAmount) {
this.name = name;
this.transactions = new ArrayList<Double>();
addTransaction(initialAmount);
}
public void addTransaction(double amount) {
this.transactions.add(amount);
}
public String getName() {
return name;
}
public ArrayList<Double> getTransactions() {
return transactions;
}
}
自分で試したこと
ここに問題・エラーに対して試したことを記載してください。
コードがうまくうごかないので、基礎になるメソッド findBranch()が動くか確認してみました。
ここから、もううまく動きませんが、理由がわかりません。
模範回答もあるので、参照しましたが、ようくわからないのでもしわかる方がいたら教えてください。
0