2

Hopefully I'm not missing something stupid. I'm able to create a single EditText and add it to a LinearLayout; however, when I try to do so to an array of EditTexts, I get an NPE on whatever line is first in the for loop. Here is the working code:

LinearLayout p2player1 = (LinearLayout)findViewById(R.id.p2player1);
    EditText p1 = new EditText(this);
    LinearLayout.LayoutParams editParams = new LinearLayout.LayoutParams(200,LinearLayout.LayoutParams.WRAP_CONTENT);
    p1.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
    p1.setInputType(InputType.TYPE_CLASS_NUMBER);
    p1.setId(View.generateViewId());
    p1.setLayoutParams(editParams);
    p2player1.addView(p1,editParams);

Everything fine.

Here's the array code:

LinearLayout.LayoutParams editParams = new LinearLayout.LayoutParams(200,LinearLayout.LayoutParams.WRAP_CONTENT);
    EditText[] play1 = new EditText[20];
    editParams.setMargins(184,0,0,0);
    for (int i=0;i<21;i++) {
        play1[i].setLayoutParams(editParams);
        play1[i].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        play1[i].setId(View.generateViewId());
        play1[i].setInputType(InputType.TYPE_CLASS_NUMBER);
        play1[i].setHint("0000");
        if (i>2 && IsOdd(i)) {
            play1[i].setFocusable(false);
        }

        p2player1.addView(play1[i],editParams);
    }
public boolean IsOdd(int n) {
    if ((n % 2) == 0) {
        return false;
    } else { return true; }
}

I included the IsOdd function just so it could be seen. That's not where it actually is. My Ultimate Goal is to generate a LinearLayout of Vertical EditTexts. Seems like it would be so easy, so I'm sure I'm being stupid, and missing something very easy.

Here's the NPE:

Process: com.dyna.ks.scorekeeper, PID: 5439
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dyna.ks.scorekeeper/com.dyna.ks.scorekeeper.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
                                                                       at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                       at android.os.Looper.loop(Looper.java:154)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
                                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
                                                                       at com.dyna.ks.scorekeeper.MainActivity.setupTwoPlayer(MainActivity.java:105)
                                                                       at com.dyna.ks.scorekeeper.MainActivity.onCreate(MainActivity.java:62)
                                                                       at android.app.Activity.performCreate(Activity.java:6955)
                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) 
                                                                       at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) 
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                       at android.os.Looper.loop(Looper.java:154) 
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6776) 
                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 

What am I missing or doing wrong, please?

Cœur
  • 32,421
  • 21
  • 173
  • 232
Kenyx
  • 129
  • 1
  • 1
  • 6
  • 2
    You still need to instantiate each individual `EditText`. `new EditText[20]` only instantiates the array. Also, your loop should be `i < 20`, since arrays are zero-based. – Mike M. Feb 08 '18 at 04:40
  • You are missing to initialise EditText p1 = new EditText(this); – duggu Feb 08 '18 at 04:44
  • Oops! Thanks. I know they're 0 based, just a typp. :ahem: "typo" – Kenyx Feb 08 '18 at 04:47

1 Answers1

1

This is because you did not initialized all editText.

Below is declaration only.

EditText[] play1 = new EditText[20];

You need to initialise each edittext into loop as play1[i] = new EditText(this);. See below code which will work fine

for (int i=0;i<21;i++) {
        play1[i] = new EditText(this);
        play1[i].setLayoutParams(editParams);
        play1[i].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        play1[i].setId(View.generateViewId());
        play1[i].setInputType(InputType.TYPE_CLASS_NUMBER);
        play1[i].setHint("0000");
        if (i>2 && IsOdd(i)) {
            play1[i].setFocusable(false);
        }

        p2player1.addView(play1[i],editParams);
    }
Pankaj Kumar
  • 78,055
  • 25
  • 161
  • 180
  • Thank you so much! I knew if was something stupid on my end. I'll accept the answer as soon as I'm able to. – Kenyx Feb 08 '18 at 04:48