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.....

Edited 3 Years Ago by Nick Evan: Fixed formatting

Your code is unformatted and you failed to indicate where the error is. Reade the error messages that you get. Don't expect others to do it for you:

Exception in thread "main" java.lang.NullPointerException
at Bank.main(Bank.java:25)

At the file Bank.java line 25 you got a NullPointerException. You are trying to use at that line something which is null. Make sure that at the line all the objects are initialized and have value.

What line is it at your code.

Gonna fix it for you... If you can't even spot such a simple error... What were you saying about some "banking purposes"? Looks like a piece of rubbish. Not organised. Variables aren't correctly named.

Edited 6 Years Ago by nickguletskii: n/a

import java.util.*;
import java.io.*;

class test {
	public 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;
		test b[] = new test[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 test();
				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");
	}
}

Fixed it for you.

@nickguletskii
How are you helping the OP learn programming by solving his problem instead of teaching him how to solve his problems?
Can you take the time to describe to the OP how you found the problems? And give the OP some hints on how he can do it next time?
Or do you want to be there to solve his next problem?

I don't find that a mistake... If my memory isn't completely fried, it was a simple semicolon error - no semicolon needed after for, e.g.:

for (int i=0;i<10;i++);{
}

EDIT: Also made

static int x = 0;

public. Don't forget to check that!

A simple recommendation: get Eclipse and forget about silly mistakes.

Edited 6 Years Ago by nickguletskii: n/a

This question has already been answered. Start a new discussion instead.