4 Replies - 2230 Views - Last Post: 15 January 2014 - 07:44 PM

#1 yasirshabbir44  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-January 14

Record Retrieval Problem in android sqlite

Posted 13 January 2014 - 03:47 AM

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.getCandida te(Database.java:96)
01-13 15:24:11.634: E/AndroidRuntime(3781): at com.ProgramingTutorials.sqlite.ViewCandidate$1.onC lick(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.jav a: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$MethodAndArgsCa ller.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.CursorFacto ry;
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.CursorFacto ry;
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...

This post has been edited by modi123_1: 13 January 2014 - 09:37 AM
Reason for edit:: please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Record Retrieval Problem in android sqlite

#2 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 267
  • View blog
  • Posts: 547
  • Joined: 07-July 10

Re: Record Retrieval Problem in android sqlite

Posted 14 January 2014 - 10:01 AM

Look at your LogCat. There is a NullPointerException What is the first message that references a class you created? It tells you the problem happened at Database.java:96.

Line 96:
Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();

Toast.makeText is a static method. That is not null.
e is a thrown exception. That is not null.
Toast.LENGTH_SHORT is a static field. That is not null.

So the only possibilities are that the makeText method returns null or the context is null. My guess is that the context is null. context is injected in via the constructor.

My guess (and this is only a guess) is that you create the Database object and pass it around. By the time you use it, the Activity that created the Context injected is destroyed. You don't control when an Activity is destroyed if it is not being displayed. I would inject the Context object to the method (or let the calling Activity create it) and see what happens.

Then you need to debug what error is causing this line to run. My guess is that it is a similar problem.
Was This Post Helpful? 0
  • +
  • -

#3 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 193
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Record Retrieval Problem in android sqlite

Posted 14 January 2014 - 11:16 AM

My guess is that e.getMessage() is null.
It's not rare when using base exception.
Try changing e.getMessage() to like "Ops! Something went wrong!"
Was This Post Helpful? 0
  • +
  • -

#4 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: Record Retrieval Problem in android sqlite

Posted 15 January 2014 - 07:30 PM

Forgive me if I am wrong but does the error occur initially on line 48?

01-13 15:24:11.634: E/AndroidRuntime(3781): at com.ProgramingTutorials.sqlite.ViewCandidate$1.onC lick(ViewCandidate.java:48)


What is on line 48?

as a guess it may be here

this.searchButton.setonclickListener(new onclickListener()

Was This Post Helpful? 0
  • +
  • -

#5 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 193
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Record Retrieval Problem in android sqlite

Posted 15 January 2014 - 07:44 PM

i just pulled out only the code for the ViewCandidate class, and at line 48 you call getCandidate(id);
and if we go into the database class, and look at line 96, you'll find Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();

if you look a few lines above it, you'll see that it is inside of the getCandidate(int id) function. Because we have now traced it (using the StackTrace you posted)
So we know for sure where the error is.
Try to first change e.getMessage() to something like "Ops! Something went wrong!"
If if still gives you the same error, then you'll have to see if it is the context that is null.
And if the context is null, you'll have to trace it to see where you're setting it (or should be but aren't),
but that would take longer then just presetting an error message as of in my first reply :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1