2012-09-01 55 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