i create a java prog for simple banking purpose..
but its not working properly...
when we create account through 2nd option,it work as it coded but if go through 1st option it doesnot access created object but create new one....and unable to perform deposit and withdraw function in created account.
import java.util.*;
import java.io.*;
class Bank
{
static int x=0;
int acc;
int bal;
static int v=101;
String nm;
public static void main(String []a)
{
int y=1,t,i;
Bank b[]=new Bank[10];
Scanner cin=new Scanner(System.in);
while(y==1)
{
System.out.println("WELCOME\n\nMENU:\n1.Already have an account\n2.Create new account\n3.Exit\n\nEnter your choice");
int n=cin.nextInt();
switch(n)
{
case 1:System.out.println("Enter your account no");
int aa=cin.nextInt();
int h=x;
for(i=0;i<h;i++);
{
t=b[i].acc;
if(t==aa)
{
System.out.println("WELCOME "+b[i].nm);
System.out.println("your account no:"+b[i].acc);
System.out.println("your balance:"+b[i].bal);
System.out.println("MENU:\n1.Deposit\n2.Withdraw\n3.Exit\n\nEnter your choice:");
int u=cin.nextInt();
switch(u)
{
case 1:System.out.println("Enter amount to deposit:");
int r=cin.nextInt();
b[i].bal+=r;
System.out.println("your update balance :"+b[i].bal);
break;
case 2:System.out.println("Enter amount to withdraw:");
int rr=cin.nextInt();
b[i].bal-=rr;
System.out.println("your update balance :"+b[i].bal);
break;
case 3:System.exit(0);
default:System.out.println("404 ERROR:Wrong choice");
}
}
}
break;
case 2:System.out.println("Enter user name");
b[x]=new Bank();
b[x].nm=cin.nextLine();
System.out.println("Enter initial balance:");
b[x].bal=cin.nextInt();
b[x].acc=v+1;
System.out.println("User name:"+b[x].nm);
System.out.println("your new account no:"+b[x].acc);
System.out.println("your initial balance:"+b[x].bal);
v++;
x++;
break;
case 3:System.exit(0);
default:System.out.println("404 ERROR:Wrong choice");
}
System.out.println("Do you want to continue?(1 for and 0 for no)");
y=cin.nextInt();
}
System.out.println("THANK YOU");
}
}
/////////**************output**************//////////////
C:\Documents and Settings\anushree\Desktop\java prog>javac Ban
C:\Documents and Settings\anushree\Desktop\java prog>java Bank
WELCOME
MENU:
1.Already have an account
2.Create new account
3.Exit
Enter your choice
2
Enter user name:
aaa
Enter initial balance:
234
User name:aaa
your new account no:102
your initial balance:234
Do you want to continue?(1 for and 0 for no)
1
WELCOME
MENU:
1.Already have an account
2.Create new account
3.Exit
Enter your choice
2
Enter user name:
kkk
Enter initial balance:
3454
User name:kkk
your new account no:103
your initial balance:3454
Do you want to continue?(1 for and 0 for no)
1
WELCOME
MENU:
1.Already have an account
2.Create new account
3.Exit
Enter your choice
1
Enter your account no
103
Exception in thread "main" java.lang.NullPointerException
at Bank.main(Bank.java:25)
Can anyone show me my mistake?
thankyou in advance.....