mardi 2 février 2016

App is force closing when I try to fetch data from SQLiteDB in Android

Data are successfully inserted in DB.But when I try to retrieve data app force closes. So the scenario is user enters a date in a activity and clicking of a button a search is made in DB.If match exists,i am going to display that in a new activity.

ViewByDate Activity

        ViewDateResult vb=new ViewDateResult();
        String s=et1.getText().toString();
        vb.givedate(s);
        Intent i=new Intent(ViewByDate.this,ViewDateResult.class);
        ViewByDate.this.startActivity(i);

Here the editext value is passed as a string to next activity when the button is clicked.Once done i am starting the new activity

ViewDateResult Activity

EditText t1,t2,t3,t4;
SQLiteDatabase db;
public static String que;
void givedate(String date) 
{
 que=date;
 }
 private static final String SELECT_SQL = "SELECT * FROM makeway where Date="+ que ;
private Cursor c;
protected void show()

{

   String In=c.getString(0);
   String out=c.getString(1);
   String date=c.getString(2);
   String Total=c.getString(3);
   t1.setText(In);
   t2.setText(out);

   t3.setText(date);
   t4.setText(Total);
   }


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_date_result);
    t1=(EditText)findViewById(R.id.id1);
    t2=(EditText)findViewById(R.id.id2);
    t3=(EditText)findViewById(R.id.id3);
    t4=(EditText)findViewById(R.id.id4);

    c= db.rawQuery(SELECT_SQL,null);

    show();
      c.close();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_view_date_result, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

Logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{myapps.com.workhours/myapps.com.workhours.ViewDateResult}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376)
        at android.app.ActivityThread.access$800(ActivityThread.java:147)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)

Date in the query is one of the fields in makeway.db Since I am doing retrieval of data for the first time I cant figure out what mistake I did. Help me out to figure this. Thanks in advance

Aucun commentaire:

Enregistrer un commentaire