getting error when connecting database in android

I am trying to connect the sqlite database with android. I hereby paste the code,

webview.java,


public class webview extends Activity{
EditText ediValue ;
TextView txt;
 DatabaseHandler db;
public void onCreate(Bundle savedInstance)
{

    super.onCreate(savedInstance);
    setContentView(R.layout.activity_main);
    btn.setOnClickListener(new View.OnClickListener() {


    public void onClick(View v) {
        SharedPreferences.Editor editor =     
                                     getPreferences(MODE_PRIVATE).edit();
         editor.putString("text", ediValue.getText().toString());
         editor.putInt("selection-start", ediValue.getSelectionStart());
         editor.putInt("selection-end", ediValue.getSelectionEnd());
         editor.commit();
         String data = getPreferences(MODE_PRIVATE).getString("text","");
         Login login = new Login();
         login.setName(data);
         login.setScore(10);

         db.addScore(login);

    }
});
    }


}

My Login.java is,

public class Login {

 public String name;
 public int score;
 public String getName(){
     return name;
 }
 public int getScore(){
     return score;
 }
 public void setScore(int s){
     score=s;
 }
 public void setName(String name){
     this.name = name;
 }
}

And my Databasehandler.java is,

public class DatabaseHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "ScoreData ";
private static final String TABLE_SCORE = "score";
private static final String KEY_SCORE = "score";
private static final String KEY_NAME = "name";

 public DatabaseHandler(Context context) {
   super(context, null, null, DATABASE_VERSION);
   }
  public void onCreate(SQLiteDatabase db) {

   String CREATE_SCORE_TABLE="CREATE TABLE " + TABLE_SCORE +"(" + KEY_NAME + " TEXT," + 
             KEY_SCORE+" INTEGER" + ")";
   db.execSQL(CREATE_SCORE_TABLE);
   }
   public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_SCORE);
       onCreate(db);
   }


   public void addScore(Login login) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_SCORE, login.getScore());
    values.put(KEY_NAME, login.getName());
    db.insert(TABLE_SCORE, null, values);
    db.close();
   }
   }

And now i am getting nullpointerexception, when i run this activity. Can anyone tell what is wrong in this code. I use android 4.2. Apart from the code, i did not do any changes in db side. I hereby post the exception,

05-02 09:00:44.635: E/Trace(1033): error opening trace file: No such file or directory (2)
05-02 09:00:44.875: E/AndroidRuntime(1033): FATAL EXCEPTION: main
05-02 09:00:44.875: E/AndroidRuntime(1033): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.newandroid/com.example.newandroid.webview}: java.lang.NullPointerException
05-02 09:00:44.875: E/AndroidRuntime(1033):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at android.os.Looper.loop(Looper.java:137)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at java.lang.reflect.Method.invokeNative(Native Method)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at java.lang.reflect.Method.invoke(Method.java:511)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at dalvik.system.NativeStart.main(Native Method)
05-02 09:00:44.875: E/AndroidRuntime(1033): Caused by: java.lang.NullPointerException
05-02 09:00:44.875: E/AndroidRuntime(1033):     at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at com.example.newandroid.webview.<init>(webview.java:17)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at java.lang.Class.newInstanceImpl(Native Method)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at java.lang.Class.newInstance(Class.java:1319)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
05-02 09:00:44.875: E/AndroidRuntime(1033):     ... 11 more
05-02 09:03:42.775: E/AndroidRuntime(1414): FATAL EXCEPTION: main

Answers


You have to initialize the dataBase first like

DatabaseHandler db = new DatabaseHandler(this);

and then

db.addScore(new Login("Your values", "Your Values"));

I guess this would work.


In your webview Activity you specify this:

 btn.setOnClickListener(new View.OnClickListener() {

But you never declared the button. Make sure you declare the button first using something like this:

Button btn = (Button) findViewById(R.id.yourbutton);

Need Your Help

JavaScript to Redirect Users To Page Depending On The Address They Are Accessing The Web Stie With

javascript php scripting redirect

First i would like to start off saying that i am sorry if someone else has posted something about this. And Secondly i am new to html and JavaScript so i need a step by step explanation. I am worki...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.