Note that there are some explanatory texts on larger screens.

plurals
  1. POJava null pointer exception on using simple Intent Extra
    text
    copied!<p>When I try to send some text over to the next <code>Activity</code> the program crashes saying "null pointer exception unable to start activity" but without using <code>Intent Extra</code> the <code>Intent</code> works fine.</p> <p><strong>Main Activity</strong></p> <pre><code>public class MainActivity extends Activity implements OnClickListener{ EditText et; public final static String KEY="com.example.myprotrial"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et = (EditText) findViewById(R.id.editText1); Button btn =(Button)findViewById(R.id.button1); btn.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onClick(View arg0) { Intent i = new Intent(MainActivity.this,MyActivity.class); i.putExtra(KEY, et.getText().toString()); startActivity(i); } } </code></pre> <p><strong>MyActivity</strong></p> <pre><code>public class MyActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); Intent intent=getIntent(); String a = intent.getStringExtra(MainActivity.KEY); TextView tv = (TextView) findViewById(R.id.textView1); tv.setText(a); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.my, menu); return true; } } </code></pre> <p><strong>LogCat</strong></p> <pre><code>12-09 13:58:33.115: E/AndroidRuntime(1588): FATAL EXCEPTION: main 12-09 13:58:33.115: E/AndroidRuntime(1588): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myprotrial/com.example.myprotrial.MyActivity}: java.lang.NullPointerException 12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread.access$600(ActivityThread.java:141) 12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 12-09 13:58:33.115: E/AndroidRuntime(1588): at android.os.Handler.dispatchMessage(Handler.java:99) 12-09 13:58:33.115: E/AndroidRuntime(1588): at android.os.Looper.loop(Looper.java:137) 12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread.main(ActivityThread.java:5103) 12-09 13:58:33.115: E/AndroidRuntime(1588): at java.lang.reflect.Method.invokeNative(Native Method) 12-09 13:58:33.115: E/AndroidRuntime(1588): at java.lang.reflect.Method.invoke(Method.java:525) 12-09 13:58:33.115: E/AndroidRuntime(1588): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 12-09 13:58:33.115: E/AndroidRuntime(1588): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 12-09 13:58:33.115: E/AndroidRuntime(1588): at dalvik.system.NativeStart.main(Native Method) 12-09 13:58:33.115: E/AndroidRuntime(1588): Caused by: java.lang.NullPointerException 12-09 13:58:33.115: E/AndroidRuntime(1588): at com.example.myprotrial.MyActivity.onCreate(MyActivity.java:18) 12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.Activity.performCreate(Activity.java:5133) 12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 12-09 13:58:33.115: E/AndroidRuntime(1588): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 12-09 13:58:33.115: E/AndroidRuntime(1588): ... 11 more </code></pre>
 

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