【问题标题】:BankAccount program , adding to arraylist through a methodBankAccount 程序,通过方法添加到arraylist
【发布时间】:2013-05-22 21:32:36
【问题描述】:

我在将对象添加到数组列表时遇到问题,使用类中的方法会引发错误,我似乎无法找到问题所在。 错误是Error = non-static variable db cannot be referenced from a static contex

驱动代码(有 5 种情况,但问题出在所示的情况下。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;

& 这里是来自数据库类的代码 - 调用数组列表的 add 方法 打包银行账户;

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++;
            }
        }
    } 
}

【问题讨论】:

标签: java methods arraylist


【解决方案1】:

在 switch 块的末尾,您引用了不是 static 的 db 对象,但是您处于静态上下文 (public **static** void main) 中。简单的解决办法是换行

Database db = new Database();

到

static Database db = new Database();

您可能还需要更改其他两个变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-16
    • 2012-04-14
    • 1970-01-01
    • 2018-10-11
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多