i get this error:
ntime: FATAL EXCEPTION: main Process: com.example.puzzle_project, PID: 4100 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.puzzle_project/com.example.puzzle_project.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
but i dont know what is the problem.
my mainActivity is:
package com.example.puzzle_project;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_TEXT="com.example.application.example.EXTRA_TEXT";
public static final String EXTRA_NUMBER="com.example.application.example.EXTRA_NUMBER";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openActivity2();
}
});
}
public void openActivity2(){
EditText editText1= (EditText) findViewById(R.id.edittext1);
String text= editText1.getText().toString();
EditText editText2=(EditText)findViewById(R.id.edittext2);
int number = Integer.parseInt(editText2.getText().toString());
Intent intent = new Intent(this, Activity2.class);
intent.putExtra(EXTRA_TEXT,text);
intent.putExtra(EXTRA_NUMBER,number);
startActivity(intent);
}
}
CodePudding user response:
Try this code
Follow 2 things-
- Before sending data check isempty or null.
- Add inputType in xml for particular editTexts Ex. your second EditText is Number so add
android:inputType="number"
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_TEXT = "com.example.application.example.EXTRA_TEXT";
public static final String EXTRA_NUMBER = "com.example.application.example.EXTRA_NUMBER";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openActivity2();
}
});
}
public void openActivity2() {
EditText editText1 = (EditText) findViewById(R.id.edittext1);
String text = editText1.getText().toString();
EditText editText2 = (EditText) findViewById(R.id.edittext2);
String number = (editText2.getText().toString());
if (text.isEmpty() || number.isEmpty()) {
Toast.makeText(this, "Please add inputs", Toast.LENGTH_LONG).show();
return;
}
Intent intent = new Intent(this, Activity2.class);
intent.putExtra(EXTRA_TEXT, text);
intent.putExtra(EXTRA_NUMBER, number);
startActivity(intent);
}
}
CodePudding user response:
NullPointerException
Occurs when you Attempt to use objects methods or variables which is null like :-
String name = null;
int len = name.length();
This will also throw NullPointerException
. Every programmer go through this type of Error its very common and the best way to solve this problem is to read the error like what you share in the Question is:-
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.puzzle_project/com.example.puzzle_project.MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
on a null object reference
Lets try to understand this error what it is trying to say so,
First Line is Unable to start activity
means there is a problem in the OnCreate() Method because First OnCreate() method is call to start any Activity.
Second Line is Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
As you can read this Attempt to invoke method on NULL OBJECT REFRENCE It's means you are call on click on null objects means button is null.
Now lets Understand Why button is null, button can be null by following reasons like:-
- button is not initialize ( But You did it ).
- method is returning null from which we get button from means findViewById() is returning null. ( It Maybe a case)
Now Why findViewById() is returning null because you may put wrong id and findViewById is not able to find your id in the View.
In-Short Check your View Id and findViewById() its the problem because of that findViewById is returning null.
I can say that in short way but I just want you to solve your problem on your own because your are going to get a hell of errors in the future.
Thanks for reading
CodePudding user response:
take out the elements of the method and initialize them without findviewbyid in the class activity and in the method onClick() use "findviewbyid" for your element and pass these elements as parameters of the method openActivity2().
i have not try but i think is the probleme.