-1

I am trying to set the text of a TextView. However, I am receiving the following error whenever I click a button that should add text to the TextView:

NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference" from the logcat.

Currently, the init() function programmatically creates/aligns the TextView inside a linearlayout. Next, I instantiate the TextView Whole1. Then I set the OnClickListener for the buttons that can modify the TextView. The specific line that the error mentions is inside setWholeOnClickListener():

Whole1.setText(button.getText());

I've found What is a NullPointerException, and how do I fix it? as a reference, but haven't had much luck resolving this issue. What did I miss?

The following is my Java file:

public class MainActivity extends AppCompatActivity {
  TextView Whole1;
  private boolean isNull = true;

  private int[] wholeButtons = {R.id.wholeButton1, R.id.wholeButton2, R.id.wholeButton3, R.id.wholeButton4, R.id.wholeButton5, R.id.wholeButton6, R.id.wholeButton7, R.id.wholeButton8, R.id.wholeButton9, R.id.wholeButton0};

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

    init();
    Whole1 = (TextView) findViewById(R.id.Whole1);
    setWholeOnClickListener();
  }

  private void setWholeOnClickListener() {
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Button button = (Button) v;
            if (isNull) {
                Whole1.setText(button.getText());
                isNull = false;
            } else {
                Whole1.append(button.getText());
            }
        }
    };
    for (int id : wholeButtons) {
        findViewById(id).setOnClickListener(listener);
    }
  }

  /* Initializes the first view for input */
  private void init() {
    LinearLayout Result = (LinearLayout) findViewById(R.id.Result);

    RelativeLayout Mixed = new RelativeLayout(this);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    Mixed.setLayoutParams(params);

    TextView Whole1 = new TextView(this);
    TextView Fraction1 = new TextView(this);
    TextView Num1 = new TextView(this);
    TextView Den1 = new TextView(this);

    /* Scale is a screen dependent value, used for filling the screen */
    final float scale = getResources().getDisplayMetrics().density;
    int pixels = (int) (2 * scale + 0.5f);


    /* Fraction bar 1 */
    params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, pixels);
    params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
    params.addRule(RelativeLayout.RIGHT_OF, Whole1.getId());
    Fraction1.setBackgroundColor(Color.BLACK);
    Fraction1.setText("________"); // Placeholder
    Fraction1.setLayoutParams(params);
    Mixed.addView(Fraction1);

    /* Numerator 1 */
    params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
    params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
    params.addRule(RelativeLayout.ABOVE, Fraction1.getId());
    Num1.setText("3");
    Num1.setGravity(Gravity.CENTER);
    Num1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
    Num1.setLayoutParams(params);
    Mixed.addView(Num1);

    /* Denominator 1 */
    params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
    params.addRule(RelativeLayout.BELOW, Fraction1.getId());
    Den1.setText("4");
    Den1.setGravity(Gravity.CENTER);
    Den1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
    Den1.setLayoutParams(params);
    Mixed.addView(Den1);

    /* Whole 1 */
    params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    params.addRule(RelativeLayout.LEFT_OF, Num1.getId());
    params.addRule(RelativeLayout.CENTER_VERTICAL);
    Whole1.setGravity(Gravity.CENTER);
    Whole1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 40);
    Whole1.setLayoutParams(params);
    Mixed.addView(Whole1);

    Result.addView(Mixed);

    Result.invalidate();
  }
}
Community
  • 1
  • 1
Nick
  • 1
  • try setting id programmatically using setId(int id) – Saiteja Prasadam Jul 31 '16 at 03:54
  • @SaitejaPrasadam This method worked. I used setId() in the init() function, and used findViewById() with the hard coded ID. It still didn't work when I used findViewById(R.id.Whole1) though. Do you know why that is the case? – Nick Jul 31 '16 at 05:38
  • why don't you just override the textview which you have declared in the class?? simple – Saiteja Prasadam Jul 31 '16 at 05:43
  • @SaitejaPrasadam I appreciate your response! That is a viable solution. Though, I am still curious why R.id.Whole1 won't work, while hardcoding an ID of 1000, for example, will work. – Nick Jul 31 '16 at 05:53
  • Try Mixed.findViewById(...) your textview is part of relative layout. – Saiteja Prasadam Jul 31 '16 at 06:23

1 Answers1

0
 Whole1 = (TextView) findViewById(R.id.Whole1);

finds a TextView with the id R.id.Whole1 in the activity's layout. You created a TextView programmatically, assigned it to your Whole1 variable, but then overwrote the variable with the new assignment Whole1 = (TextView) findViewById(R.id.Whole1);. After that it's probably null because you dont have a TextView with that id in the layout XML that you inflated.

fweigl
  • 21,278
  • 20
  • 114
  • 205