Reputation: 614
I am trying to write a program so that whenever the button called 'front' is pressed all other buttons will change to gray except the button that equals GreenButton. I am struggling with accessing the buttons in the for loop, and keep getting a null pointer exception.
The full logcat is shown below, showing that the NPE is occurring when I am trying to access the array in the loop
09-20 11:50:54.727 5302-5302/com.example.kshah.movingbutton E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kshah.movingbutton, PID: 5302
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setBackgroundColor(int)' on a null object reference
at com.example.kshah.movingbutton.MainActivity$1.onTouch(MainActivity.java:108)
at android.view.View.dispatchTouchEvent(View.java:9296)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
at android.app.Activity.dispatchTouchEvent(Activity.java:2771)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2364)
at android.view.View.dispatchPointerEvent(View.java:9520)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4230)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4096)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5922)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5896)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5857)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6025)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:323)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
CODE:
public class MainActivity extends AppCompatActivity {
private Button front;
private Button a;
private Button b1;
private Button b2;
private Button b3;
private Button b4;
Button[] buttons = {front,a,b1,b2,b3,b4};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
front = (Button) findViewById(R.id.front);
a = (Button) findViewById(R.id.a);
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b3 = (Button) findViewById(R.id.b3);
b4 = (Button) findViewById(R.id.b4);
front.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
Button GreenButton = b3;
for(int i = 0; i <= buttons.length; i++) {
if (buttons[i] == b3) {
buttons[i].setBackgroundColor(Color.GREEN);
}
else
buttons[i].setBackgroundColor(Color.GRAY);
}
return false;
}
});
}
}
Upvotes: 1
Views: 70
Reputation: 6961
for(int i = 0; i <= buttons.length; i++){do something with buttons[i]}
is NEVER a valid "for" loop for a simple reason. It will ALWAYS give you NullPointerException because during the last cycle of the loop when i == buttons.length(); you are trying to perform an action on buttons[buttons.length()] which simply DOES NOT EXIST.
Try for(int i = 0; i < buttons.length; i++){do something with buttons[i]}
Upvotes: 0
Reputation: 29783
You can try using new Button[size], something like this:
// allocates memory for 6 button.
Button[] buttons = new Button[6];
Then, add the buttons to it:
front = (Button) findViewById(R.id.front);
a = (Button) findViewById(R.id.a);
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b3 = (Button) findViewById(R.id.b3);
b4 = (Button) findViewById(R.id.b4);
buttons[0] = front;
buttons[1] = a;
buttons[2] = b1;
buttons[3] = b2;
buttons[4] = b3;
buttons[5] = b4;
Explanation:
When you creating the array with:
private Button front;
private Button a;
private Button b1;
private Button b2;
private Button b3;
private Button b4;
Button[] buttons = {front,a,b1,b2,b3,b4};
You're creating an array with null object elements, which is like
Button[] buttons = {null,null,null,null,null,null};
Then when you assign the Button to one of the array member with:
front = (Button) findViewById(R.id.front);
You're assigning an object to front
not to buttons[0]
.
You can try the following code to understand what the above explanation means:
public class MainActivity extends AppCompatActivity {
public static final String TAG = MainActivity.class.getSimpleName();
private Button front;
private Button a;
private Button b1;
private Button b2;
private Button b3;
private Button b4;
Button[] buttons = {front,a,b1,b2,b3,b4};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check address:
Log.d(TAG, "Before initializing..");
for(int i = 0; i < buttons.length; i++) {
Log.d(TAG, "i " + buttons[i]);
}
front = (Button) findViewById(R.id.front);
a = (Button) findViewById(R.id.a);
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b3 = (Button) findViewById(R.id.b3);
b4 = (Button) findViewById(R.id.b4);
Log.d(TAG, "After initializing..");
for(int i = 0; i < buttons.length; i++) {
Log.d(TAG, "i " + buttons[i]);
}
Log.d(TAG, "After assigning..");
Log.d(TAG, "front " + front);
Log.d(TAG, "a " + a);
Log.d(TAG, "b1 " + b1);
Log.d(TAG, "b2 " + b2);
Log.d(TAG, "b3 " + b3);
Log.d(TAG, "b4 " + b4);
}
}
Upvotes: 1
Reputation: 37404
You cannot store the references in an initializer array like this
Button[] buttons = {front,a,b1,b2,b3,b4};
but why ? let go to Array Initializers
The length of the array to be constructed is equal to the number of variable initializers immediately enclosed by the braces of the array initializer. Space is allocated for a new array of that length. If there is insufficient space to allocate the array, evaluation of the array initializer completes abruptly by throwing an OutOfMemoryError. Otherwise,
a one-dimensional array is created of the specified length
,and each component of the array is initialized to its default value
So here the references will just be replaced by the values mean null
hence they can no longer keep the track of change in the actual reference values
so the solution is to declare the array
or ArrayList
with reqired length and add your references to it.
// {null,null,null,....}
//Button[] buttons = {front,a,b1,b2,b3,b4};
Button[] buttons = new Button[6];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttons[0] = (Button) findViewById(R.id.front);
buttons[1] = (Button) findViewById(R.id.a);
buttons[2] = (Button) findViewById(R.id.b1);
buttons[3] = (Button) findViewById(R.id.b2);
buttons[4] = (Button) findViewById(R.id.b3);
buttons[5] = (Button) findViewById(R.id.b4);
buttons[0].setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
Button GreenButton = b3;
// note < instead of <=
for(int i = 0; i < buttons.length; i++) {
if (buttons[i] == buttons[4]) {
buttons[i].setBackgroundColor(Color.GREEN);
}
else
buttons[i].setBackgroundColor(Color.GRAY);
}
return false;
}
});
Upvotes: 2