Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does this cause a NullPointerException?
    primarykey
    data
    text
    <p>I have a program which is as follows, Can anyone tell me what is the reason for getting an NPE.</p> <pre><code>public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); secondClass x = new secondClass(); x.sText("Test Spring"); } } public class secondClass extends MainActivity { public void sText(String s) { TextView text = (TextView) findViewById(R.id.text); text.setText(s); } } </code></pre> <p><strong>Mainfest</strong></p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" ... &lt;TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:id="@+id/text" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p><strong>LogCat</strong></p> <pre><code>08-16 01:31:11.320 15032-15032/? E/Trace: error opening trace file: No such file or directory (2) 08-16 01:31:11.420 836-987/? E/EmbeddedLogger: App crashed! Process: com.example.myapplication 08-16 01:31:11.420 836-987/? E/EmbeddedLogger: App crashed! Package: com.example.myapplication v1 (1.0) 08-16 01:31:11.420 836-987/? E/EmbeddedLogger: Application Label: My Application 08-16 01:31:11.420 15032-15032/? E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java) at android.app.ActivityThread.access$600(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java) at android.os.Handler.dispatchMessage(Handler.java) at android.os.Looper.loop(Looper.java) at android.app.ActivityThread.main(ActivityThread.java) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java) at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:110) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at android.app.Activity.findViewById(Activity.java) at com.example.myapplication.secondClass.sText(secondClass.java:13) at com.example.myapplication.MainActivity.onCreate(MainActivity.java:14) at android.app.Activity.performCreate(Activity.java) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java) </code></pre> <p>To my understanding, this should instantiate and initialize a secondClass object then call its sText() method. The sText method should then store a reference in TextView. Next it calls the setText() method on TextView, which changes the text in the TextView to what was passed into sText().</p> <p>The logcat says it throws a NullPointerException but I'm not entirely sure why. If this is a rookie mistake, it's because I am a rookie. Thank you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload