-1

Now I am getting null pointer Exception here. In, ViewAllData method myAdapter = new SQLCustomViewAllDataAdapter(SQLViewAllData.this,aList); here I am getting Error on creating the Constructor.

SQLViewAllData.java

public class SQLViewAllData extends Activity {

private SQLDbHelper myDbHelper;
private SQLCustomViewAllDataAdapter myAdapter;
private ListView myListView;
private ArrayList<SQLPojo.UserDetails> aList = new ArrayList<>();
//private Context myContext;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview_view_data);
    //myContext = this;

    widgetInitialization();
    viewAllData();
}
//Initializing Widgets
public void widgetInitialization() {
    myDbHelper = new SQLDbHelper( this );
    myListView = (ListView)findViewById( R.id.listview_view_data_LV );
}

public ArrayList<SQLPojo.UserDetails> getUserInfo(){
    return myDbHelper.getUserDetails();
}

//view all data stored in DB
public void viewAllData() {
    aList = getUserInfo();
    myAdapter = new SQLCustomViewAllDataAdapter(SQLViewAllData.this,aList);
    myListView.setAdapter(myAdapter);
}

}

Angler Ia
  • 7
  • 3
  • try `myDbHelper = new SQLDbHelper( SQLViewAllData.this);` – Kostas Drak Dec 24 '16 at 13:47
  • why are you duplicating questions: http://stackoverflow.com/questions/41306863/how-to-get-sqlite-db-values-in-arraylist-to-listview ? – pskink Dec 24 '16 at 14:16
  • Here is a link explains which very well about different contexts and their uses. https://possiblemobile.com/2013/06/context – muthuraj Dec 24 '16 at 14:19

2 Answers2

2

You need to change:

myDbHelper = new SQLDbHelper( myContext );

To

myDbHelper = new SQLDbHelper(this);

Also remove 'Activity myContext' field since your class already extends Activity.

Michael Spitsin
  • 2,539
  • 2
  • 19
  • 29
  • OP might need `myContext` for later use so you can also suggest assigning `myContext = this` inside `oncreate` before calling to that function – Pavneet_Singh Dec 24 '16 at 13:44
  • @PavneetSingh if he will need context in some place, he will pass `this` instead of meaningless field – Michael Spitsin Dec 24 '16 at 13:58
  • think like , there might be 2 other function using the same variable or maybe not but if it's there then again the same issue so IMO you can explain to OP why `mcontext` is null and what he can do to solve this issue if any similar cases are there, rest the current issue will be resolved with this so i did voted you at first pace – Pavneet_Singh Dec 24 '16 at 14:01
1

u have not intialized the context i.e myContext