Application stop suddenly when tring to login-Collection of common programming errors

After creating inserting username and password signup.java , my app suddenly stop when I login ( in emulator) , and i moves to signup page in (bluestack). Not sure if data is actually being entered in database in signing up.

DBManager.java

package com.example.student_project;

import com.example.student_project.*;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;


public class DBManager  {
    public static final String KEY_ROWID = "_id";
    public static final String KEY_USERNAME = "username";
    public static final String KEY_PASSWORD = "password";


    private static final String DATABASE_NAME= "LOGIN.db";
    private static final int DATABASE_VERSION  = 4;
    private static final String  DATABASE_TABLE = "LOGIN_TABLE";

    private static DbHelper ourHelper;
    private final Context ourContext;
    private static SQLiteDatabase ourDatabase;

    class DbHelper extends SQLiteOpenHelper {

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            // TODO Auto-generated method stub
            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " ( " + 
            KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT , " +
            KEY_USERNAME + " TEXT NOT NULL , " + 
            KEY_PASSWORD + " TEXT NOT NULL );" 
            );
                }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            onCreate(db);
        }

    }


    public DBManager(Context c) {
    ourContext = c;
    }

    public DBManager open() throws SQLException{
        ourHelper = new DbHelper(ourContext);
        try
        {
            ourDatabase = ourHelper.getWritableDatabase();
        }
        catch(SQLException ex)
        {
            ourDatabase = ourHelper.getReadableDatabase();
        }
        return this;
    }

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

    public long createEntry(String username, String password) throws SQLException
    {
        // TODO Auto-generated method stub
        ContentValues cv = new ContentValues();
        cv.put(KEY_USERNAME, username);
        cv.put(KEY_PASSWORD, password);
        return ourDatabase.insert(DATABASE_TABLE, null, cv);

    }

    public static boolean verifyUser(String username , String password) 
    {

        String columns[] = new String[2];
        columns[0] = username;
        columns[1] = password;
        Cursor c= null;
        int count=0;
        try
        {

                // TODO Auto-generated method stub
                c = ourDatabase.query(DATABASE_TABLE, columns,null , null, null, null, null);
                count = c.getCount();

        }
        catch (SQLException e)
        {
            // TODO: handle exception
            e.printStackTrace();
        }
        finally
        {
            c.close();
        }
        if(count>0)
        {
            return true;
        }
        else
        {
            return false;
        }

    }
}

MainActivity.java

package com.example.student_project;

import com.example.student_project.*;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.InputFilter.LengthFilter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener
{
    private Button btn_signup;
    private Button btn_login;
    private EditText et_lusername;
    private EditText et_lpassword;

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

        Log.i("mytag","this is my tag");
        et_lusername = (EditText) findViewById(R.id.et_lusername);
        et_lpassword = (EditText) findViewById(R.id.et_lpassword);
        btn_signup = (Button) findViewById(R.id.btn_signup);
        btn_login = (Button) findViewById(R.id.btn_login);

        btn_login.setOnClickListener(this);
        btn_signup.setOnClickListener(this);

    }

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


    @Override
    public void onClick(View v)
    {
        // TODO Auto-generated method stub
        switch(v.getId())
        {
            case R.id.btn_login :
            {
                String un = et_lusername.getText().toString();
                String pw = et_lusername.getText().toString();

                boolean success = DBManager.verifyUser(un,pw);
                if (success)
                {
                    Intent i = new Intent(MainActivity.this,AdminMenu.class);
                    startActivity(i);   
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "wrong username or password",Toast.LENGTH_LONG);
                }


            }
            case R.id.btn_signup:
            {
                Intent i = new Intent(MainActivity.this,Signup.class);
                startActivity(i);   
            }
        }
    }


    }

Signup.java

package com.example.student_project;


import com.example.student_project.*;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
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 Signup extends Activity implements OnClickListener {
private Button btn_add;
private EditText et_username;
private EditText et_password;
private EditText et_confirmpassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.signup);

    et_username = (EditText) findViewById(R.id.et_username);
    et_password = (EditText) findViewById(R.id.et_password);
    et_confirmpassword = (EditText) findViewById(R.id.et_confirmpassword);
    btn_add = (Button) findViewById(R.id.btn_add);

    btn_add.setOnClickListener(this);
}

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

    return true;
}

@Override
public void onClick(View v)
{
    // TODO Auto-generated method stub
    switch(v.getId())
    {
    case R.id.btn_add :

        boolean diditwork = true;        
         try 
         {
            String username = et_username.getText().toString();
             String password = et_password.getText().toString();
             String cpassword = et_confirmpassword.getText().toString();

             if(password.equalsIgnoreCase(cpassword))
             {
                 DBManager empty = new DBManager(Signup.this);
                 empty.open();
                 empty.createEntry(username,password);
                 empty.close(); 

                 Intent addintent = new Intent(Signup.this,MainActivity.class);
                    startActivity(addintent);
             }
             else 
             {
                 Toast.makeText(getApplicationContext(), "passwords does not match", Toast.LENGTH_LONG);
             }
         } 
         catch (Exception e)
         {
            diditwork = false;
            Dialog d = new Dialog(this);
            String error = e.toString();
            d.setTitle("dang");
            TextView tv = new TextView(this);
            tv.setText(error);
            d.setContentView(tv);
            d.show();
        }
        /* finally
         {
            if(diditwork)
            {
            Dialog d = new Dialog(this);
            d.setTitle("hech ya");
            TextView tv = new TextView(this);
            tv.setText("success");
            d.setContentView(tv);
            d.show();
            }

        } */


    }
}

}

Admin.java

    package com.example.student_project;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AdminMenu extends Activity
{

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



}

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

}

logcat error

    W/ActivityManager(  293): Unbind failed: could not find connection for android.os.BinderProxy@410ce000

I/Choreographer( 1994): Skipped 55 frames!  The application may be doing too much work on its main thread.

D/AndroidRuntime( 1994): Shutting down VM

W/dalvikvm( 1994): threadid=1: thread exiting with uncaught exception (group=0x40a71930)

E/AndroidRuntime( 1994): FATAL EXCEPTION: main

E/AndroidRuntime( 1994): java.lang.NullPointerException

E/AndroidRuntime( 1994):    at com.example.student_project.DBManager.verifyUser(DBManager.java:110)

E/AndroidRuntime( 1994):    at com.example.student_project.MainActivity.onClick(MainActivity.java:61)

E/AndroidRuntime( 1994):    at android.view.View.performClick(View.java:4204)

E/AndroidRuntime( 1994):    at android.view.View$PerformClick.run(View.java:17355)

E/AndroidRuntime( 1994):    at android.os.Handler.handleCallback(Handler.java:725)

E/AndroidRuntime( 1994):    at android.os.Handler.dispatchMessage(Handler.java:92)

E/AndroidRuntime( 1994):    at android.os.Looper.loop(Looper.java:137)

E/AndroidRuntime( 1994):    at android.app.ActivityThread.main(ActivityThread.java:5041)

E/AndroidRuntime( 1994):    at java.lang.reflect.Method.invokeNative(Native Method)

E/AndroidRuntime( 1994):    at java.lang.reflect.Method.invoke(Method.java:511)

E/AndroidRuntime( 1994):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

E/AndroidRuntime( 1994):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

E/AndroidRuntime( 1994):    at dalvik.system.NativeStart.main(Native Method)

W/ActivityManager(  293):   Force finishing activity com.example.student_project/.MainActivity

W/WindowManager(  293): Failure taking screenshot for (246x410) to layer 21020
  1. It is because your Cursor c is null..and u have’nt caught the NullPointerException in the catch block in verifyUser()..change the SQLException in the catch to Exception to catch all the exceptions..

  2. You are calling DBManager.verifyUser(un,pw); which reqires database to be opened. Probably ourDatabase variable is Null and you got handled exception when trying to get cursor and unhandled when trying to access null cursor.

    Probably you do not need verifyUser to bee static as you need to call DBManager.open before it

Originally posted 2013-11-26 18:03:36.