Reputation: 13
hello i'm developing an application for students in college & i'm trying to add sqlite database but whenevr i try to insert data to the database like the student register in the app when i press button register the app crashes actually i'm new to the android world N i'm seeing some tutorials to know what to do >>>N i dunno what the mistake hope anyone can help me
heres my code
this is the openhelper
public class DataBaseopretions extends SQLiteOpenHelper {
public static final int database_version=1;
public String CREATE_QUERY="CREATE_TABLE"+Tableinfo.TABLE_NAME+"("+ Tableinfo.ST_ID +" TEXT,"
+ Tableinfo.ST_pass + " TEXT);";
public DataBaseopretions(Context context) {
super(context, Tableinfo.TABLE_NAME , null, database_version);
// TODO Auto-generated constructor stub
Log.d("Database opreations", " database created");
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_QUERY);
Log.d("Database opreations", " table created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public void putinformation(DataBaseopretions dop ,String st_id ,String pass){
SQLiteDatabase SQ=dop.getWritableDatabase();
ContentValues CV=new ContentValues();
CV.put(Tableinfo.ST_ID, st_id);
CV.put(Tableinfo.ST_pass, pass);
long k= SQ.insert(Tableinfo.TABLE_NAME, null, CV);
Log.d("Database opreation", "one row insearted");
}
}
this where i created the database
public class Tabledata {
public Tabledata(){
}
public static abstract class Tableinfo implements BaseColumns {
public static final String ST_ID="studentId";
public static final String ST_pass="pass";
public static final String DATABASE_NAME="colleg";
public static final String TABLE_NAME="student";
}
}
n this is the registration
public class Rigstretion extends Activity {
EditText st_id , st_pass, Pass_again;
String student_id ,pass_ag ,pass;
Button regs, cancel_reg;
Context cxt;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.registor);
st_id=(EditText)findViewById(R.id.st_id);
st_pass=(EditText)findViewById(R.id.st_pass);
Pass_again=(EditText)findViewById(R.id.pass_again);
regs=(Button)findViewById (R.id.regs) ;
cancel_reg=(Button) findViewById(R.id.cancel_reg);
regs.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
student_id=st_id.getText().toString();
pass=st_pass.getText().toString();
pass_ag=Pass_again.getText().toString();
if(!(pass.equals(pass_ag)))
{
Toast.makeText(getBaseContext(), "Password not match", Toast.LENGTH_LONG).show();
st_id.setText("");
st_pass.setText("");
Pass_again.setText("");
}
else
{
DataBaseopretions db =new DataBaseopretions(cxt);
db.putinformation(db, student_id, pass);
Toast.makeText(getBaseContext(), "Registrtion success", Toast.LENGTH_LONG).show();
finish();
}
}
});
}
& this the logcat
10-15 08:49:05.477: W/SocketClient(110): write error (Broken pipe)
10-15 08:49:06.213: D/OpenGLRenderer(1057): TextureCache::get: create texture(0xb804b280): name, size, mSize = 185, 3328, 1618848
10-15 08:49:06.281: W/audio_hw_primary(116): out_write() limiting sleep time 46802 to 39909
10-15 08:49:06.285: D/Database opreations(1057): database created
10-15 08:49:06.285: D/AndroidRuntime(1057): Shutting down VM
10-15 08:49:06.285: W/dalvikvm(1057): threadid=1: thread exiting with uncaught exception (group=0xa6265288)
10-15 08:49:06.293: E/AndroidRuntime(1057): FATAL EXCEPTION: main
10-15 08:49:06.293: E/AndroidRuntime(1057): java.lang.NullPointerException
10-15 08:49:06.293: E/AndroidRuntime(1057): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
10-15 08:49:06.293: E/AndroidRuntime(1057): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
10-15 08:49:06.293: E/AndroidRuntime(1057): at za.example.drawer.DataBaseopretions.putinformation(DataBaseopretions.java:40)
10-15 08:49:06.293: E/AndroidRuntime(1057): at za.example.drawer.Rigstretion$1.onClick(Rigstretion.java:48)
10-15 08:49:06.293: E/AndroidRuntime(1057): at android.view.View.performClick(View.java:4084)
10-15 08:49:06.293: E/AndroidRuntime(1057): at android.view.View$PerformClick.run(View.java:16966)
10-15 08:49:06.293: E/AndroidRuntime(1057): at android.os.Handler.handleCallback(Handler.java:615)
10-15 08:49:06.293: E/AndroidRuntime(1057): at android.os.Handler.dispatchMessage(Handler.java:92)
10-15 08:49:06.293: E/AndroidRuntime(1057): at android.os.Looper.loop(Looper.java:137)
10-15 08:49:06.293: E/AndroidRuntime(1057): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-15 08:49:06.293: E/AndroidRuntime(1057): at java.lang.reflect.Method.invokeNative(Native Method)
10-15 08:49:06.293: E/AndroidRuntime(1057): at java.lang.reflect.Method.invoke(Method.java:511)
10-15 08:49:06.293: E/AndroidRuntime(1057): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-15 08:49:06.293: E/AndroidRuntime(1057): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-15 08:49:06.293: E/AndroidRuntime(1057): at dalvik.system.NativeStart.main(Native Method)
10-15 08:49:06.293: W/ActivityManager(394): Force finishing activity za.example.drawer/.Rigstretion
10-15 08:49:06.341: E/SocketStream(437): readFully was waiting for 275772 bytes, got 49152
10-15 08:49:06.341: E/SocketStream(437): readFully was waiting for 226620 bytes, got 49152
10-15 08:49:06.345: E/SocketStream(437): readFully was waiting for 177468 bytes, got 49152
10-15 08:49:06.345: E/SocketStream(437): readFully was waiting for 128316 bytes, got 49152
10-15 08:49:06.345: E/SocketStream(437): readFully was waiting for 79164 bytes, got 492
10-15 08:49:06.349: E/SocketStream(437): readFully was waiting for 78672 bytes, got 49152
10-15 08:49:06.353: E/SocketStream(437): readFully was waiting for 29520 bytes, got 16384
10-15 08:49:06.357: W/audio_hw_primary(116): out_write() limiting sleep time 59274 to 39909
10-15 08:49:06.365: W/audio_hw_primary(116): out_write() limiting sleep time 49591 to 39909
10-15 08:49:06.377: D/dalvikvm(394): GC_FOR_ALLOC freed 494K, 9% free 9185K/10055K, paused 14ms, total 17ms
10-15 08:49:06.417: W/audio_hw_primary(116): out_write() limiting sleep time 69546 to 39909
10-15 08:49:06.453: W/audio_hw_primary(116): out_write() limiting sleep time 79501 to 39909
10-15 08:49:06.493: D/(394): HostConnection::get() New Host Connection established 0xb7f9c4b8, tid 408
10-15 08:49:06.493: W/audio_hw_primary(116): out_write() limiting sleep time 59455 to 39909
10-15 08:49:06.881: W/ActivityManager(394): Activity pause timeout for ActivityRecord{5358f428 za.example.drawer/.Rigstretion}
10-15 08:49:16.385: W/ActivityManager(394): Launch timeout has expired, giving up wake lock!
10-15 08:49:16.893: W/ActivityManager(394): Activity idle timeout for ActivityRecord{535c3ef0 za.example.drawer/.MainActivity}
10-15 08:49:31.553: W/ThrottleService(394): unable to find stats for iface rmnet0
10-15 08:49:31.561: W/SocketClient(110): write error (Broken pipe)
10-15 08:49:32.805: D/OpenGLRenderer(510): TextureCache::get: create texture(0xb8101c90): name, size, mSize = 51, 1440, 162152
10-15 08:50:05.497: W/SocketClient(110): write error (Broken pipe)
i searched for solutions N nothing is working ... thanks in advance ^^
Upvotes: 0
Views: 268
Reputation: 13
thanks guys i found out what the mistake .. it was near the create table
in the oncreate ()
all i did was i delete CREATE_QUERY
IN THE public class DataBaseopretions extends SQLiteOpenHelper
and i wrote the create table statements in the onecreate()
method
like this and it works
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + Tableinfo.DATABASE_TABLE + " (" + Tableinfo.ST_ID + " INTEGER PRIMARY KEY , " +
Tableinfo.ST_PASS +" TEXT NOT NULL, " + Tableinfo.PASS_AG + " TEXT NOT NULL ); "
);
Upvotes: 1
Reputation: 16833
cxt
var is not initialized, so null is passed as parameter to new DataBaseopretions
. Anyway, cxt
is useless, just do like that :
DataBaseopretions db =new DataBaseopretions(Rigstretion.this);
Upvotes: 0