1. Download our Official Android App: Forums for Android!

Record Retrieval Problem

Discussion in 'Android Help' started by Android Question, Jan 13, 2014.

  1. Android Question

    Thread Starter
    Rank:
    None
    Posts:
    0
    Joined:

    Let COnsider this logcat


    01-13 15:24:11.634: E/AndroidRuntime(3781): FATAL EXCEPTION: main
    01-13 15:24:11.634: E/AndroidRuntime(3781): java.lang.NullPointerException
    01-13 15:24:11.634: E/AndroidRuntime(3781): at android.widget.Toast.<init>(Toast.java:92)
    01-13 15:24:11.634: E/AndroidRuntime(3781): at android.widget.Toast.makeText(Toast.java:238)
    01-13 15:24:11.634: E/AndroidRuntime(3781): at com.ProgramingTutorials.sqlite.Database.getCandidate(Database.java:96)
    01-13 15:24:11.634: E/AndroidRuntime(3781): at com.ProgramingTutorials.sqlite.ViewCandidate$1.onClick(ViewCandidate.java:48)
    01-13 15:24:11.634: E/AndroidRuntime(3781): at android.view.View.performClick(View.java:4091)
    01-13 15:24:11.634: E/AndroidRuntime(3781): at android.view.View$PerformClick.run(View.java:17072)
    01-13 15:24:11.634: E/AndroidRuntime(3781): at android.os.Handler.handleCallback(Handler.java:615)
    01-13 15:24:11.634: E/AndroidRuntime(3781): at android.os.Handler.dispatchMessage(Handler.java:92)
    01-13 15:24:11.634: E/AndroidRuntime(3781): at android.os.Looper.loop(Looper.java:153)
    01-13 15:24:11.634: E/AndroidRuntime(3781): at android.app.ActivityThread.main(ActivityThread.java:5086)
    01-13 15:24:11.634: E/AndroidRuntime(3781): at java.lang.reflect.Method.invokeNative(Native Method)
    01-13 15:24:11.634: E/AndroidRuntime(3781): at java.lang.reflect.Method.invoke(Method.java:511)
    01-13 15:24:11.634: E/AndroidRuntime(3781): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
    01-13 15:24:11.634: E/AndroidRuntime(3781): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
    01-13 15:24:11.634: E/AndroidRuntime(3781): at dalvik.system.NativeStart.main(Native Method)

    /////////////////////////////////////////////////////////

    Here is the Another class who control SQLite

    package com.ProgramingTutorials.sqlite;


    import android.annotation.SuppressLint;
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.DatabaseErrorHandler;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteDatabase.CursorFactory;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.util.Log;
    import android.widget.Toast;

    public class Database extends SQLiteOpenHelper {


    private static final String KEY_ID="id";
    private static final String KEY_NAME="name";
    private static final String KEY_ADDRESS="address";
    private static final String TABLE_NAME="Candidate";
    private static final String DATABASE_NAME="odesk";
    private SQLiteDatabase db;
    Context context;


    public Database(Context context) {
    super(context, DATABASE_NAME, null, 1);
    this.context=context;
    }


    public void close(){
    db.close();
    }

    public boolean addCandidate(Candidate c) {
    db=this.getWritableDatabase();


    try{

    ContentValues value=new ContentValues();
    value.put(KEY_ID, c.getId());
    value.put(KEY_NAME, c.getName());
    value.put(KEY_ADDRESS, c.getAddress());

    if(db.insert(TABLE_NAME, null, value)!=-1){
    Log.d("Insert", "Data Inserted");
    Toast.makeText(context, "Data is Inserted", Toast.LENGTH_SHORT).show();
    return true;

    }
    else{
    return false;
    }
    }
    catch(Exception e){
    Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
    return false;
    }



    }


    //Get the record of Candidate from SQLITE

    public Candidate getCandidate(int i){

    try{
    db=this.getReadableDatabase();


    String sql="select * from Candidate where id='"+i+"' ";

    //Cursor cursor=db.rawQuery(sql, null);
    Cursor cursor=db.query(TABLE_NAME, new String[]{KEY_ID,KEY_NAME,KEY_ADDRESS}, KEY_ID+ "=" +i, null, null, null, null);
    Candidate c;
    if(cursor!=null){
    cursor.moveToFirst();
    c=new Candidate();
    c.setId(cursor.getInt(0));
    c.setName(cursor.getString(1));
    c.setAddress(cursor.getString(2));
    db.close();
    return c;
    }
    else{
    db.close();
    return null;
    }
    }catch(Exception e){
    Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
    return null;
    }
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table Candidate (id int primary key,name varchar(255),address varchar(255))");
    Log.d("CREATED", "DATABASE Created");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }


    }



    ///////////////////////////////////////////////////////////////////////////////
    here is the Candidate CClass

    package com.ProgramingTutorials.sqlite;


    import android.annotation.SuppressLint;
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.DatabaseErrorHandler;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteDatabase.CursorFactory;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.util.Log;
    import android.widget.Toast;

    public class Database extends SQLiteOpenHelper {


    private static final String KEY_ID="id";
    private static final String KEY_NAME="name";
    private static final String KEY_ADDRESS="address";
    private static final String TABLE_NAME="Candidate";
    private static final String DATABASE_NAME="odesk";
    private SQLiteDatabase db;
    Context context;


    public Database(Context context) {
    super(context, DATABASE_NAME, null, 1);
    this.context=context;
    }


    public void close(){
    db.close();
    }

    public boolean addCandidate(Candidate c) {
    db=this.getWritableDatabase();


    try{

    ContentValues value=new ContentValues();
    value.put(KEY_ID, c.getId());
    value.put(KEY_NAME, c.getName());
    value.put(KEY_ADDRESS, c.getAddress());

    if(db.insert(TABLE_NAME, null, value)!=-1){
    Log.d("Insert", "Data Inserted");
    Toast.makeText(context, "Data is Inserted", Toast.LENGTH_SHORT).show();
    return true;

    }
    else{
    return false;
    }
    }
    catch(Exception e){
    Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
    return false;
    }



    }


    //Get the record of Candidate from SQLITE

    public Candidate getCandidate(int i){

    try{
    db=this.getReadableDatabase();


    String sql="select * from Candidate where id='"+i+"' ";

    //Cursor cursor=db.rawQuery(sql, null);
    Cursor cursor=db.query(TABLE_NAME, new String[]{KEY_ID,KEY_NAME,KEY_ADDRESS}, KEY_ID+ "=" +i, null, null, null, null);
    Candidate c;
    if(cursor!=null){
    cursor.moveToFirst();
    c=new Candidate();
    c.setId(cursor.getInt(0));
    c.setName(cursor.getString(1));
    c.setAddress(cursor.getString(2));
    db.close();
    return c;
    }
    else{
    db.close();
    return null;
    }
    }catch(Exception e){
    Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
    return null;
    }
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table Candidate (id int primary key,name varchar(255),address varchar(255))");
    Log.d("CREATED", "DATABASE Created");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }


    }
    /////////////////////////////////////////////////////////////////////////////


    Here is the ViewCandidate activityclass for get the record

    package com.ProgramingTutorials.sqlite;

    import android.app.Activity;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.provider.ContactsContract.Data;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;

    public class ViewCandidate extends Activity {

    Button searchButton;
    TextView idTextView;
    TextView nameTextView;
    TextView addressTextView;
    EditText candidateEditText;
    Database db=new Database(getBaseContext());


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_candidate);



    this.searchButton=(Button) findViewById(R.id.buttonSearchByName);
    this.candidateEditText=(EditText) findViewById(R.id.editTextSearchByName);
    this.idTextView=(TextView) findViewById(R.id.editTextID);
    this.nameTextView=(TextView) findViewById(R.id.textViewCandidateName);
    this.addressTextView=(TextView) findViewById(R.id.textViewAddress);


    this.searchButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {


    String val=candidateEditText.getText().toString();

    int id=Integer.parseInt(val);
    Candidate c=db.getCandidate(id);
    Toast.makeText(getApplicationContext(), val, Toast.LENGTH_SHORT).show();

    if(c!=null){

    idTextView.setText(""+c.getId());
    nameTextView.setText(""+c.getName());
    addressTextView.setText(""+c.getAddress());}


    }
    });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.view_candidate, menu);
    return true;
    }

    }
    ////////////////////////////////
    Kindly Suggest me what is the problem...
    Whenever I try get the record from database the my application has closed...
     

    Advertisement

Share This Page

Loading...