简体   繁体   English

将ArrayList传递给另一个类

[英]Passing ArrayList to another class

What is happening here in my app is that I get a column from the database, which works cause when I System.out.println() it shows exactly what I want in an arraylist format, I then try to pass that to another class and I keep getting an error. 在我的应用程序中发生的是我从数据库中获取一个列,当我在System.out.println()中以arraylist格式显示我想要的内容时,它会起作用,然后我尝试将其传递给另一个类,我一直在收到错误。 Anyone know why this is happening? 任何人都知道为什么会这样吗? Please if you can show code, thank you in advance. 如果您能显示代码,请提前感谢。

The Code from my Main Class (pretty lengthy so only linking a small portion): 来自我的主类的代码(非常冗长所以只链接一小部分):

//Declarations at the top
ArrayList<String> roles = new ArrayList<String>();
AFragmentTabDB aFDB;
DataBaseHelper myDB;

//Trying to pass the arraylist to another class called aFDB
roles = myDB.getTableColumn("role", new String[] {"name"});
System.out.println(roles);  
aFDB.setRoleList(roles);

Here is the class I am trying to pass this into: 这是我试图将其传递到的类:

public class AFragmentTabDB {


ArrayList<String> roles = new ArrayList<String>();

public void setRoleList(ArrayList<String> aL){
    this.roles = aL;

}

public ArrayList<String> getRoleList(){
    return roles;

}

}

Here is where my getTableColumn() is: 这是我的getTableColumn()的位置:

public class DataBaseHelper extends SQLiteOpenHelper{

    public ArrayList<String> getTableColumn(String tableName, String[] column){

    ArrayList<String> aL = new ArrayList<String>();

    cursor = myDataBase.query(tableName, column, null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      aL.add(cursor.getString(0));
      cursor.moveToNext();
    }

    cursor.close();
    return aL;
}
}

You haven't constructed the AFragmentTabDB object so when you call setRoleList() you are calling that on a null object. 您还没有构造AFragmentTabDB对象,所以当您调用setRoleList()时,您在null对象上调用它。

Change your line from 改变你的行

AFragmentTabDB aFDB;

to

AFragmentTabDB aFDB = new AFragmentTabDB();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM