Hello guys I am working on a program using the composite pattern .The program contains two buttons "business accounts " and "personal accounts". When I click on the personal account button the program loads up the tree with all the accounts and visa versa when the business account button is clicked. The program runs fine when the personal account button is clicked 1st. But if I try click on the business account button 1st I get a null pointer exception. I think the error is to do with loading the tree , if anyone has any suggestions how to over come this it would be greatly appreciated
public class TheGui extends JFrame implements ActionListener , TreeSelectionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel panel1 , panel2;
private JButton BusinessButton , PersonalButton;
SubAccount Personal, Customer1, Customer2, Customer3 , Customer4 , AccountNo, IntrestRate, AcBalance,
AcSavings, AcCurrent;
SubAccount Business, Client1 ,Client2, Client3;
JScrollPane sp;
JTree tree , tree1;
DefaultMutableTreeNode troot;
DefaultMutableTreeNode troot1;
JLabel Balance;
PersonalAccount Persil = new PersonalAccount();
PersonalAccount2 Persil1 = new PersonalAccount2();
PersonalAccount3 Persil3 = new PersonalAccount3();
PersonalAccount4 Persil4 = new PersonalAccount4();
Account Aco;
public TheGui(){
super("franny");
MakePersonal();
SetUpGui();
}
public void SetUpGui(){
Container contentPane = getContentPane();
panel1 = new JPanel();
panel2 = new JPanel();
PersonalButton = new JButton("Personal Accounts");
panel1.add(new BuisnessComponent(
BusinessButton = new JButton("Business Accounts")));
panel1.add(new PersonalComponent(
PersonalButton = new JButton("Personal Account")));
PersonalButton.addActionListener(this);
panel2.setLayout(new BorderLayout());
sp = new JScrollPane();
panel2.add("Center", sp);
panel2.add("South", Balance = new JLabel(" "));
panel2.setBorder(new BevelBorder(BevelBorder.RAISED));
//troot = new DefaultMutableTreeNode(Leader.getName());
tree= new JTree(troot);
tree1 = new JTree(troot1);
tree.setBackground(Color.lightGray);
/* Put the Tree in a scroller. */
BusinessButton.addActionListener(this);
contentPane.add(panel1, BorderLayout.NORTH);
contentPane.add(panel2, BorderLayout.CENTER);
setSize(400,400);
setVisible( true );
}
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if (obj == PersonalButton)
{
loadTree(Personal);
MakePersonal();
}
if(obj == BusinessButton)
loadTree2(Business);
MakeBusiness();
}
public void loadTree (SubAccount topDog) {
DefaultMutableTreeNode troot;
troot = new DefaultMutableTreeNode(topDog.getName());
panel2.remove(tree);
tree= new JTree(troot);
tree.addTreeSelectionListener(this);
sp.getViewport().add(tree);
addNodes(troot, topDog);
tree.expandRow(0);
repaint();
}
public void loadTree2 (SubAccount topDog2) {
DefaultMutableTreeNode troot1;
troot1 = new DefaultMutableTreeNode(topDog2.getName());
panel2.remove(tree1);
tree1= new JTree(troot1);
tree1.addTreeSelectionListener(this);
sp.getViewport().add(tree1);
addNodes(troot1, topDog2);
tree1.expandRow(0);
repaint();
}
//--------------------------------------
private void addNodes(DefaultMutableTreeNode pnode, SubAccount sup) {
DefaultMutableTreeNode node;
Enumeration sp = sup.subordinates();
if (sp != null) {
while (sp.hasMoreElements()) {
SubAccount newPers = (SubAccount)sp.nextElement();
node = new DefaultMutableTreeNode(newPers.getName());
pnode.add(node);
addNodes(node, newPers);
}
}
}
public void MakePersonal(){
{
Aco = Persil;
Personal = new HeadAccount("Personal Accounts", 200000);
Personal.add(Customer1 = new HeadAccount(Aco.getAccountName().getName() , 11000));
Customer1.add(AccountNo = new HeadAccount("Account Number " + Aco.getAccountNum().getName(), 200));
Customer1.add(IntrestRate = new HeadAccount("Intrest Rate " + Aco.getAccountRate().getName(), 200));
Customer1.add(AcBalance = new HeadAccount(Aco.getAccountBalance().getName(), 200));
AcBalance.add(AcSavings = new HeadAccount(Aco.getAccountSavings().getName(), 0));
AcBalance.add(AcCurrent = new HeadAccount(Aco.getAccountCurrent().getName(), 0));
}
{
Aco = Persil1;
Personal.add(Customer2 = new HeadAccount(Aco.getAccountName().getName() , 11000));
Customer2.add(AccountNo = new HeadAccount("Account Number " + Aco.getAccountNum().getName(), 200));
Customer2.add(IntrestRate = new HeadAccount("Intrest Rate " + Aco.getAccountRate().getName(), 200));
Customer2.add(AcBalance = new HeadAccount(Aco.getAccountBalance().getName(), 200));
AcBalance.add(AcSavings = new HeadAccount(Aco.getAccountSavings().getName(), 0));
AcBalance.add(AcCurrent = new HeadAccount(Aco.getAccountCurrent().getName(), 0));
}
{
Aco = Persil3;
Personal.add(Customer3 = new HeadAccount(Aco.getAccountName().getName() , 11000));
Customer3.add(AccountNo = new HeadAccount("Account Number " + Aco.getAccountNum().getName(), 200));
Customer3.add(IntrestRate = new HeadAccount("Intrest Rate " + Aco.getAccountRate().getName(), 200));
Customer3.add(AcBalance = new HeadAccount(Aco.getAccountBalance().getName(), 200));
AcBalance.add(AcSavings = new HeadAccount(Aco.getAccountSavings().getName(), 0));
AcBalance.add(AcCurrent = new HeadAccount(Aco.getAccountCurrent().getName(), 0));
}
{
Aco = Persil4;
Personal.add(Customer4 = new HeadAccount(Aco.getAccountName().getName() , 11000));
Customer4.add(AccountNo = new HeadAccount("Account Number " + Aco.getAccountNum().getName(), 200));
Customer4.add(IntrestRate = new HeadAccount("Intrest Rate " + Aco.getAccountRate().getName(), 200));
Customer4.add(AcBalance = new HeadAccount(Aco.getAccountBalance().getName(), 200));
AcBalance.add(AcSavings = new HeadAccount(Aco.getAccountSavings().getName(), 0));
AcBalance.add(AcCurrent = new HeadAccount(Aco.getAccountCurrent().getName(), 0));
}
}
public void MakeBusiness(){
Business = new HeadAccount("Business Accounts", 200000);
Business.add(Client1 = new HeadAccount("PHP Degsign LTD", 11000));
Business.add(Client2 = new HeadAccount("Clonsilla SPAR", 3000));
Business.add(Client3 = new HeadAccount("Murphys Public House", 1000));
Client1.add(AccountNo = new HeadAccount("Account Number", 200));
Client2.add(AccountNo = new HeadAccount("Account Number", 200));
Client3.add(AccountNo = new HeadAccount("Account Number", 200));
}
public void valueChanged(TreeSelectionEvent evt) {
TreePath path = evt.getPath();
String selectedTerm = path.getLastPathComponent().toString();
SubAccount sup = Personal.getChild(selectedTerm);
if (sup != null)
Balance.setText(new Float(sup.getCosts()).toString());
}
public static void main(String[]args){
@SuppressWarnings("unused")
TheGui yeee = new TheGui();
}
}