Reputation: 137
I am attempting to use a global class to make the object data available through all of my activities. In my first activity, I am initializing my global class patient
, and setting the variable with setPatientName
. In my next activity, I call 'getPatientName', but it is returning null
. When I try to set the result of 'getPatientName', it gives me the error
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
EDIT: My first activity is collecting the name from a text field, and that is what i'm trying to setPatientName
as
The first activity:
tv2=(TextView)findViewById(R.id.textView2);
EditText name = (EditText) findViewById(R.id.textView2);
String nameString = name.getText().toString();
final Patient p = (Patient) getApplicationContext();
p.setPatientName(nameString);
The Second Activity:
Patient p = (Patient)getApplication();
String patName = p.getPatientName();
tv2.setText(patName);
The Patient class:
package com.example.imac.chs_pharmacy;
import android.app.Application;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
public class Patient extends Application {
//private variables
public String patient_name;
//default constructor
public Patient(){
}
public Patient(String startPatientName) {
this.patient_name = startPatientName;
}
public void setPatientName( String patientName ){
Log.d(TAG, "setting patient name");
this.patient_name = patientName;
}
public String getPatientName( ){
Log.d(TAG, "getting patient name");
return this.patient_name;
}
manifest.xml:
<application android:name="com.example.imac.chs_pharmacy.Patient"
It's also worth noting that in my Patient
class, i'm logging out a string in my getPatientName
and setPatientName
, but it only seems to be logging on the setPatientName
. Is my setPatientName
not getting fired off for some reason?
Upvotes: 0
Views: 448
Reputation: 3574
There is no need of extending Application
. Try as below
public class Patient {
private static Patient patientInstance;
private String patient_name;
//private contrunctor to prevent from creating patient instance directly through constructor.
private Patient() {
}
public static Patient getInstance() {
if (patientInstance == null) {
patientInstance = new Patient();
}
return patientInstance;
}
public void setPatientName( String patientName ){
this.patient_name = patientName;
}
public String getPatientName( ){
return this.patient_name;
}
}
Then use the class as below
Patient p = Patient.getInstance();
String patName = p.getPatientName();
TextView tv2 = (TextView) findViewById(R.id.your_text_view_id);
tv2.setText(patName);
Upvotes: 1
Reputation: 61
Let's try this solution:
public class Patient extends Application {
//private variables
public String patient_name = "StartPatientName";
//default constructor you should not ovveride Application class constructor!
// public Patient(){
// }
// public Patient(String startPatientName) {
// this.patient_name = startPatientName;
//}
public void setPatientName( String patientName ){
Log.d(TAG, "setting patient name");
this.patient_name = patientName;
}
public String getPatientName( ){
Log.d(TAG, "getting patient name");
return this.patient_name;
}
Also in the second activity init tv2 as below:
Patient p = (Patient)getApplication();
String patName = p.getPatientName();
TextView tv2 = (TextView) findViewById(R.id.your_text_view_id);
tv2.setText(patName);
Upvotes: 1