This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2016/11/24

java long nullpointerexception

1
2
divinity divinity

2016/11/24

#
hi danpost I have decided to leave it jus like that. that it is the only problem with it. everything else is working just perfectly thanks for all of your help, understanding and patience
divinity divinity

2016/11/26

#
hi danpost, super_hippo can someone tell me what causing this error how do i fix this error
Please select your choice now!!
4
Account@3570b0/nID: 3 
Balance: -5.0

2
Enter the amount to deposit
10
Balance is 35.0

Enter the amount to withdraw
40
Insufficient amount  Balance is: -5.0
how does it go from insufficient fund to -5 when the balance supposed to say 35.0 how to fix that from the codes posted above I have since removed these lines
 public double deposit(double deposit)
    {
        balance +=deposit;
        return(getbalance()+deposit);
    }
    public double withdraw(double withdraw)
    {
        balance -=withdraw;
        return (getbalance() - withdraw);
         
Super_Hippo Super_Hippo

2016/11/26

#
You removed these lines? How did you replace them? I think you have to show the whole code again.
divinity divinity

2016/11/26

#
this is the main
import java.text.DecimalFormat;
import java.util.Scanner;


public class ATM {
    DecimalFormat df = new DecimalFormat("#.00");
        public static void main(String[] args) {
            createAccount();
    }
        public static void createAccount()
        {
            Scanner input = new Scanner(System.in);
            int i=0; 
            double bal=50.0;
            Account[] acc = new Account[10];
            Person[] p = new Person[i++];
            
            for(i=0; i<acc.length; i++){
                System.out.println("Enter first name");
                String fn = input.next();
                System.out.println("Enter last name");
                String ln = input.next();            
                System.out.println("Enter address");
                String ad = input.next(); 
                System.out.println("Enter contact");
                String c = input.next();                
                                
                Person person= new Person(fn, ln, ad, c);
                acc[i]= new Account(i, 50, person);
            }
            while(true)
            {
                System.out.println("Enter your id");
                int id = input.nextInt();
                
                   if(id == -1)
                   {
                       break;
                   }
                while(id < 0 || id > 9)
                {
                    System.out.println("id is incorect, try again");
                    id = input.nextInt();
                }
                while(true)
                {
                    displayMenu();
                    
                    if(!useMenu(acc[id],input))
                    {
                        System.out.println();
                        break;
                    }
                    
                    System.out.println("\n");
                }
            }
        }
        public static boolean useMenu(Account acc, Scanner input)
        {
            int userchoice = input.nextInt();
            
            double amt=0;
            if(userchoice == 5)
            {
                return false;
            }
            else if(userchoice == 1)
            {
                System.out.println("The account balance is: "+ acc.getbalance());
                return true;
            }
            else if(userchoice == 2)
            {
                System.out.println("Enter the amount to withdraw");
                acc.withdraw(input.nextDouble());
                if(amt > acc.getbalance())
                {
                    System.out.println("Insufficient amount "+ " Balance is: "+acc.getbalance());
                } 
                else
                {
                    acc.withdraw(amt);
                }
            }
            else if(userchoice == 3)
            {
                System.out.println("Enter the amount to deposit");
                acc.deposit(input.nextDouble());
                acc.deposit(amt);
                System.out.println("Balance is "+ acc.getbalance());
                return true;
            }
            else if(userchoice == 4)
            {
                System.out.println(acc.toString());
            }
            else
            {
                System.out.println("invalid choice");
            }
            return true;
        }
        
        public static void displayMenu()
        {
            System.out.println("Welcome to the Automated Teller Machine!\n");
            System.out.println("Select from the following menu option below:\n");
            System.out.println("===================================");
            System.out.println("|       Main Menu                 |");
            System.out.println("===================================");
            System.out.println("| [1] Check Balance               |");
            System.out.println("| [2] Withdraw                    |");
            System.out.println("| [3] Deposit                     |");
            System.out.println("| [4] Display User Information    |");
            System.out.println("| [5] Exist                       |");
            System.out.println("===================================");
            System.out.println("Please select your choice now!!");
        }
}

this is the account class
public class Account
{
    
    private int ID;// this is the id variable 
    private double balance;
    private Person owner;
    
    public Account()
    {
    //no arg constructor    
    }
    //constructor method with 
    public Account(int ID, double balance, Person owner)
    {
        this.ID = ID;
        this.balance = balance;
        this.owner = owner;
    }
    
    public int getID()
    {
        return ID;
    }
    public double getbalance()
    {
        return balance;
    }
    public Person getowner()
    {
        return owner;
    }
    public void setID(int ID)
    {
        this.ID = ID;
    }
    public void setbalance(double balance)
    {
        this.balance = balance;
    }
    public void setowner(Person owner)
    {
        this.owner = owner;
    }
    public void deposit(double amt)
    {
        balance = balance + amt;
        
    }
    public void withdraw(double amt)
    {
        balance = balance - amt;
        
    }
    @Override
    public String toString()
    {
        String getdetails = super.toString()+
                "/nID: "+ID+" "
                + "\nBalance: "+balance;
        
        return getdetails;
    }
}

this is the person
public class Person {

    private String FirstName;
    private String LastName;
    private String address;
    private String contact;

    public Person(String f, String l, String a, String c) {
        FirstName = f;
        LastName = l;
        address = a;
        contact = c;
    }

    //get first name

    public String getFirstName() {
        return FirstName;
    }

    //get last name

    public String getLastName() {
        return LastName;
    }

    //get address

    public String getaddress() {
        return address;
    }

    //get telephone number

    public String getcontact() {
        return contact;
    }

    //accessor method with the person name and with parameters

    public void setFirstName(String FirstName) {
        this.FirstName = FirstName;
    }

    //accessor method with the person last name with parameters

    public void setLastName(String LastName) {
        this.LastName = LastName;
    }

    //accessor method the person address with parameters

    public void setaddress(String address) {
        this.address = address;
    }

    //accessor method that take one parameter

    public void setcontact(String contact) {
        this.contact = contact;
    }

    @Override
    public String toString() {
        String getdetails = super.toString()
                + "First Name " + FirstName + " "
                + "Last Name " + LastName + " "
                + "Address " + address + " "
                + " Contact " + contact;

        return getdetails;
    }

}
danpost danpost

2016/11/26

#
Wait. New code now showing. Give me time to look at it.
danpost danpost

2016/11/26

#
Okay. Line 76 is making an unconditional withdrawal of the amount input (no balance check has been performed as yet). Then, the 'amt' variable is never assigned any value (other than the initial zero value assigned to it). This should be done on line 76:
amt = input.nextDouble()
divinity divinity

2016/11/26

#
thanks danpost i got that one fixed and now how do i fixed this one. it was working good all the time and now I dont know what d hell went wrong d
Account@3570b0/nID: 1 
Balance: 35.0
this supposed to show the user information
danpost danpost

2016/11/26

#
Maybe you meant to use this for line 96 of the ATM class:
System.out.println(acc.getowner().toString());
Also, I do not think you want to use 'super.toString' in the 'toString' methods.
divinity divinity

2016/11/26

#
hi danpost i did exactly wat you suggested and this is the result Person@21b220F- why am i getting this line
Person@21b220First Name joy Last Name joyeau Address arouca Contact 6223535
danpost danpost

2016/11/26

#
divinity wrote...
Person@21b220F- why am i getting this line
I mentioned this:
danpost wrote...
I do not think you want to use 'super.toString()' in the 'toString' methods.
You may also want to use line breaks between each field by inserting "\n" between each one.
divinity divinity

2016/11/27

#
hi I am still getting this error and what do i do if I cannot use the super.toString, how do i get the user information to print if i cannot used the super.toString method Person@3570b0
danpost danpost

2016/11/27

#
You do not need the 'super.toString'. You are overriding the method with your own 'toString' method in those classes. Calling 'toString' on an Account or Person object would normally display that which you are trying to get rid of. If you did not have a 'toString' method in the class, that is what will be returned. When you add the method into the Account or Person class, it will execute that method instead of the one in the super class. But, if you call 'super.toString', you are allowing it to execute again, returning the class name of the object followed by a pointer to where the object is stored in memory. Just remove 'super.toString()+' from your 'toString' methods in those classes. The user information should still print out.
divinity divinity

2016/11/27

#
thanks very much danpost, it work out well, many many thanks danpost and super_hippo
You need to login to post a reply.
1
2