-1

I'm having a problem adding an object to an arraylist, using a method from class throws a error and I cant seem to find the problem. The error is Error = non-static variable db cannot be referenced from a static contex

Driver code (there are 5 cases but problem is in the case shown. last line above break)

package bankaccount;
import java.util.Random;
public class DriverClass 
{
    Database db = new Database();
    Database Deleted = new Database();
    boolean done = false;

    public static void main(String[] args)
    {
        while (!false)
        {
            int menu = IO.getInt("Please choose one of the following:"+
                    "\n 1 to create new account"+ "\n 2 to delete an account"+
                    "\n 3 to withdraw from an account"+"\n 4 to deposit to an account"+
                    "\n 5 to list all customers"+"\n 6 to list all deleted customers"+
                    "\n 7 to display single account "+"\n 8 to exit this program");

            switch(menu)
            {


            case 1:
                //Create bankaccount object  = creates new account
                String LastName = IO.getString("Please type last name: ");
                String FirstName = IO.getString("Please type first name: ");
                Name n = new Name (LastName,FirstName);

                //Create address object
                String street = IO.getString("Please type your address: ");
                String city = IO.getString("Please type your city: ");
                String state = IO.getString("Please type your state: ");
                String zipcode = IO.getString("Please type your zipcode: ");
                Address addr = new Address (street,city,state,zipcode);
                //Create Account number
                Random randomGenerator = new Random();
                int randomInt = randomGenerator.nextInt(2000000000);
                AccountNum accno = new AccountNum(randomInt);
                //Create customer object
                Customer c = new Customer(n,addr,accno);
                //Create bankaccount object 
                double amt = IO.getDouble("Please type the opening account balance: ");
                BankAccount b = new BankAccount(c, amt);
                db.add(b);
                break;

& here is the code from the database class - calling the add method for array list package bankaccount;

import java.util.ArrayList;

public class Database 
{
    int index;
    boolean found;
    ArrayList<BankAccount> list;
    BankAccount acc;

    Database()
    {
        ArrayList<BankAccount> list = new ArrayList<BankAccount>();
    }

    public void add(BankAccount b)
    {
        list.add(b);
    }

    BankAccount remove (int i)
    {
        return list.remove(i);
    }

    BankAccount getaccount()
    {
        return acc;
    }

    ArrayList getlist()
    {
        return list;
    }

    int getindex()
    {
        return index;
    }

    boolean inlist()
    {
        return found;
    }

    void search (String key)
    {
        found = false;
        int i = 0;
        //int.length = list.size();

        while (i < list.size() && !found)
        {
            BankAccount b = list.get(i);
            if (key.equals(b.getcustomer()))
            {
                acc = b; found = true; index = i;
            }
            else 
            {
                i++;
            }
        }
    } 
}
7
  • Error = non-static variable db cannot be referenced from a static context. Appreciate any push in the right direction! Commented May 28, 2013 at 2:50
  • check this out stackoverflow.com/questions/5671610/… stackoverflow.com/questions/3527137/… Commented May 28, 2013 at 2:51
  • Thanks - ill check them out now. Commented May 28, 2013 at 2:52
  • stackoverflow.com/questions/7384908/… Commented May 28, 2013 at 2:54
  • Thank you for the links - did help me clarify how to add objects to arraylist. Commented May 28, 2013 at 3:03

1 Answer 1

0

At the end of the switch block you make reference to the db object which is not static, however you are in a static context (public **static** void main). The simple solution would be to change the line

Database db = new Database();

to

static Database db = new Database();

You will likely need to change the other two variables as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.