0

After following a youtube tutorial, I wrote some Code which should create a database and a table.

When I load the app up and click on the Magic Words Button, the app crashes and shuts down.
The database is also not created, but I'm presuming this is because the app crashes.

Any ideas why it crashes?

Magic Words

public class Magic_Words extends AppCompatActivity {
    MyDBHelper myDb;
    EditText edittxt_Ring, edittxt_Locate, edittxt_Lock, edittxt_Wipe;
    Button btn_Save;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_magic__words);
        //myDb = new DataBaseHelper(this);
        myDb = new MyDBHelper(this);

        edittxt_Ring = (EditText) findViewById(R.id.txt_Ring);
        edittxt_Locate = (EditText) findViewById(R.id.txt_Locate);
        edittxt_Lock = (EditText) findViewById(R.id.txt_Lock);
        edittxt_Wipe = (EditText) findViewById(R.id.txt_Wipe);
        Add_Data();
    }

    public void Add_Data() {
        btn_Save.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean isInserted = myDb.insertMagicWords(edittxt_Ring.getText().toString());
                        if (isInserted == true)
                            Toast.makeText(Magic_Words.this, "Magic Words Saved", Toast.LENGTH_LONG).show();
                        else
                            Toast.makeText(Magic_Words.this, "Error Saveng Your Magic Words.", Toast.LENGTH_LONG).show();

                    }
                }

        );
    }
}

BDHelper

//public class MyDBHandler extends SQLiteOpenHelper {

    public class MyDBHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "TextIt.db";
    public static final String TABLE_NAME = "MagicWords";
    public static final String ColumnID = "ID";
    public static final String ColumnCommand = "Word";


    public MyDBHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME + " (" + ColumnCommand + "(ID INTEGER PRIMARY KEY AUTOINCREMENT," +
                ColumnCommand + "TEXT)");
    }

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

    public boolean insertMagicWords(String txt_Ring) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(ColumnCommand, txt_Ring);
        long result = db.insert(TABLE_NAME,null, contentValues);
        if (result == -1)
            return false;
        else
            return true;

    }
}

Stack trace(I think)

04-13 08:56:58.967 17258-17258/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.kieran.textit, PID: 17258
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kieran.textit/com.kieran.textit.Magic_Words}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                       at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:148)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                       at com.kieran.textit.Magic_Words.Add_Data(Magic_Words.java:37)
                                                       at com.kieran.textit.Magic_Words.onCreate(Magic_Words.java:33)
                                                       at android.app.Activity.performCreate(Activity.java:6237)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                       at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:148) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

2

It's because your button is getting null.

Add this on your onCreate.

btn_Save = (Button) findViewById(R.id.yourbuttonid);
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
0

Your create table query is not correct. You should put space between column name and its data type, also keep in mind about braces you are using in your query

Syntax of Create query

Create table TABLE_NAME (ID Integer Primary Key Autoincrement, COLUMN_1 Text, COLUMN_2 Text)

According to your case

Replace

db.execSQL("create table " + TABLE_NAME + " (" + ColumnCommand + "(ID INTEGER PRIMARY KEY AUTOINCREMENT," +
                ColumnCommand + "TEXT)");

With

db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT," + ColumnCommand + " TEXT)");

Also you should check all your widgets initialized properly like in your question above you are missing btnSave initialization (this caused nullPointerException according to your logcat), so add it

btnSave = (Button) findViewById(R.id.btnSave);

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

Your logcat says it all

Zubair Ahmed
  • 2,857
  • 2
  • 27
  • 47
  • Thanks for this. Still having the same issues tho. Everything i try to get to the MagicWords page the app just closes. –  Apr 13 '16 at 11:18