Stefan Stefan - 1 month ago 8
Android Question

Adding data to container class in sqlite Android

I have a contailer class

ServiceMeasureCollection
:


import java.util.ArrayList;

public class ServiceMeasureCollection {
public String service_id="";
public String service_type="";
public String service_measure="";

public static ArrayList<ServiceMeasureCollection> data_arr=null;
public ServiceMeasureCollection(String service_id,String service_type, String service_measure){

this.service_id=service_id;
this.service_type=service_type;
this.service_measure=service_measure;
}
}



I want to call this class from an ArrayList and fill it qith sqlite data as follows:


public ArrayList<ServiceMeasureCollection> getServiceProviderServices() {
ServiceMeasureCollection.data_arr = new ArrayList<ServiceMeasureCollection>();

Log.d("test", "testing if getServProv Services works " );
String selectQuery = "SELECT * FROM " + TABLE_SERVICEPROVSERVICE;

SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
Log.d(TAG, "testing if getServProv Services works " );
// Move to first row
cursor.moveToFirst();

if (cursor.getCount() > 0) {
do {
ServiceMeasureCollection.data_arr.add(new ServiceMeasureCollection(cursor.getString(0),
cursor.getString(8),cursor.getString(1)));

} while (cursor.moveToNext());
}

cursor.close();
db.close();
// return user
Log.d(TAG, "Fetching user from Sqlite: " +ServiceMeasureCollection.data_arr.toString());

return ServiceMeasureCollection.data_arr;
}



Everytime I try to call it in my Main via
db.getServiceProviderServices()
I get a Null Pointer Exception. I am guessing this is because I initialize
data_arr=Null
.
Does anybody know how to fix this or what the real issue is?

Stacktrace:


E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.monobono.monobonoapp, PID: 4458
java.lang.NullPointerException
at de.monobono.monobonoapp.activity.ProviderServiceListFragment.onViewCreated(ProviderServiceListFragment.java:78)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5147)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(Native Method)



EDIT:


public class ProviderServiceListFragment extends Fragment {

View myView;
public String stampid;
private ListView listView;
private ListView listView2;
private ListView listView3;
public TextView dr_name;
private ProgressDialog pDialog;
private SQLiteHandler connection;
private SQLiteHandler db;
private ServiceMeasureAdapter list_adapter;
private ListView lv_android;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.service_measure_list_section, container,
false);
return myView;

}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
stampid = getArguments().getString("stampID");
super.onViewCreated(view, savedInstanceState);

db.getServiceProviderServices();

} }


Answer

Your member variable private SQLiteHandler db is null by the time onViewCreated is called. This is causing your NullPointerException. You must add something like this is the onCreateView function of your Fragment

db = new SQLiteHandler(/*Required parameters*/);