• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps need help

cmsm

Lurker
May 17, 2012
2
0
i cant seem to get it to talk to the database and don't know what to do its for a class project it gets to the emulator and then shuts down when i input the username and password please help


package com.cms.log;

import java.io.IOException;
import java.sql.*;
import android.app.Activity;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class LogActivity extends Activity {
EditText StudentID;
EditText password;
Button btnSubmit;
int count = 0;



private static SQLiteDatabase Project2;
//private DatabaseHelper mDbHelper;


private static String DB_PATH = "/log/assets/MyGrades.db";
/** Called when the activity is first created. */

public void openDataBase() throws SQLException {
// Open the database
String myPath = DB_PATH;
Project2 = SQLiteDatabase.openDatabase(myPath,
null, SQLiteDatabase.OPEN_READWRITE);
openDataBase();
}

public void close() {
Cursor mDbHelper = null;
mDbHelper.close();
}

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

addListenerOnButton();
}
public void addListenerOnButton() {
StudentID = (EditText) findViewById(R.id.txtStudentID);
password = (EditText) findViewById(R.id.password);
btnSubmit = (Button) findViewById(R.id.btnLogin);

btnSubmit.setOnClickListener(new OnClickListener() {




public void onClick(View v) {
// TODO Auto-generated method stub

Cursor cursor = Project2.rawQuery(
"Select Count(*) From Student Where Login = 'StudentID.getText().toString()' and Password = 'password.getText().toString()'", null);
if (cursor.moveToFirst()) {
count = cursor.getInt(0);

}
if(count > 0 ){
Toast.makeText(LogActivity.this, "Login Successful",Toast.LENGTH_LONG).show();
} else{
Toast.makeText(LogActivity.this, "Invalid Login",Toast.LENGTH_LONG).show();
}




}
});
}}
 
05-18 01:25:39.108: E/AndroidRuntime(602): FATAL EXCEPTION: main
05-18 01:25:39.108: E/AndroidRuntime(602): java.lang.NullPointerException
05-18 01:25:39.108: E/AndroidRuntime(602): at com.cms.log.LogActivity$1.onClick(LogActivity.java:64)
05-18 01:25:39.108: E/AndroidRuntime(602): at android.view.View.performClick(View.java:3511)
05-18 01:25:39.108: E/AndroidRuntime(602): at android.view.View$PerformClick.run(View.java:14105)
05-18 01:25:39.108: E/AndroidRuntime(602): at android.os.Handler.handleCallback(Handler.java:605)
05-18 01:25:39.108: E/AndroidRuntime(602): at android.os.Handler.dispatchMessage(Handler.java:92)
05-18 01:25:39.108: E/AndroidRuntime(602): at android.os.Looper.loop(Looper.java:137)
05-18 01:25:39.108: E/AndroidRuntime(602): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-18 01:25:39.108: E/AndroidRuntime(602): at java.lang.reflect.Method.invokeNative(Native Method)
05-18 01:25:39.108: E/AndroidRuntime(602): at java.lang.reflect.Method.invoke(Method.java:511)
05-18 01:25:39.108: E/AndroidRuntime(602): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-18 01:25:39.108: E/AndroidRuntime(602): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-18 01:25:39.108: E/AndroidRuntime(602): at dalvik.system.NativeStart.main(Native Method)
 
Upvote 0
I don't think you have called your "openDataBase()" function. The function itself is recursive (openDataBase() calls itself) so it will never finish executing if you do call it.

The logcat error message tells you pretty much exactly what the problem is:

Code:
05-18 01:25:39.108: E/AndroidRuntime(602): FATAL EXCEPTION: main
05-18 01:25:39.108: E/AndroidRuntime(602): java.lang.NullPointerException
05-18 01:25:39.108: E/AndroidRuntime(602): at com.cms.log.LogActivity$1.onClick(LogActivity.java :64)
The first line says NullPointerException - in Java that generally means you are trying to use an object that you haven't constructed. The second line tells you that the error occurred on line 64 of LogActivity.java. I think that is this line (but you need to check as I don't know if you have any extra blank lines at the top of your source code):

Code:
Cursor cursor = Project2.rawQuery(
The only object which is used on this line is "Project2".
 
  • Like
Reactions: cmsm
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones