0

I want to display data on the profile page stored in firebase, but when I try to display text with only numbers, and run the application and then go to the profile page for the user, it gives an error like this:


E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.drheartbeatmonitor, PID: 3415
java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.example.drheartbeatmonitor/com.example.drheartbeatmonitor.Profile}: 
java.lang.NullPointerException: Attempt to invoke virtual method 'void 
android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at 
android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void 
android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at com.example.drheartbeatmonitor.Profile.onCreate(Profile.java:67)
    at android.app.Activity.performCreate(Activity.java:8000)
    at android.app.Activity.performCreate(Activity.java:7984)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
    at 
android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:223) 
    at android.app.ActivityThread.main(ActivityThread.java:7656) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

This is my code : 1.login page:

String ID_Device = snapshot.child(Username).child("ID_Device").getValue().toString();
String HealthInsure = snapshot.child(Username).child("Health Insure").getValue().toString();
intent.putExtra("ID_Device", ID_Device);
intent.putExtra("Health Insure", HealthInsure);

2. profile page

String ID_Device = Patient.ID_Device;
String HealthInsure = Patient.HealthInsure;
health_insure.setText(HealthInsure);
ID_Dev.setText(ID_Device);
92AlanC
  • 1,327
  • 2
  • 14
  • 33

1 Answers1

1
 `Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void 
android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at com.example.drheartbeatmonitor.Profile.onCreate(Profile.java:67)`

In Profile.java line 67 are you trying to setText() on a null object? This could mean that the TextView you are trying to set the text is not yet created. Make sure you have called setContentView() to set the layout and then retrieved the TextView object via findViewById() before calling setText().

coderms
  • 96
  • 6
  • I'm already using findViewById() before calling setText()... I didn't find setContentView() function for TextView .. this error happened just if text consist of the only numbers – Razan Hasan Dec 01 '20 at 17:14
  • Please post the code in your onCreate() function. Typically in onCreate() you will need to setContentView() of your activity layout file (where the TextView is present), then call findViewById() to get the TextView object. It is really strange if the error happens only for numbers. – coderms Dec 01 '20 at 17:48
  • you mean `setContentView(R.layout.activity_patient); ID_Dev=findViewById(R.id.id_dev); health_insure=findViewById(R.id.healthInsurance);` – Razan Hasan Dec 01 '20 at 18:03
  • thank you very much, when you ask me about `findViewById(R.id.id_dev); `,I checked id and found that correct id as this `ID_Dev=findViewById(R.id.ID_Device_ed);` – Razan Hasan Dec 01 '20 at 18:12