jpgerb
jpgerb

Reputation: 1120

error declaring TextViews

I am receiving an error when I start my activity. Here is my activity:

public class DebtList extends Activity {

    String debtNames1, debtNames2, debtNames3, debtNames4, debtNames5 = null; 
    Double debtAmount1, debtAmount2, debtAmount3, debtAmount4, debtAmount5;
    Double debtRate1, debtRate2, debtRate3, debtRate4, debtRate5;
    Integer debtTerm1, debtTerm2, debtTerm3, debtTerm4, debtTerm5;

    //Name Displays
    TextView debtName1 = (TextView) findViewById(R.id.dispName1); //logcat states this is the location of the error
    TextView debtName2 = (TextView) findViewById(R.id.dispName2);
    TextView debtName3 = (TextView) findViewById(R.id.dispName3);
    TextView debtName4 = (TextView) findViewById(R.id.dispName4);
    TextView debtName5 = (TextView) findViewById(R.id.dispName5);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.debtlist);

Here is a copy of the logcat:

10-20 05:59:15.014: D/AndroidRuntime(389): Shutting down VM
10-20 05:59:15.014: W/dalvikvm(389): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-20 05:59:15.094: D/dalvikvm(389): GC_FOR_MALLOC freed 5706 objects / 305808 bytes in 68ms
10-20 05:59:15.094: E/AndroidRuntime(389): FATAL EXCEPTION: main
10-20 05:59:15.094: E/AndroidRuntime(389): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{biz.midl.debttracking/biz.midl.debttracking.DebtList}: java.lang.NullPointerException
10-20 05:59:15.094: E/AndroidRuntime(389):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
10-20 05:59:15.094: E/AndroidRuntime(389):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-20 05:59:15.094: E/AndroidRuntime(389):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-20 05:59:15.094: E/AndroidRuntime(389):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-20 05:59:15.094: E/AndroidRuntime(389):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-20 05:59:15.094: E/AndroidRuntime(389):  at android.os.Looper.loop(Looper.java:123)
10-20 05:59:15.094: E/AndroidRuntime(389):  at android.app.ActivityThread.main(ActivityThread.java:4627)
10-20 05:59:15.094: E/AndroidRuntime(389):  at java.lang.reflect.Method.invokeNative(Native Method)
10-20 05:59:15.094: E/AndroidRuntime(389):  at java.lang.reflect.Method.invoke(Method.java:521)
10-20 05:59:15.094: E/AndroidRuntime(389):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-20 05:59:15.094: E/AndroidRuntime(389):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-20 05:59:15.094: E/AndroidRuntime(389):  at dalvik.system.NativeStart.main(Native Method)
10-20 05:59:15.094: E/AndroidRuntime(389): Caused by: java.lang.NullPointerException
10-20 05:59:15.094: E/AndroidRuntime(389):  at android.app.Activity.findViewById(Activity.java:1637)
10-20 05:59:15.094: E/AndroidRuntime(389):  at biz.midl.debttracking.DebtList.<init>(DebtList.java:16)
10-20 05:59:15.094: E/AndroidRuntime(389):  at java.lang.Class.newInstanceImpl(Native Method)
10-20 05:59:15.094: E/AndroidRuntime(389):  at java.lang.Class.newInstance(Class.java:1429)
10-20 05:59:15.094: E/AndroidRuntime(389):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
10-20 05:59:15.094: E/AndroidRuntime(389):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)

I tried doing final TextView debtName1 = ..... and it provided the same error message.

Please let me know what can be done to alleviate the error.

Upvotes: 0

Views: 36

Answers (1)

hakanostrom
hakanostrom

Reputation: 1081

All the five TextView assignments must be inside the onCreate function.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.debtlist);

    //Name Displays
    TextView debtName1 = (TextView) findViewById(R.id.dispName1);
    TextView debtName2 = (TextView) findViewById(R.id.dispName2);
    TextView debtName3 = (TextView) findViewById(R.id.dispName3);
    TextView debtName4 = (TextView) findViewById(R.id.dispName4);
    TextView debtName5 = (TextView) findViewById(R.id.dispName5);

    // Some other stuff ..
    }

Variable assignments at a global level happen when the object (your DebtList activity) is created. By that time the xml is not yet inflated (happens in setContentView(..) ) and therefor the id´s of that xml´s views (R.id.dispNameX) does not yield to a real object. Hence the NullPointerException.

Upvotes: 3

Related Questions