0

I am trying to write this simple class which will take user input and display it to a text view but every time I try to run it I am getting a fatal error in my main class. I'm a newbie and I know this is something simple but for the life of me I can't find it!! Any help greatly appreciated. Please see below my code to create the class

 package com.ObComDis.wayne;

import android.app.Activity;
import android.os.Bundle;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ObComDisActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

String name;
EditText et = (EditText) findViewById(R.id.edittext);
TextView t = (TextView) findViewById(R.id.textView1);

public String getName()
{
    String name = et.getText().toString();
    return name;
}
public void main (String args [])
{
    ObComDisActivity myocd = new ObComDisActivity();
    t.setText(name);

}
}

And here is the reading I am getting from log cat.....

07-11 18:55:01.149: D/AndroidRuntime(525): Shutting down VM
07-11 18:55:01.149: W/dalvikvm(525): threadid=1: thread exiting with uncaught exception       (group=0x40015560)
07-11 18:55:01.189: E/AndroidRuntime(525): FATAL EXCEPTION: main
07-11 18:55:01.189: E/AndroidRuntime(525): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ObComDis.wayne/com.ObComDis.wayne.ObComDisActivity}: java.lang.NullPointerException
07-11 18:55:01.189: E/AndroidRuntime(525):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
07-11 18:55:01.189: E/AndroidRuntime(525):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-11 18:55:01.189: E/AndroidRuntime(525):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-11 18:55:01.189: E/AndroidRuntime(525):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-11 18:55:01.189: E/AndroidRuntime(525):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 18:55:01.189: E/AndroidRuntime(525):  at android.os.Looper.loop(Looper.java:123)
07-11 18:55:01.189: E/AndroidRuntime(525):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-11 18:55:01.189: E/AndroidRuntime(525):  at java.lang.reflect.Method.invokeNative(Native Method)
07-11 18:55:01.189: E/AndroidRuntime(525):  at java.lang.reflect.Method.invoke(Method.java:507)
07-11 18:55:01.189: E/AndroidRuntime(525):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-11 18:55:01.189: E/AndroidRuntime(525):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-11 18:55:01.189: E/AndroidRuntime(525):  at dalvik.system.NativeStart.main(Native Method)
07-11 18:55:01.189: E/AndroidRuntime(525): Caused by: java.lang.NullPointerException
07-11 18:55:01.189: E/AndroidRuntime(525):  at android.app.Activity.findViewById(Activity.java:1647)
07-11 18:55:01.189: E/AndroidRuntime(525):  at com.ObComDis.wayne.ObComDisActivity.<init>(ObComDisActivity.java:25)
07-11 18:55:01.189: E/AndroidRuntime(525):  at java.lang.Class.newInstanceImpl(Native Method)
07-11 18:55:01.189: E/AndroidRuntime(525):  at java.lang.Class.newInstance(Class.java:1409)
07-11 18:55:01.189: E/AndroidRuntime(525):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-11 18:55:01.189: E/AndroidRuntime(525):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
07-11 18:55:01.189: E/AndroidRuntime(525):  ... 11 more
2
  • Can you format your log cat reading? If possible post to pastebin. It is a pain to read as is. Commented Jul 11, 2012 at 19:04
  • Sorry about that! Don't know what you mean by post it to the paste bin? How can I format it? I just pasted what was in my log cat to the question? Thanks./ Commented Jul 12, 2012 at 17:29

1 Answer 1

1

findViewById() returns null until that view is created, which in your case would happen only as a result of your setContentView() in onCreate().

Try something like this:

EditText et;
TextView t;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    et = (EditText) findViewById(R.id.edittext);
    t = (TextView) findViewById(R.id.textView1);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.