2012-09-01 49 views
0

在我的应用程序中发生的事情是,我从数据库中得到一列,这起作用,因为当我System.out.println()它以列表形式显示我想要的内容时,我然后尝试将其传递给另一个班级,并且不断收到错误。任何人都知道这是为什么发生?如果您可以显示代码,请提前致谢。将ArrayList传递给另一个类

将代码从我的主类(相当冗长所以只能连接一小部分):

//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); 

这里是我想这种传递到类:

public class AFragmentTabDB { 


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

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

} 

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

} 

} 

这里是哪里我的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; 
} 
} 
+4

这是错误? – SJuan76

+0

getRoleList()从来没有被称为 – wtsang02

+0

@ wtsang02,它在代码的第一段的最后一行 – SJuan76

回答

2

您还没有构建AFragmentTabDB对象,因此当您调用setRoleL ist()你在一个空对象上调用它。

AFragmentTabDB aFDB; 

你行更改为

AFragmentTabDB aFDB = new AFragmentTabDB(); 
+0

就是这样!我非常非常感谢你<3。 – JoeyL