-2

I tried to add data to sqlite, but when I clicked Add Button error appear and my app closed. I don't understand, so many wasted hours for looking the solution by myself..

here is my java code, AddSampleActivity.java

package com.example.mangan;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddSampleActivity extends AppCompatActivity {

DatabaseHelper myDb;
EditText editNama, editJarak, editRating , editHarga;

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

    editNama = (EditText) findViewById(R.id.nama);
    editJarak = (EditText) findViewById(R.id.jarak);
    editRating = (EditText) findViewById(R.id.rating);
    editHarga = (EditText) findViewById(R.id.harga);

    myDb = new DatabaseHelper(this);
}

public void addData(View view) {
    boolean isInserted = myDb.insertData(editNama.getText().toString(),
            editJarak.getText().toString(),
            editRating.getText().toString(),
            editHarga.getText().toString() );
    if(isInserted)
        Toast.makeText(AddSampleActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
    else
        Toast.makeText(AddSampleActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();
    }
}

my layout, activity_add_sample.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context="com.example.mangan.AddSampleActivity">

<EditText
    android:id="@+id/jarak_editText"
    android:hint="Jarak"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<EditText
    android:id="@+id/rating_editText"
    android:hint="Rating"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<EditText
    android:id="@+id/harga_editText"
    android:hint="Harga"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/add_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="addData"
    android:text="Add"/>

</LinearLayout>

This is fullstack trace

java.lang.IllegalStateException: Could not execute method for android:onClick
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                  at android.view.View.performClick(View.java:5675)
                  at android.view.View$PerformClick.run(View.java:22651)
                  at android.os.Handler.handleCallback(Handler.java:836)
                  at android.os.Handler.dispatchMessage(Handler.java:103)
                  at android.os.Looper.loop(Looper.java:208)
                  at android.app.ActivityThread.main(ActivityThread.java:6267)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
               Caused by: java.lang.reflect.InvocationTargetException
                  at java.lang.reflect.Method.invoke(Native Method)
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                  at android.view.View.performClick(View.java:5675) 
                  at android.view.View$PerformClick.run(View.java:22651) 
                  at android.os.Handler.handleCallback(Handler.java:836) 
                  at android.os.Handler.dispatchMessage(Handler.java:103) 
                  at android.os.Looper.loop(Looper.java:208) 
                  at android.app.ActivityThread.main(ActivityThread.java:6267) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924) 
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                  at com.example.mangan.AddSampleActivity.addData(AddSampleActivity.java:30)
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                  at android.view.View.performClick(View.java:5675) 
                  at android.view.View$PerformClick.run(View.java:22651) 
                  at android.os.Handler.handleCallback(Handler.java:836) 
                  at android.os.Handler.dispatchMessage(Handler.java:103) 
                  at android.os.Looper.loop(Looper.java:208) 
                  at android.app.ActivityThread.main(ActivityThread.java:6267) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924) 

It seem that some content on addData method is the problem. I read another question, Getting IllegalStateException on button click, but the problem is little bit different..

ggsuha
  • 155
  • 1
  • 20
  • `Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference` this line of log tells you where is the problem. And you connected your EditText by its hint instead you have to use `id`. For example: `editJarak = (EditText) findViewById(R.id. jarak_editText);` And after this delete this question its to abiovus it will be marked as duplicated and you will get downvotes. – Yupi Dec 01 '17 at 17:30
  • Your issue was in your log. Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference You should not assume the EditText has a value. Change using boolean isInserted = myDb.insertData(editNama.getText().toString(), editJarak.getText().toString(), editRating.getText().toString(), editHarga.getText().toString() ); to – Sam Dec 01 '17 at 17:41
  • String nama = editNama.getText() == null ? "" : editNama.getText().toString(); String jarak = editJarak.getText() == null ? "" : editJarak.getText().toString(); String rating = editRating.getText() == null ? "" : editRating.getText().toString(); String harga = editHarga.getText() == null ? "" : editHarga.getText().toString(); boolean isInserted = myDb.insertData(nama, jarak, rating, harga); – Sam Dec 01 '17 at 17:41
  • that will fix your issue – Sam Dec 01 '17 at 17:41

2 Answers2

1

Your edittext ids aren't correct. change them like below

editJarak = (EditText) findViewById(R.id.jarak_editText);
editRating = (EditText) findViewById(R.id.rating_editText);
editHarga = (EditText) findViewById(R.id.harga_editText);
Bek
  • 7,790
  • 4
  • 18
  • 31
0

Your edit text ids are wrong. Try this,

    editNama = (EditText) findViewById(R.id.nama_editText);
    editJarak = (EditText) findViewById(R.id.jarak_editText);
    editRating = (EditText) findViewById(R.id.rating_editText);
    editHarga = (EditText) findViewById(R.id.harga_editText);
Vihanga Yasith
  • 328
  • 5
  • 18