JoeyL
JoeyL

Reputation: 1305

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. 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:

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

Upvotes: 0

Views: 4236

Answers (1)

brianestey
brianestey

Reputation: 8302

You haven't constructed the AFragmentTabDB object so when you call setRoleList() you are calling that on a null object.

Change your line from

AFragmentTabDB aFDB;

to

AFragmentTabDB aFDB = new AFragmentTabDB();

Upvotes: 2

Related Questions