-3

when i click on register button i get "unfornately project has stopped working" if you tell me to add more code regarding this then add more but plzz help me main activity

public class MainActivity extends AppCompatActivity {
    SQLiteOpenHelper openHelper;
    SQLiteDatabase db;

    public static final String Aadesh ="";
    EditText name,email,phone,pswd,confirmpswd;
    Button btn1,btn2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void secondpage(View view)
    {
        //EditText nametxt,emailtxt,phonetxt,pswdtxt,confirmpswdtxt;
       // Button registerbtn,signubtn;
        openHelper=new Databasehelper(this);
        Intent intent = new Intent(this,secondpage.class);
        name=(EditText)findViewById(R.id.nametxt);
        email=(EditText)findViewById(R.id.emailtxt);
        phone=(EditText)findViewById(R.id.phonetxt);
        pswd=(EditText)findViewById(R.id.pswdtxt);
        confirmpswd=(EditText)findViewById(R.id.confirmpswdtxt);
        btn1=(Button)findViewById(R.id.registerbtn);
        btn2=(Button)findViewById(R.id.signupbtn);

        //signubtn=(Button)findViewById(R.id.signupbtn);
        //String message=name.getText().toString();
        //intent.putExtra(Aadesh,message);
        //startActivity(intent);
    }
    public void onClick(View v)
    {
        db=openHelper.getWritableDatabase();
        String rname=name.getText().toString();
        String remail= email.getText().toString();
        String rpswd= pswd.getText().toString();
        String rphone=phone.getText().toString();
        insertdata(rname,remail,rpswd,rphone);
        Toast.makeText(getApplicationContext(),"register successfully ",Toast.LENGTH_LONG).show();
    }
    public void insertdata(String rname,String remail,String rpswd,String rphone)
    {
        ContentValues contentValues=new ContentValues();
       // contentValues.put(Databasehelper ContentValues;
        contentValues.put(Databasehelper.col_2,rname);
        contentValues.put(Databasehelper.col_4,rpswd);
        contentValues.put(Databasehelper.col_5,remail);
        contentValues.put(Databasehelper.col_6,rphone);

        long id=db.insert(Databasehelper.table_name,null,contentValues);
    }
    public void thirdpage(View v)
    {
        Intent intent =new Intent(this,thirdpage.class);
        startActivity(intent);
    }
}

logcat show's this error

    --------- beginning of crash
03-27 12:02:15.953 1999-1999/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.dictatormahesh.project, PID: 1999
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
        at android.view.View.performClick(View.java:4756)
        at android.view.View$PerformClick.run(View.java:19749)
        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:5221)
        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:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
        at android.view.View.performClick(View.java:4756) 
        at android.view.View$PerformClick.run(View.java:19749) 
        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:5221) 
        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:899) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.database.sqlite.SQLiteOpenHelper.getWritableDatabase()' on a null object reference
        at com.example.dictatormahesh.project.MainActivity.onClick(MainActivity.java:51)
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
        at android.view.View.performClick(View.java:4756) 
        at android.view.View$PerformClick.run(View.java:19749) 
        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:5221) 
        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:899) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
03-27 12:02:15.955 551-637/system_process W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
03-27 12:02:15.975 551-584/system_process W/ActivityManager:   Force finishing activity com.example.dictatormahesh.project/.MainActivity
03-27 12:02:16.075 121-619/? E/cutils-trace: Error opening trace file: Permission denied (13)
03-27 12:02:16.201 511-511/? E/EGL_emulation: tid 511: eglCreateSyncKHR(1215): error 0x3004 (EGL_BAD_ATTRIBUTE)

main xml

<Button
     android:id="@+id/registerbtn"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginStart="73dp"
     android:layout_marginTop="4dp"
     android:layout_marginEnd="223dp"
     android:layout_marginBottom="10dp"
     android:background="@color/colorAccent"
     android:onClick="onClick"
     android:text="REGISTER"
     app:layout_constraintBottom_toBottomOf="parent"
     app:layout_constraintEnd_toEndOf="parent"
     app:layout_constraintStart_toStartOf="parent"
     app:layout_constraintTop_toBottomOf="@+id/textView13" />
Zoe
  • 27,060
  • 21
  • 118
  • 148
Aadesh
  • 11
  • 2
  • I think your `activity_main.xml` file was cut off. Is there anything in the file right before the `android:id="@+id/registerbtn"` line? – Cliabhach Mar 27 '19 at 16:26
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zoe Mar 27 '19 at 16:30
  • no sir, textview tag is their above that – Aadesh Mar 27 '19 at 16:41

3 Answers3

0

you need to create the instance of Databasehelper in the onCreate method of the activity. since you have declared it inside the method its scope is only inside that method. To get access it from onClick instantiate at onCreate.

 public void secondpage(View view)
{

    openHelper=new Databasehelper(this); //move this line to onCreate

}

Jiten Basnet
  • 1,623
  • 15
  • 31
0

As @Jiten mentioned in his answer, the reason of the crash is because you are trying to access the getWritableDatabase() method of the openHelper object before instantiating it. There are two ways to fix this.

Method 1: Move the code to instantiate the openHelper in the onCreate method. Your onCreate method should now look like this

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

    openHelper = new Databasehelper(this);
}

Method 2: Move the code to instantiate the openHelper in your onClick method before you do anything else. Your onClick method should now look like this

public void onClick(View v) {
    openHelper = new Databasehelper(this);
    db=openHelper.getWritableDatabase();
    String rname=name.getText().toString();
    String remail= email.getText().toString();
    String rpswd= pswd.getText().toString();
    String rphone=phone.getText().toString();
    insertdata(rname,remail,rpswd,rphone);
    Toast.makeText(getApplicationContext(),"register successfully ",Toast.LENGTH_LONG).show();
}
Bilal Naeem
  • 962
  • 7
  • 13
-1

You have a nullPointer on your SQLiteHelper :

java.lang.NullPointerException: Attempt to invoke virtual method 
'android.database.sqlite.SQLiteDatabase 
android.database.sqlite.SQLiteOpenHelper.getWritableDatabase()' on a null object 
reference at com.example.dictatormahesh.project.MainActivity.onClick(MainActivity.java:51) at 

Did you instanciate it ?

Goufalite
  • 2,253
  • 3
  • 17
  • 29
  • sir i have ```import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper;```only this much how do i solve this i am new android studio plzz help – Aadesh Mar 27 '19 at 16:47
  • yes instanciated sir but not this **import android.database.sqlite.SQLiteOpenHelper.getWritableDatabase();** – Aadesh Mar 27 '19 at 17:06
  • PLEASE SIR REPLY I AM VERY NEW TO ANDROID STUDIO – Aadesh Mar 27 '19 at 17:43