Reputation: 319
I am trying to convert a double type from EditText
, but I do not seem to understand the right place to write it down.
From what I have research, the error seem to be from the
EditText cct = (EditText)findViewById(R.id.curr_credit_text);
initialized before onCreate()
method. However, if I initialize it inside the onCreate()
method, the other methods cannot access the parsed variable, like so
Where exactly do I suppose to initialize the variable, so it can then be accessed from other methods?
I have tried all of the three arrow for the placements, but every time it gives another error.
Here is the logcat
FATAL EXCEPTION: main
Process: com.example.user.testapp, PID: 27472
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.user.testapp/com.example.user.testapp.RevGPAActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2227)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376)
at android.app.ActivityThread.access$800(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
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.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2113)
at com.example.user.testapp.RevGPAActivity.<init>(RevGPAActivity.java:20)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1572)
at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2217)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376)
at android.app.ActivityThread.access$800(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
Upvotes: 0
Views: 69
Reputation: 7929
You must declare your variables outside the onCreate()
method and initialize them inside it.
eg:
private EditText cct;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rev_gpa);
cct = (EditText) findViwByid(R.id.curr_creadit_text);
...
}
Upvotes: 0
Reputation: 1921
You have to initialize variable in onCreate
and get those values when you calculate the GradPoint()
, you can add a button and call the GradPoint()
when the button is pressed. Because if you call them in onCreate
, those functions will be immediately called, you are suppose to call them once the values are entered, right? And if you call them without the values entered, you will get NumberFormatException
because you are trying to parse an empty string, so you have to be careful while using Double.parseDouble()
as well. I am writing below a sample code structure to show what I am trying to say here. Hope it helps.
EditText edt1;
Button sumbit;
private double CURRENT_CREDIT;
private double FUTURE_CREDIT;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt1=(EditText)findviewById(R.id.edt_username);
submit=(EditText)findviewById(R.id.submit);
...
submit.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
//Check if all the required values are not empty
if(!edt1.getText().toString().isEmpty() && ...)
calcGradPoint();
}
});
}
void calcGradPoint(){
CURRENT_CREDIT = Double.parseDouble(ed1.getText().toString());
...
}
Upvotes: 0
Reputation: 1736
Do something like this for each EditText
:
public class RevGPAActivity extends Activity {
private EditText cct;
....
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rev_gpa);
cct = (EditText) findViewById(R.id.current_credit_text);
....
}
}
You will be able to access it out of onCreate()
also.
Upvotes: 0
Reputation: 27515
Do initialization like this
EditText edt1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt1=(EditText)findviewById(R.id.edt_username);
}
Upvotes: 2
Reputation: 1053
Place your variable before onCreate() like this
private double CURRENT_CREDIT;
private double FUTURE_CREDIT;
and so....
doing this you will declare global varialble which can be acessed in all over the class.
Upvotes: 0