Solved

Null Pointer Exception Error Here!

Posted on 1998-04-23
4
160 Views
Last Modified: 2012-05-07
Here's my programme, i couldn't find the problem, please compile and try run it!! Thanks !!

import java.io.*;

public class atm_account{

    double account_balance;
    String pin;
    String user_name;
    int telephone_no;
    static final int MAXACCOUNT=20;
    static atm_account user_data[]=new atm_account[20];
    static int num_accounts=3;
   

   public static void main(String args[]) throws IOException
    {

     String user1="Vincent" , user2="Kheng", user3="Dick";
     int tele1=7812020 , tele2=7183385 , tele3=7752231;
     double bal1=6000 , bal2=4000 , bal3=2000;
     String pin1="beoyz" , pin2="kani" , pin3="wfe1215";

     atm_account user_data[]=new atm_account[20];

     atm_account data1=new atm_account(user1,tele1,bal1,pin1);
     atm_account data2=new atm_account(user2,tele2,bal2,pin2);
     atm_account data3=new atm_account(user3,tele3,bal3,pin3);


     user_data[0]=data1;
     user_data[1]=data2;
     user_data[2]=data3;

     m_menu();
    }


     public static void m_menu() throws IOException
     {

     int a=0;
     String option;
     int key_in;

     DataInputStream main=new DataInputStream(System.in);

     System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
     System.out.println("\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»");
     System.out.println("\tº *********  Welcome to BEOYz'z ATM  ********** º");
     System.out.println("\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ");
     System.out.println("\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»");
     System.out.println("\tº Main Menu                                     º");
     System.out.println("\tº ÍÍÍÍÍÍÍÍÍ                                     º");
     System.out.println("\tº                                               º");
     System.out.println("\tº 1. Login to User Account                      º");
     System.out.println("\tº 2. Add New account                            º");
     System.out.println("\tº 3. Terminate the service                      º");
     System.out.println("\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ");
     System.out.println();
     System.out.print("\tPlease Enter Option : ");
     System.out.flush();
     option=main.readLine();
     key_in=Integer.parseInt(option);

       while(key_in > 3 || key_in < 1)
        {
        System.out.print("\tInvalid Option Entered, Please Enter again (1-3): ");
        System.out.flush();
        option=main.readLine();
        key_in=Integer.parseInt(option);
        }

   
     switch(key_in)
     {
     case 1 : System.out.println("\n\n\tLogging in to User Account\n");
              locate_account();
     case 2 : System.out.println("\n\n\tPreparing for Setting up new account");
              is_available();
     case 3 : System.out.println("\tThank You for using BEOYz's ATM");
              System.exit(0);
     }

   }

 



   public atm_account(String username, int teleno, double accountbalance, String pino) throws IOException
   {
   user_name = username;
   telephone_no = teleno;
   account_balance = accountbalance;
   pin = pino;
   }



    //Checking Account Availability
    public static void is_available() throws IOException
    {
     boolean space=add_account();

     if (space == true){
        System.out.println("\tCongratualation ! You have successfully register an account");
        System.out.println("\tEnjoy your transaction !");
        System.out.println("\tTry to Log in to your New Account ");
        locate_account();
        }
     else if (space == false){
        System.out.println("\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»");
        System.out.println("\tº Extremely Sorry ! Our service is currently unavailable º");
        System.out.println("\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ");
        System.out.println("\n");
        System.out.println("\tSorry, Our accounts are currently full.");
        System.out.println("\tThanks for using BEOYz's ATM service, bye!");
        m_menu();}

    }


    //Adding New Account
    public static boolean add_account() throws IOException
    {
    if(num_accounts==MAXACCOUNT)
        return false;


    int i=0;
    String acc_bal;
    String tel_no;
    String usernama="null";
    String pin_no="null";
    double accountbal=0;

    DataInputStream in=new DataInputStream(System.in);

     System.out.println("\n\n\n\n\n\n\n");
     System.out.println("\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»");
     System.out.println("\tº Creating New User Account Section º");
     System.out.println("\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ");
   
    while(i<1)
    {
    System.out.print("\t\nEnter New User Name : ");
    System.out.flush();
    usernama = in.readLine();

      for(int x=0;x<20;x++)
      {
        if((usernama.equals(user_data[x].user_name))){
          System.out.println("\t\n*********** --- Error! --- *************");
          System.out.println("User name has been occupied, Please enter another name\n");
          x=20;}
        else
          i++;
       }
    }

   

    System.out.print("\tEnter Contact Number : ");
    System.out.flush();
    tel_no=in.readLine();
    int tele=Integer.parseInt(tel_no);

    int f=0;
   
    while(f<1){
    System.out.print("\tEnter First Deposit Value : RM");
    System.out.flush();
    acc_bal=in.readLine();
    Double d=new Double(acc_bal);
    accountbal=d.doubleValue();

    if(accountbal<0){
      System.out.println("\t ********** Error ***********");
      System.out.println("\tSorry ! We don't accept null deposit ");
      System.out.println("\tPlease enter an acceptable amount again!");
      }
    else
     f++;
    }



    int j=0;
    while(j<1)
    {
    System.out.print("\tEnter User preferred Personal Identification Number(PIN) : ");
    System.out.flush();
    pin_no=in.readLine();

       for(int y=0;y<20;y++)
       {
        if((pin_no.equals(user_data[y].pin))){
          System.out.println("PIN has been used, Please Enter another one");
          y=20;}
        else
          j++;
       }
    }


    atm_account new_user=new atm_account(usernama, tele, accountbal, pin_no);
    user_data[num_accounts]=new_user;
    num_accounts++;
    return true;
   }



    //Matching User Name
    private static int find_name(String name)
    {
    int i = 0;
      while (i < num_accounts)
      {
      if((name.equals(user_data[i].user_name)))
        return i;
        i++;
      }
      return -1;
    }



    //Locate an account
    public static void locate_account() throws IOException
    {
     String usernam;

    DataInputStream in2=new DataInputStream(System.in);

    System.out.println("\n\n\n\n\n\n\n");
    System.out.println("\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»");
    System.out.println("\tº Entering User Account Section º");
    System.out.println("\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ");

    System.out.print("\n\tEnter User Name : ");
    System.out.flush();
    usernam=in2.readLine();

    for(int e=0;e<num_accounts;e++)
    {
      if((usernam.equals(user_data[e].user_name)))      
      verify_pin(e);
    }
   
       System.out.println("\n\t*******   Error   **********");
       System.out.println("\tUnknowned User name " +usernam);
       m_menu();
       

    }


     //Verifying PIN number
     public static void verify_pin(int posi) throws IOException
     {
      String pin_num;

      DataInputStream in3=new DataInputStream(System.in);
      System.out.print("\tEnter User Personal Identification Number(PIN) : ");
      System.out.flush();
      pin_num=in3.readLine();


      if((pin_num.equals(user_data[posi].pin)))
      {
       System.out.println("\n\n\n\n\n\n\n");
       System.out.println("\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»");
       System.out.println("\tº Accessing User Account........... º");
       System.out.println("\tº .......Seaching User Account Data º");
       System.out.println("\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ");
       menu(posi);
      }

      else{
       System.out.println("\n\n\n\n\n\n\n");
       System.out.println("\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»");
       System.out.println("\tº Extremely Sorry ! Invalid PIN Entered.          º");
       System.out.println("\tº To prevent Security Hazard, re-enter User Info  º");
       System.out.println("\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ");
       locate_account();
       }
     }


    public static void menu(int posit) throws IOException
    {
     String choice;
     int option;

     DataInputStream in4=new DataInputStream(System.in);
     System.out.println("\n\n\n\n\n\n\n");
     System.out.println("\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»");
     System.out.println("\tº *******  User Account Main Menu  *******  º");
     System.out.println("\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ");
     System.out.println("\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»");
     System.out.println("\tº 1. Update User Personal Details           º");
     System.out.println("\tº 2. Change User PIN                        º");
     System.out.println("\tº 3. Withdrawal                             º");
     System.out.println("\tº 4. Deposit                                º");
     System.out.println("\tº 5. Check Account Balance                  º");
     System.out.println("\tº 6. Return to BEOYz's ATM Main Menu        º");
     System.out.println("\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ");
     System.out.println("\n\n\n\n");
     System.out.println("\t" +user_data[posit].user_name+ ", Please Enter option : ");
     System.out.flush();
     choice=in4.readLine();
     option=Integer.parseInt(choice);

     while (option > 6 || option < 1 )
     {
       System.out.print("\n\n\tOption not in list, please re-enter : ");
       System.out.flush();
       choice=in4.readLine();
       option=Integer.parseInt(choice);
     }

     switch(option)
     {
      case 1 : update(posit);
               break;
      case 2 : change_pin(posit);
               break;
      case 3 : withdrawal(posit);
               break;
      case 4 : deposit(posit);
               break;
      case 5 : check_balance(posit);
               break;
      case 6 : m_menu();
               break;
     }
   }



    //Updating User Personal Details
    public static void update(int positi) throws IOException
    {
    String new_name;
    int new_tele;

    DataInputStream in6=new DataInputStream(System.in);

    System.out.println("\n\n\n\n\n\n\n");
    System.out.println("\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»");
    System.out.println("\tº *******  Updating User Personal Details  *******  º");
    System.out.println("\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ");
    System.out.print("\n\tPlease Enter New User Name : ");
    System.out.flush();
    new_name=in6.readLine();

    System.out.print("Please Enter New Phone Number : ");
    System.out.flush();
    String temp=in6.readLine();
    new_tele=Integer.parseInt(temp);

    user_data[positi].user_name=null;

      for(int x=0;x<20;x++)
      {
        if((new_name.equals(user_data[x].user_name)))
          System.out.println("\n\tUser name has been occupied, Please enter another name : ");
          System.out.flush();
          new_name=in6.readLine();
          x=20;
      }

    user_data[positi].user_name=new_name;
    user_data[positi].telephone_no=new_tele;

    menu(positi);
    }


    public static void change_pin(int positio) throws IOException
    {
    String new_pin;

    DataInputStream in7=new DataInputStream(System.in);

    System.out.println("\n\n\n\n\n\n\n");
    System.out.println("\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»");
    System.out.println("\tº *******  Changing User PIN  *******  º");
    System.out.println("\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ");
    System.out.println("\n\n\n");
    System.out.println("\tDear " +user_data[positio]+ ",");
    System.out.print("\tPlease Enter New PIN : ");
    System.out.flush();
    new_pin=in7.readLine();

    user_data[positio].pin=null;

      for(int y=0;y<20;y++)
      {
        if((new_pin.equals(user_data[y].pin)))
          System.out.print("User PIN has been used, Please enter another PIN : ");
          System.out.flush();
          new_pin=in7.readLine();
          y=20;
       }

    user_data[positio].pin=new_pin;
    System.out.println("\tUser PIN for " +user_data[positio].user_name+ " has been changed to " +new_pin);
    System.out.println("\tPlease be aware !");

    menu(positio);
    }


    public static void withdrawal(int tempat) throws IOException
    {
    double withdraw;
    String temp;

    DataInputStream in8=new DataInputStream(System.in);

    System.out.println("\n\n\n\n\n\n\n");
    System.out.println("\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»");
    System.out.println("\tº *******  Withdrawal Section  *******  º");
    System.out.println("\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ");
    System.out.println("\n\n\n");
    System.out.println("\tUser Current Account Balance is RM" +user_data[tempat].account_balance);
    System.out.print("\tPlease Enter Amount to be withdrawed : RM");
    System.out.flush();
    temp=in8.readLine();
    Double d=new Double(temp);
    withdraw=d.doubleValue();

    System.out.println("\t ******* Transaction in progress ******");
    user_data[tempat].account_balance=user_data[tempat].account_balance-withdraw;
    System.out.println("\n\tTransaction succeeded, The new account balance for user is RM" +user_data[tempat].account_balance);
    menu(tempat);
    }


    public static void deposit(int tempat2) throws IOException
    {
    String temp;
    double deposit;

    DataInputStream in9=new DataInputStream(System.in);

    System.out.println("\n\n\n\n\n\n\n");
    System.out.println("\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»");
    System.out.println("\tº *******  Deposit Section  *******  º");
    System.out.println("\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ");
    System.out.println("\n\n\n");
    System.out.println("\tUser current account balance is RM" +user_data[tempat2].account_balance);
    System.out.print("\tPlease Enter deposit value : RM");
    System.out.flush();
    temp=in9.readLine();
    Double d2=new Double(temp);
    deposit=d2.doubleValue();

    System.out.println("\t******* Transaction in progress *******");
    user_data[tempat2].account_balance=user_data[tempat2].account_balance+deposit;
    System.out.println("The New account balance for user is RM" +user_data[tempat2].account_balance);
    menu(tempat2);
    }


    public static void check_balance(int tempat3) throws IOException
    {
    System.out.println("\n\n\n\n\n\n\n");
    System.out.println("\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»");
    System.out.println("\tº *******  Check Balance Section  *******  º");
    System.out.println("\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ");
    System.out.println("\n\n\n");
    System.out.println("\tUser current account balance is RM" +user_data[tempat3].account_balance);
    System.out.println();
    menu(tempat3);
    }

}
0
Comment
Question by:beoyz
  • 4
4 Comments
 
LVL 5

Accepted Solution

by:
fontaine earned 150 total points
Comment Utility
The problem is that when you write:

for(int x=0;x<20;x++)
 {

           if((usernama.equals(user_data[x].user_name))){
             System.out.println("\t\n*********** --- Error! --- *************");
             System.out.println("User name has been occupied, Please enter another name\n");
             x=20;}
           else
             i++;
          }

when x=3, user_data[x]=null (because you only have 3 accounts defined). It is then normal to have a NullPointerException if you try to get the user_name attribute of something that is not an atm_account but is null.

What you have to do is to loop until the number of accounts that are defined and not up to 20.
0
 
LVL 5

Expert Comment

by:fontaine
Comment Utility
[Note: the code is extracted from the add_account() method. I see that the mistake occurs at different places in your program. Don't forget to change the code each time that it has to. ]
0
 
LVL 5

Expert Comment

by:fontaine
Comment Utility
Hello beoyz! No new from you. Do you succeeded in applying the modif. I suggested (which has been tested and is thus known to work)?
0
 
LVL 5

Expert Comment

by:fontaine
Comment Utility
Hello beoyz! It appears that you have forgotten to grade this question. This is just a reminder. Isn't it great to have its Java code debugged? For me, it is always nice to see that the time I have spent solving a problem is appreciated. It also encourages to help again. So, it would be great if you could close this question: it is always disappointing to get an automatic "C" grade for an answer that is worth an "A".
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now