The xml code:
<RelativeLayout
android:layout_width="193dp"
android:layout_height="100dp"
android:background="@color/RED"
android:layout_alignParentBottom="true"
android:id="@+id/butt1"></RelativeLayout>
<RelativeLayout
android:layout_width="193dp"
android:layout_height="100dp"
android:background="@color/YELLOW"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:id="@+id/butt2"></RelativeLayout>
<RelativeLayout
android:layout_width="193dp"
android:layout_height="100dp"
android:background="@color/BLUE"
android:layout_above="@+id/butt2"
android:layout_alignParentRight="true"
android:id="@+id/butt3"></RelativeLayout>
<RelativeLayout
android:layout_width="193dp"
android:layout_height="100dp"
android:background="@color/GREEN"
android:layout_above="@+id/butt1"
android:id="@+id/butt4"></RelativeLayout>
The java code:
butt4 = (RelativeLayout)findViewById(R.id.butt4);
butt3 = (RelativeLayout)findViewById(R.id.butt3);
butt1 = (RelativeLayout)findViewById(R.id.butt1);
butt2 = (RelativeLayout)findViewById(R.id.butt2);
butt1.setBackgroundColor(Color.RED);
butt2.setBackgroundColor(Color.BLUE);
butt3.setBackgroundColor(Color.GREEN);
butt4.setBackgroundColor(Color.YELLOW);
The Log:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method
'void android.view.View.setBackgroundColor(int)' on a null object reference
at com.example.chirag.red.Hard.change(Hard.java:73)
at com.example.chirag.red.Hard.onCreate(Hard.java:53)
at android.app.Activity.performCreate(Activity.java:6672)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
The app crashes every time this activity starts. The error is always at the line, "butt1.setBackgroundColor()" and it gives me a null pointer exception. I have given the layout an id, referenced it and also passed a color as the parameter in the method. I really don't see where i've gone wrong. Please help!