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();
}
}