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 this is my assignment. i have coded this program and I am wondering how to fix this error. i hve three class. I am stuck as to what to do next and how to fix it any help would do it supposed to run ten time, doh know why it run that length of time. is there anyway i can fix it. this is the main
public class Project 
{

    
    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 telephone number");
                String tn = input.next();                
                                
                //p[i]= new Person(fn, ln, ad, tn);
                //acc[i]= new Account(i, 50);
            }
            while(true)
            {
                System.out.println("Enter your id");
                int id = input.nextInt();
                
                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();
            
            if(userchoice == 4)
            {
                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(acc.withdraw(userchoice)> acc.getbalance())
                {
                    System.out.println("Insufficient amount");
                }
                else
                {
                    acc.withdraw(userchoice);
                }
            }
            else if(userchoice == 3)
            {
                System.out.println("Enter the amount to deposit");
                acc.deposit(input.nextDouble());
                return true;
            }
            else if(userchoice == 4)
            {
                acc.toString();
            }
            else
            {
                System.out.println("invalid choice");
            }
            return true;
        }
        
        public static void displayMenu()
        {
            System.out.println("Main Menu");
            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("Enter a choice");
        }
}

this is the account class 
package Project;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author USER
 */
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 double deposit(double deposit)
    {
        balance +=deposit;
        return(getbalance()+deposit);
    }
    public double withdraw(double withdraw)
    {
        balance -=withdraw;
        return (getbalance() - withdraw);
        
    }
    public String toString()
    {
        String details = owner.toString()+"/nID: "+ID+""
                + "\nBalance: "+balance;
        return details;
    }
}
and this is the Person class

 * @author USER
 */
public class Person
{
    private String FirstName;
    private String LastName;
    private String address;
    private String telephoneNumber;
    public Person(String f,String l,String a, String t){
        FirstName=f;
        LastName=l;
        address=a;
        telephoneNumber=t;
    }
    
    //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 getTelephoneNumber()
    {
        return telephoneNumber;
    }
    //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 setTelephoneNumber(String telephoneNumber)
    {
        this.telephoneNumber = telephoneNumber;
    }
    @Override
    public String toString()
    {
        String details = "Name: "+FirstName +" "+ LastName
                +"\nAddress: "+address+"\nPhone No: "+telephoneNumber;
        return details;
    }
            
    
}
 and this here is the output and the error 
Enter first name
javon
Enter last name
smith
Enter address
edr
Enter telephone number
2263363
Enter first name
jane
Enter last name
doe
Enter address
valysan
Enter telephone number
4567893
Enter first name
devon
Enter last name
mathews
Enter address
diegomartin
Enter telephone number
7845663
Enter first name
kaisley
Enter last name
doe
Enter address
belmont
Enter telephone number
4459898
Enter first name
jaime
Enter last name
denn
Enter address
belmont
Enter telephone number
2269897
Enter first name
yohan
Enter last name
small
Enter address
maloney
Enter telephone number
2236965
Enter first name
tiffany
Enter last name
hamil
Enter address
laventille
Enter telephone number
6254784
Enter first name
luana
Enter last name
doe
Enter address
edr
Enter telephone number
6236321
Enter first name
dolly
Enter last name
mohan
Enter address
sanjuan
Enter telephone number
7415202
Enter first name
yohan
Enter last name
john
Enter address
b
Enter telephone number
4479696
Enter your id
3
Main Menu
| [1] Check Balance
| [2] Withdraw
| [3] Deposit
| [4] Display User Information
| [5] Exist 
Enter a choice
1
Exception in thread "main" java.lang.NullPointerException
	at Project.Project.useMenu(Project.java:80)
	at Project.Project.createAccount(Project.java:61)
	at Project.Project.main(Project.java:24)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 minutes 1 second)
danpost danpost

2016/11/24

#
You are creating an array to hold 10 Account objects (line 14), but you never create any Account objects and set references to them in the array. Actually you have a line commented out that could create an Account object (line 28); however, it is missing the Person object argument. You do not need to have an array for the Person objects as each Account object will hold its owner in an instance field. Remove line 15. Then, replace lines 28 and 29 with the following:
Person person = new Person(fn, ln, ad, tn);
acc[i] = new Account(i, 50, person);
With this, your accounts will be created properly and added to your array.
divinity divinity

2016/11/24

#
hi danpost it is working but with a few issues first issues: how do i get to restrict the number of times it enter the firstname, lastname, address, and contact( it is supposed to run at least 10 times because it is the amount of accounts,how to fix that second issues: the balance say 50, when the first choice enter, (check balance) right it give you the balance but when you enter the second choice, (which is to withdraw, you enter the amount to be withdrawn, when u check the balance afterward, the balance say something different, for e.g, the initial balance say $50, but when you withdraw $25, the balance say $21 when it say $25, how do i fix that, it is obviously the calculation is wrong somewhere.how to fix dah third issues: how do i get it out of the loop, when you entered the last transaction after it asked u to enter the id, i would like to code it in such a way when it ask for the id, i should be able to enter something where it can break out the loop. what do i do to fix these issues. here is the output the codes is already up there so i am just going to post the output only just for what you to check out for me
Enter first name
javon
Enter last name
doe
Enter address
belmont
Enter telephone number
2263354
Enter first name
sahton
Enter last name
smith
Enter address
gonzales
Enter telephone number
3365854
Enter first name
yohan
Enter last name
mohammed
Enter address
chaguanas
Enter telephone number
7458969
Enter first name
solange
Enter last name
tewarie
Enter address
felicity
Enter telephone number
4479963
Enter first name
john
Enter last name
clyde
Enter address
sanfernando
Enter telephone number
2824556
Enter first name
paul
Enter last name
walker
Enter address
princestown
Enter telephone number
4567414
Enter first name
paula
Enter last name
james
Enter address
diegomartin
Enter telephone number
2210003
Enter first name
jennamarie
Enter last name
sonnyman
Enter address
tobago
Enter telephone number
3224100
Enter first name
donald
Enter last name
donaldson
Enter address
canaan
Enter telephone number
7558947
Enter first name
ruthlyn
Enter last name
dickson
Enter address
maloney
Enter telephone number
7100002
Enter your id
3
Main Menu
| [1] Check Balance
| [2] Withdraw
| [3] Deposit
| [4] Display User Information
| [5] Exist 
Enter a choice
1
The account balance is: 50.0


Main Menu
| [1] Check Balance
| [2] Withdraw
| [3] Deposit
| [4] Display User Information
| [5] Exist 
Enter a choice
2
Enter the amount to withdraw
25


Main Menu
| [1] Check Balance
| [2] Withdraw
| [3] Deposit
| [4] Display User Information
| [5] Exist 
Enter a choice
1
The account balance is: 21.0


Main Menu
| [1] Check Balance
| [2] Withdraw
| [3] Deposit
| [4] Display User Information
| [5] Exist 
Enter a choice
3
Enter the amount to deposit
10


Main Menu
| [1] Check Balance
| [2] Withdraw
| [3] Deposit
| [4] Display User Information
| [5] Exist 
Enter a choice
1
The account balance is: 31.0


Main Menu
| [1] Check Balance
| [2] Withdraw
| [3] Deposit
| [4] Display User Information
| [5] Exist 
Enter a choice
2
Enter the amount to withdraw
40


Main Menu
| [1] Check Balance
| [2] Withdraw
| [3] Deposit
| [4] Display User Information
| [5] Exist 
Enter a choice
1
The account balance is: -13.0


Main Menu
| [1] Check Balance
| [2] Withdraw
| [3] Deposit
| [4] Display User Information
| [5] Exist 
Enter a choice
4

Enter your id
5
Main Menu
| [1] Check Balance
| [2] Withdraw
| [3] Deposit
| [4] Display User Information
| [5] Exist 
Enter a choice
-1
invalid choice


Main Menu
| [1] Check Balance
| [2] Withdraw
| [3] Deposit
| [4] Display User Information
| [5] Exist 
Enter a choice
5
invalid choice


Main Menu
| [1] Check Balance
| [2] Withdraw
| [3] Deposit
| [4] Display User Information
| [5] Exist 
Enter a choice
2
Enter the amount to withdraw
40


Main Menu
| [1] Check Balance
| [2] Withdraw
| [3] Deposit
| [4] Display User Information
| [5] Exist 
Enter a choice
1
The account balance is: 6.0


Main Menu
| [1] Check Balance
| [2] Withdraw
| [3] Deposit
| [4] Display User Information
| [5] Exist 
Enter a choice
BUILD STOPPED (total time: 7 minutes 56 seconds)
Super_Hippo Super_Hippo

2016/11/24

#
I can't understand how the deposit method is working at all. That are the two methods:
    public double deposit(double deposit)
    {
        balance +=deposit;
        return(getbalance()+deposit);
    }
    public double withdraw(double withdraw)
    {
        balance -=withdraw;
        return (getbalance() - withdraw);
    }
I think that the last line in both methods should be 'return balance;' because you already adjust the balance variable in the line before. I can't explain your results there, but as far as I see it, both methods should not work right now.
divinity divinity

2016/11/24

#
thanks much super_hippo and another thing, once u exit the program, it should ask for the id again, which it does actually but once it start it will not stop and to exit the program the user must enter -1 when the id is requested, hoiw do i get the program to do just that
divinity divinity

2016/11/24

#
hi super_hippo, danpost I have since correct the errors and I am still getting the same errors, and just found another issues when the user choice 4 where it is supposed to display the user information, it is giving me the enter the id how to fix this please
divinity divinity

2016/11/24

#
hi danpost and super_hippo i fix the part where it was giving me the incorrect amount, so now I have one more issue to fix. the last issues is: when the user enter choice 4 it is supposed to display the user information which am not getting and when the user enter choice 5 it is supposed to ask you to re- enter your id which it does but the thing is it is supposed to keep running until the user enter -1 to exit the program and that is not what I am getting , how do i fix that.
Super_Hippo Super_Hippo

2016/11/24

#
The 'acc.toString();' you have at choice 4 returns a String, but you are not printing it out. Another thing is that it is never reached because you have this at the start of the method:
if(userchoice == 4)
{
    return false;
}
So line 85 will never be true. Choice 5 is 'Exist' (what do you mean by that?). It makes you reenter the ID because 5 is 'invalid choice' right now. You can compare userchoice with -1 and then exit the program (by using Greenfoot.stop() for example).
divinity divinity

2016/11/24

#
hi super_hippo am still not getting, no matter what I do when i change it at first, it was looping on enter the id, so hadda make speedy changes so what can i do, try everything that I can think of.
danpost danpost

2016/11/24

#
Line 47 of the Project class above creates a string for the account; but, nothing is done with that string (it is not being output, if that is what you want).
danpost danpost

2016/11/24

#
You could insert the following line at line 34 of the Project class:
if (id == -1) break;
which would break out of the 'while' loop and end the program.
divinity divinity

2016/11/24

#
hi danpost and super_hippo danpost, I tried fixing line 47 but when i do, it messing up the running of the program, i think I am going to leave it just like that. that is the only problems with the program, otherwise the program is running just like it supposed to. the codes below is another issues with the program, why is it giving me a negative balance which is not supposed to. how to fix
Main Menu
| [1] Check Balance
| [2] Withdraw
| [3] Deposit
| [4] Display User Information
| [5] Exist 
Enter a choice
2
Enter the amount to withdraw
40
Insufficient amount


Main Menu
| [1] Check Balance
| [2] Withdraw
| [3] Deposit
| [4] Display User Information
| [5] Exist 
Enter a choice
1
The account balance is: -5.0
divinity divinity

2016/11/24

#
i add it in but it gone back to the menu danpost, it is not working.
divinity divinity

2016/11/24

#
hi danpost I got it to work except for the part where i am getting the negative -5 for the balance. but that hopefully should not be a problem thanks very much guys, you all help is very much appreciated.
danpost danpost

2016/11/24

#
Lines 69 and 70 of the Project class is causing the problem. First, you do not want to withdraw anything until you check the amount with the balance. Then, you do not want to withdraw the 'userchoice' value at all (it really does not make any sense to do that). Place the amount to withdraw in a local double field; then, check that with the balance and either print the 'insufficient funds' message or make the withdrawal.
There are more replies on the next page.
1
2