coderaizer coderaizer - 5 months ago 18
Android Question

How do I fix Intent.getExtras()' on a null object reference?

I want to populate a list view based on grid item click. If user clicks the first grid item I want to show the list view according to it. I have used a pre populate SQLite database here.

What I tried is , to get item click id in first activity and pass it to DB Class. This is what I'm getting when debugging the programme.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
at com.example.sra.hellosrilanka.DBAccess.getQuotes(DBAccess.java:51)
at com.example.sra.hellosrilanka.ContactView.onCreate(ContactView.java:29)


DBAccess Class:

public class DBAccess extends Activity {
private SQLiteOpenHelper openHelper;
private SQLiteDatabase database;
private static DBAccess instance;
String passedVar=null;

public DBAccess(Context context) {

this.openHelper =new HelloDatabase(context);
}

public static DBAccess getInstance(Context context) {
if (instance == null) {
instance = new DBAccess(context);
}
return instance;
}

public void open() {
this.database = openHelper.getWritableDatabase();
}

public void close() {
if (database != null) {
this.database.close();
}
}



public List<String> getQuotes() {
List<String> list = new ArrayList<>();
Integer value;

Bundle extras = getIntent().getExtras();
String a = extras.getString("ID_EXTRA");


if(extras != null)
{
Cursor cursor = database.rawQuery("SELECT org_name FROM org_name WHERE category_id = \""+ a +"\"" , null);
/* Cursor cursor = database.rawQuery("SELECT org_name FROM org_name WHERE category_id="+a, null);*/
/*Cursor cursor = database.rawQuery("SELECT org_name FROM org_name WHERE category_id='ID_EXTRA'", null);*/
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
list.add(cursor.getString(0));
cursor.moveToNext();
cursor.close();
}
}

/* if (passedVar != null) {


}*/

return list;
}}


Contact View Class:

public class ContactView extends Activity {
private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);


this.listView = (ListView) findViewById(R.id.listView);
DBAccess databaseAccess = DBAccess.getInstance(this);
databaseAccess.open();
List<String> quotes = databaseAccess.getQuotes();
databaseAccess.close();

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, quotes);
this.listView.setAdapter(adapter);

}


}

Main activity class

public class MainActivity extends AppCompatActivity {

GridView grid;
Toolbar toolbar;
private ListView listView;

String [] result;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

grid = (GridView) findViewById(R.id.grid);


toolbar=(Toolbar)findViewById(R.id.toolBar);
setSupportActionBar(toolbar);

grid.setAdapter(new CustomAdapter(this));
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
/*Intent intent=new Intent(MainActivity.this,ContactView.class);
intent.putExtra("catid", id);
startActivity(intent);*/

Intent i=new Intent(MainActivity.this ,ContactView.class);
i.putExtra("ID_EXTRA",String.valueOf(id));
startActivity(i);

}




});}}

Answer

You just passed the instance of ContactViewActivity to the constructor of DBAccess and then didn't keep a reference to it.

ContactViewActivity.onCreate:

DBAccess databaseAccess =  DBAccess.getInstance(this);

DBAccess.getInstance:

public static DBAccess getInstance(Context context) {
    if (instance == null) {
        instance = new DBAccess(context);  // ok let's take a look into the constructor
    }
    return instance;
}

DBAccess.(constructor):

public DBAccess(Context context) {

    this.openHelper =new HelloDatabase(context);
    // you will lost a reference to the context when returning
}

DBAccess.getQuotes:

public List<String> getQuotes() {
    List<String> list = new ArrayList<>();
    Integer value;

    Bundle extras = getIntent().getExtras(); // now, get intent from what?
    ...
}
Comments