-4

I am getting the following error in Android Studio and I am unable to resolve it:

07-06 19:44:41.798 4491-4491/com.example.first_app D/AndroidRuntime: Shutting down VM
07-06 19:44:41.799 4491-4491/com.example.first_app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.first_app, PID: 4491
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.first_app/com.example.first_app.welcome}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.view.View.toString()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.view.View.toString()' on a null object reference
at com.example.first_app.welcome.onCreate(welcome.java:22)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
07-06 19:44:44.696 4491-4497/com.example.first_app W/art: Suspending all threads took: 46.612ms
07-06 19:44:46.517 4491-4491/com.example.first_app I/Process: Sending signal. PID: 4491 SIG: 9

THIS IS THE ERROR I M INCURRING


CODE FOR welcome.java

public class welcome extends AppCompatActivity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        TextView my_msg;
        String name;
        my_msg= (TextView)findViewById(R.id.textView3);
        name= "Welcome " +findViewById(R.id.textView2).toString();
        my_msg.setText(name);
    }
}

XML CODE FOR welcome.xml ...Please refer it..it only shows the message on screen "welcome Name"..the name is entered by user through activity_form.java

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView3"
        android:layout_gravity="center"
        android:layout_weight="1" />

</LinearLayout>
prerika
  • 1
  • 3
  • 1
    `java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.view.View.toString()' on a null object reference at com.example.first_app.welcome.onCreate(welcome.java:22)` – CommonsWare Jul 06 '16 at 14:20
  • look at line 22 of `com.example.first_app.welcome` – tyczj Jul 06 '16 at 14:21
  • sir please elaborate..how to do it..actually m beginner – prerika Jul 06 '16 at 14:21
  • we cant elaborate on a "question" and has no information – tyczj Jul 06 '16 at 14:22
  • 1
    http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Sergey Glotov Jul 06 '16 at 14:28
  • You need to post your code. Having just the exception is not enough. – Lexi Jul 06 '16 at 14:32
  • what in the world are you trying to do here? `name= "Welcome " +findViewById(R.id.textView2).toString();` whatever it is, it is very very wrong – tyczj Jul 06 '16 at 14:51
  • i want to display the message "welcome name" and this name will be entered by user through activity_form.java – prerika Jul 06 '16 at 14:53
  • Please post the XML code of welcome.xml – Karen Forde Jul 06 '16 at 14:55
  • you get the error because you dont have anything with an id of `textView2` in your layout shown – tyczj Jul 06 '16 at 15:56
  • textView2 is the id for edit text used in first activity..which is responsible for taking input from user – prerika Jul 06 '16 at 16:09
  • which does not exist in this activity which is why its crashing – tyczj Jul 06 '16 at 17:41
  • so wat can be done? – prerika Jul 06 '16 at 18:04
  • you need to pass the data you want there in your intent that you use to launch that activity http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android – tyczj Jul 06 '16 at 18:06
  • it did no gud..can u tell me how to invoke virtual method 'java.lang.String android.view.View.toString()' on a null object reference – prerika Jul 06 '16 at 18:45
  • No, you can't invoke ANY method on a null reference - that's the whole point. You have misunderstood some of the most fundamental basic concepts of the programming - you should probably head back to tutorials. – takendarkk Jul 06 '16 at 21:02

1 Answers1

1

First the way to solve your exception to type cast the View and getText from that View

(TextView) findViewById(R.id.textView2).getText().toString();

So the final String will look like

name= "Welcome " + (TextView) findViewById(R.id.textView2).getText().toString();

SUGGESTION

Use proper naming conventions for views, Rather than naming view1 view2 etc

Sanoop Surendran
  • 3,484
  • 4
  • 28
  • 49