Fragment Activity with a NPE


Hi I'm trying to create my first fragment activities and thnx for the help from Natali <a href="https://stackoverflow.com/a/9110136/742030" rel="nofollow">(Cont from this thread)</a>. I convertied a class that worked fine into a fragmentand I've made it to this point but now I have the infamous NPE that I cant resolve.

It looks like its from my list-fragment class but I cant figure it out. I'm sure its an easy fix..(?) and I also replaced some variables with hard-code to make it a little easier to run.

Below is a copy of my LogCat and the activities. Thnx for your help in advance!!

<strong>EDIT:</strong> <em>The correct log file:</em> LogCat:

02-03 10:21:14.410: E/AndroidRuntime(29792): FATAL EXCEPTION: main 02-03 10:21:14.410: E/AndroidRuntime(29792): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.andaero.test/com.andaero.test.fragments.MainActivity}: java.lang.NullPointerException 02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815) 02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831) 02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.ActivityThread.access$500(ActivityThread.java:122) 02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024) 02-03 10:21:14.410: E/AndroidRuntime(29792): at android.os.Handler.dispatchMessage(Handler.java:99) 02-03 10:21:14.410: E/AndroidRuntime(29792): at android.os.Looper.loop(Looper.java:132) 02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.ActivityThread.main(ActivityThread.java:4123) 02-03 10:21:14.410: E/AndroidRuntime(29792): at java.lang.reflect.Method.invokeNative(Native Method) 02-03 10:21:14.410: E/AndroidRuntime(29792): at java.lang.reflect.Method.invoke(Method.java:491) 02-03 10:21:14.410: E/AndroidRuntime(29792): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 02-03 10:21:14.410: E/AndroidRuntime(29792): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 02-03 10:21:14.410: E/AndroidRuntime(29792): at dalvik.system.NativeStart.main(Native Method) 02-03 10:21:14.410: E/AndroidRuntime(29792): Caused by: java.lang.NullPointerException 02-03 10:21:14.410: E/AndroidRuntime(29792): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:99) 02-03 10:21:14.410: E/AndroidRuntime(29792): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94) 02-03 10:21:14.410: E/AndroidRuntime(29792): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46) 02-03 10:21:14.410: E/AndroidRuntime(29792): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47) 02-03 10:21:14.410: E/AndroidRuntime(29792): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1539) 02-03 10:21:14.410: E/AndroidRuntime(29792): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1508) 02-03 10:21:14.410: E/AndroidRuntime(29792): at com.andaero.test.fragments.ListFragment.loadQuery(ListFragment.java:72) 02-03 10:21:14.410: E/AndroidRuntime(29792): at com.andaero.test.fragments.ListFragment.onActivityCreated(ListFragment.java:60) 02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:795) 02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:977) 02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:960) 02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1674) 02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.Activity.performCreate(Activity.java:4400) 02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048) 02-03 10:21:14.410: E/AndroidRuntime(29792): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779) 02-03 10:21:14.410: E/AndroidRuntime(29792): ... 11 more

The MainActivity Class:

public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } The ListFragment Class: public class ListFragment extends android.app.ListFragment { boolean mDualPane; int mCurCheckPosition = 0; protected TextView activityTitle; boolean mExternalStorageAvailable = false; boolean mExternalStorageWriteable = false; String extStorageDirectory = Environment.getExternalStorageDirectory() .toString(); File dbfile = new File(extStorageDirectory + "/myApp/dB/myApp.db"); SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null); private static final String QUERY_KEY = "SELECT * FROM Regulatory_List"; private static final String QUERY_ORDER = "ASC"; private View layout; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { layout = inflater.inflate(R.layout.listview, null); return layout; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Bundle extras = getActivity().getIntent().getExtras(); String q = null; if (extras != null) { q = extras.getString(QUERY_KEY); } loadQuery(q); } public void loadQuery(String q) { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { String qO = getActivity().getIntent().getStringExtra("QUERY_ORDER"); Cursor c = db.rawQuery(q + " ORDER BY `_id` " + qO, null); setListAdapter(new QueryAdapter(getActivity(), c)); db.close(); } else { Alerts.sdCardMissing(getActivity()); } } public class QueryAdapter extends CursorAdapter { public QueryAdapter(Context context, Cursor c) { super(context, c); LayoutInflater.from(context); } @Override public void bindView(View v, Context context, final Cursor c) { int tvLabel = c.getColumnIndexOrThrow("label"); String label = c.getString(tvLabel); final TextView labelTxt = (TextView) v.findViewById(R.id.label); if (labelTxt != null) { labelTxt.setText("(" + label + ")"); } int tvTitle = c.getColumnIndexOrThrow("title"); final String title = c.getString(tvTitle); TextView titleTxt = (TextView) v.findViewById(R.id.listTitle); if (titleTxt != null) { titleTxt.setText(title); } int tvDescription = c.getColumnIndexOrThrow("description"); String description = c.getString(tvDescription); TextView descriptionTxt = (TextView) v.findViewById(R.id.caption); if (descriptionTxt != null) { descriptionTxt.setText(description); } int tvDate = c.getColumnIndexOrThrow("date"); String date = c.getString(tvDate); TextView dateTxt = (TextView) v.findViewById(R.id.dateAdded); if (dateTxt != null) { dateTxt.setText(date); } int tvGoto = c.getColumnIndexOrThrow("gotoURL"); final String gotoURL = c.getString(tvGoto); TextView gotoTxt = (TextView) v.findViewById(R.id.dummy); if (gotoTxt != null) { gotoTxt.setText(gotoURL); } gotoTxt.setVisibility(View.GONE); v.setTag(gotoURL); final ListView lv = getListView(); lv.setEnabled(true); lv.setClickable(true); lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) { String url = ""; url = (String) v.getTag(); int nI = c.getColumnIndexOrThrow("intent"); String intent = c.getString(nI); Class<?> myIntent = null; try { myIntent = Class.forName("com.andaero.test.fragments" + intent); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } int tvTitle = c.getColumnIndexOrThrow("title"); String title = c.getString(tvTitle); int tvLabel = c.getColumnIndexOrThrow("label"); String label = c.getString(tvLabel); String queryKey = "SELECT * FROM " + label; c.close(); db.close(); Intent i = new Intent(); i.putExtra("QUERY_KEY", queryKey); i.putExtra("KEY_URL", url); i.putExtra("KEY_SUBTITLE", title); i.putExtra("KEY_LABEL", label); i.putExtra("KEY_INTENT", intent); i.putExtra("QUERY_ORDER", "ASC"); i.putExtra("KEY_YPOS", "0.0"); startActivity(i); } }); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { final View v = LayoutInflater.from(context).inflate( R.layout.list_item, parent, false); return v; } } }


Caused by: java.lang.IllegalArgumentException: column '_id' does not exist

Means you don't have a column named _id in you table.

See this <a href="http://developer.android.com/reference/android/widget/CursorAdapter.html" rel="nofollow">link</a>.

"Adapter that exposes data from a Cursor to a ListView widget. The Cursor must include a column named "_id" or this class will not work."


Looks like the query may be invalid. I think what you want is:

Cursor c = db.rawQuery(QUERY_KEY + " ORDER BY `_id` " + QUERY_ORDER, null);

If you still have issues add more info to log:

Log.e("QUERY", q + " ORDER BY `_id` " + qO);


Why are you doing this q = extras.getString(QUERY_KEY); Is the query being pass from another activity? Seem like you want loadQuery(QUERY_KEY);


  • Trying to install sonarqube on linux machine but getting error
  • ValueError: math domain error, keeps popping up
  • In C++11 is sqrt defined as constexpr?
  • Excel, cycle through columns and rows
  • Why the Fragment doesn't work
  • ListAdapter lifecycle and screen rotation for canceling a AsyncTask
  • regarding the failure of stepwise variable selection in lm
  • How to add ImageView viewPager in ListFragment in android?
  • Design Application with fragments
  • How to perform single factor ANOVA in R with samples organized by column?
  • Calling AlertDialog.Builder in Java For Android Fragment When Back Button Is Pressed
  • Detect SD card using WMI Query
  • Django template object property lookup with a dynamic variable name
  • Possible Parser for Unknown String Format(soup?) from SUDS.client
  • Substring for SQLite Android
  • How do I Return a Runnable in this Method?
  • Android: Display Multiple Pieces of Cursor Data Together in TextViews instead of a ListView
  • Sqlite not inserting data in 2nd table
  • Uploading Picture From PhoneGap Cordova App
  • How can i read the db in my android app which i've modified in my pc and copied back to my app&
  • Why does changing sq to point-free change the type [duplicate]
  • Get values from database by month in android
  • Node js & Apache In Parallel?
  • Working with reactiveValues and ggplot in shiny
  • Cursor returns wrong values - sqlite - Android
  • MySQLi using an IN in a where
  • Put database into android project
  • Pre-populated SQLite Database not reading properly in Android Studio
  • Do query loads all the data in memory
  • Merging rows to columns
  • Zurb Foundation _global.scss meta styles for js?
  • ActiveRecord query for a count of new users by day
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • Display Images one by one with next and previous functionality
  • SQL merge duplicate rows and join values that are different
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?