0

This is my database helper code. My app is getting crashed when I connect it to database.This is my first time with android. I badly want to get rid of this.Please help me with this. Thanks in advance. dbhelper.java

package com.example.krishchandran.searchez;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.jar.Attributes;
import static android.os.Build.ID;
import static java.sql.Types.VARCHAR;
/*** Created by Krish Chandran on 5/12/2017.
*/

public class DBhelper extends SQLiteOpenHelper {
private static final int VERSION = 1;
private static final String DATABASE_NAME = "SearchEZ.db";
private static final String TABLE_NAME = "searchez";
private static final String col1 = "uid";
private static final String col2 = "pwd";

private static final String create=" create table searchez( uid varchar 
not null ," + "pwd varchar not null;";
SQLiteDatabase db;
/*for constructor, there is no return type */
public DBhelper(Context context) {
    super(context, DATABASE_NAME, null, VERSION); //four parameters
    SQLiteDatabase db = this.getWritableDatabase();// This will create 
your DB and TABLE
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(create);
    this.db=db;
}
public void insertdata(credential c){
    db=this.getWritableDatabase();
    ContentValues values=new ContentValues();

    values.put(col1, c.getUid());
    values.put(col2, c.getPwd());

    db.insert(TABLE_NAME, null, values);
    db.close();
}
public String searchpwd(String uname){
    String a,b;
    b="not found";
    db=this.getReadableDatabase();
    String q="SELECT uid,pwd FROM"+ TABLE_NAME;
    Cursor C= db.rawQuery(q, null);
    if(C.moveToFirst())
    {
        do{
            a=C.getString(0);
            if(a.equals(uname)){
              b=C.getString(1);
                break;
            }
        }while(C.moveToNext());
    }
    return b;

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
 {
 String q="DROP TABLE IF EXISTS"+TABLE_NAME;
    db.execSQL(q);
    this.onCreate(db);

}

}

this is my signup class :

package com.example.krishchandran.searchez;
import android.content.Intent;
import android.net.Credentials;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class Guest extends AppCompatActivity {
DBhelper helper= new DBhelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_guest);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) 
    findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", 
            Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
          }
      });
   }
   public void guestclick(View view) {
    EditText uname= (EditText)findViewById(R.id.editText);
    EditText pwd= (EditText)findViewById(R.id.editText2);

    String unames= uname.getText().toString();
    String pwds= pwd.getText().toString();
    credential C= new credential();
    C.setUid(unames);
    C.setPwd(pwds);

    helper.insertdata(C);


  }

   }

and this is my login class:

  package com.example.krishchandran.searchez;
  import android.content.Intent;
  import android.os.Bundle;
   import android.support.design.widget.FloatingActionButton;
   import android.support.design.widget.Snackbar;
  import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
   import android.view.View;
   import android.widget.EditText;
  import android.widget.Toast;

   public class userlogin extends AppCompatActivity {

DBhelper helper = new DBhelper(this);

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


}

public void loginclick(View view) {
    EditText uname = (EditText) findViewById(R.id.editText);
    EditText pwd = (EditText) findViewById(R.id.editText2);

    String unames = uname.getText().toString();
    String pwds = pwd.getText().toString();
    credential C = new credential();
    C.setUid(unames);
    C.setPwd(pwds);

    String pd = helper.searchpwd(unames);

    if (pwds.equals(pd)) {
        Intent i = new Intent(userlogin.this, second.class);
        i.putExtra("Username",unames);
        startActivity(i);
    }
    else{
        Toast T=Toast.makeText(userlogin.this,"Invalid UID and PWD", 
     Toast.LENGTH_SHORT);
        T.show();
     }
    }
    }

MY LOGCAT:

      05-18 04:00:52.941 12593-12593/? I/art: Late-enabling -Xcheck:jni

05-18 04:00:53.042 12593-12593/com.example.krishchandran.searchez W/System: ClassLoader referenced unknown path: /data/app/com.example.krishchandran.searchez-1/lib/arm64 05-18 04:00:53.051 12593-12593/com.example.krishchandran.searchez I/InstantRun: starting instant run server: is main process 05-18 04:00:53.098 12593-12593/com.example.krishchandran.searchez W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 05-18 04:00:53.162 12593-12621/com.example.krishchandran.searchez D/AppTracker: App Event: start 05-18 04:00:53.187 12593-12622/com.example.krishchandran.searchez I/Adreno: QUALCOMM build : 853a1ff, I9c435c2712 Build Date : 01/10/17 OpenGL ES Shader Compiler Version: XE031.09.00.04 Local Branch : Remote Branch : Remote Branch : Reconstruct Branch : 05-18 04:00:53.191 12593-12622/com.example.krishchandran.searchez I/OpenGLRenderer: Initialized EGL, version 1.4 05-18 04:00:53.191 12593-12622/com.example.krishchandran.searchez D/OpenGLRenderer: Swap behavior 1 05-18 04:00:55.252 12593-12658/com.example.krishchandran.searchez D/AppTracker: App Event: stop 05-18 04:00:55.280 12593-12593/com.example.krishchandran.searchez D/AndroidRuntime: Shutting down VM 05-18 04:00:55.281 12593-12593/com.example.krishchandran.searchez E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.krishchandran.searchez, PID: 12593 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.krishchandran.searchez/com.example.krishchandran.searchez.Guest}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2630) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2814) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6290) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:290) at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223) at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163) at com.example.krishchandran.searchez.DBhelper.(DBhelper.java:31) at com.example.krishchandran.searchez.Guest.(Guest.java:15) at java.lang.Class.newInstance(Native Method) at android.app.Instrumentation.newActivity(Instrumentation.java:1094) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2620) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2814)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6290)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)  05-18 04:00:55.281 12593-12593/? D/AppTracker: App Event: crash 05-18 04:00:55.294 12593-12593/? I/Process: Sending signal. PID: 12593 SIG: 9

  • one thing I see is, that your create string is not correct. You forget the claim at the end. Must be `String create=" create table searchez( uid varchar not null ," + "pwd varchar not null);";` – Opiatefuchs May 18 '17 at 11:49
  • Be aware: If your database is already created at some test and you make some changes in the structure (new or less column), uninstall your app and install again because the database exists. – Opiatefuchs May 18 '17 at 11:51
  • 1
    What's the exception? Post your logcat. – Henry May 18 '17 at 11:51
  • post you log please – xbadal May 18 '17 at 11:54
  • `private static final String create=" create table searchez( uid varchar not null ," + "pwd varchar not null;";` should definitely be `private static final String create="create table searchez(uid varchar not null, pwd varchar not null);";` the bracket is missing, also..why do you need to break the String with a "+", that is normally used because constants are used for those values. – Pararth May 18 '17 at 11:57

1 Answers1

0

Without the error stack, I don´t know if this are the only reasons, buit i see two issues here:

The first, your create String is wrong, you forget the claim at the end. It must be:

String create=" create table searchez( uid varchar not null ," + "pwd varchar not null);";

Second, you missed some spaces:

searchpwd()):

String q="SELECT uid,pwd FROM "+ TABLE_NAME;//space needed after FROM

and in onUpgrade():

String q="DROP TABLE IF EXISTS "+TABLE_NAME; //space needed after EXISTS

Be awar: If you created the database at one point of testing and you make some future changes in the structure (adding or deleting a column), you have to uninstall your app before try it again.

If these are not the issues, you need to show the error stacktrace. Without this, everything is an assumption.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49