I am a new android developer and am using SQlite for first time. I have a NullPointerException while creating Table and tried a lot to sort out the answer but was not able to. Am thanking in advance for your valuable time and help. Please find the code below:
package com.example.kannan.football_manager;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Created by kannan on 25/1/16.
*/
public class Admin_Database_Operations extends SQLiteOpenHelper
{
public static final String ADMIN_DB="admin_info";
public static final String ADMIN_TB="abmin_table";
public static final String ADMIN_NAME="admin_name";
public static final String ADMIN_PASS="admin_pass";
public static final String COLUMN_ID="column_id";
public static final int database_version=1;
//creating database
public Admin_Database_Operations(Context c)
{
super(c, ADMIN_DB,null,database_version);
Log.d("database creation","database created");
}
@Override
public void onCreate(SQLiteDatabase sqls) {
String CREATE_PRODUCTS_TABLE = "CREATE TABLE " +
ADMIN_TB + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + ADMIN_NAME
+ " TEXT," + ADMIN_PASS + " TEXT" + ")";
sqls.execSQL(CREATE_PRODUCTS_TABLE);
Log.d("Database Operation","TABLE CREATED IN DATABASE");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public SQLiteDatabase sq;
public void putInformation(Admin_Database_Operations ado,String name,String pass)
{
sq=ado.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(ADMIN_NAME,name);
cv.put(ADMIN_PASS,pass);
sq.insert(ADMIN_TB,null,cv);
sq.close();
}
}
package com.example.kannan.football_manager;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
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.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class LoginActivity extends AppCompatActivity {
public Button btn_Admin;
public Button btn_Cancel;
public Button btn_Login;
public Button btn_cngepsw;
public EditText edttxt_name;
public EditText edttxt_pass;
public SQLiteDatabase sqldb;
public Context ctx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//Declaring Nodes
btn_Admin=(Button) findViewById(R.id.btn_Admin);
btn_Cancel=(Button) findViewById(R.id.btn_Cancel);
btn_Login=(Button) findViewById(R.id.btn_Login);
btn_cngepsw=(Button) findViewById(R.id.btn_cngepsw);
edttxt_name=(EditText) findViewById(R.id.edttxt_name);
edttxt_pass=(EditText) findViewById(R.id.edttxt_pass);
btn_Login.setOnClickListener(
new Button.OnClickListener()
{
public void onClick(View v)
{
String name="kannan";
String pass="pass";
Admin_Database_Operations ado=new Admin_Database_Operations(ctx);
ado.onCreate(sqldb);
ado.putInformation(ado, name, pass);
Log.d("Database operation", "INSERT ONE RAW");
}
}
);
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();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_login, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
//Error
01-30 17:06:15.253 3677-3677/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kannan.football_manager, PID: 3677
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.database.sqlite.SQLiteDatabase.execSQL(java.lang.String)' on a null object reference
at com.example.kannan.football_manager.Admin_Database_Operations.onCreate(Admin_Database_Operations.java:40)
at com.example.kannan.football_manager.LoginActivity$1.onClick(LoginActivity.java:54)
at android.view.View.performClick(View.java:4761)
at android.view.View$PerformClick.run(View.java:19767)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Aucun commentaire:
Enregistrer un commentaire