-1

I get a NUllPointerException when trying to invoke a method on a button:

private FButton fButton1;
...
...
fButton1 = (FButton) findViewById(R.id.FButton1);
fButton1.setButtonColor(ContextCompat.getColor(this, fbutton_color_turquoise));    //Exception here

I am using this library here and I don't get why I get the exception?

ERROR LOG:

FATAL EXCEPTION: main
                                                   Process: com.user.app, PID: 11209
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.user.app/com.user.app.activities.StartUpScreen}: java.lang.NullPointerException: Attempt to invoke virtual method 'void info.hoang8f.widget.FButton.setButtonColor(int)' on a null object reference
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                       at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:148)
                                                       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)
                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void info.hoang8f.widget.FButton.setButtonColor(int)' on a null object reference
                                                       at com.user.app.activities.StartUpScreen.onCreate(StartUpScreen.java:44)
                                                       at android.app.Activity.performCreate(Activity.java:6237)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                       at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:148) 
                                                       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) 

my layout.xml where FButton is:

<info.hoang8f.widget.FButton
    fbutton:buttonColor="@color/fbutton_color_turquoise"
    fbutton:shadowColor="@color/fbutton_color_green_sea"
    fbutton:shadowEnabled="true"
    fbutton:shadowHeight="5dp"
    android:text="Celcius"
    android:textColor="#FFF"
    fbutton:cornerRadius="5dp"
    android:layout_marginTop="8dp"
    app:layout_constraintTop_toBottomOf="@+id/imageButton3"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_width="match_parent"
    android:id="@+id/FButton1"
    app:layout_constraintRight_toLeftOf="@+id/FButton2"
    android:layout_marginRight="8dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintHorizontal_bias="1.0" />

My ids.xml file that I had to create, since android did not recoginze the id:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="FButton1" type="id" />
</resources>
Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
Carlton
  • 2,398
  • 6
  • 28
  • 54

2 Answers2

2

It seems your fButton1 is null. Just check where you call findViewById and check your xml file whether this element really exists

Vyacheslav
  • 23,112
  • 16
  • 96
  • 174
  • 1
    Can it have something to do with that Android Studio "could not find id" when creating the `FButton`'s? I was forced to create a `ids.xml` file and add ` ` So the id in my `findViewById()`, `ids.xml` and `layout.xml` are all the same but still it throw the same exception, any ideas? – Carlton Dec 31 '16 at 20:36
0

Your error is in setting Button color, use this to change the color of Button programmatically.

setBackgroundColor(getResources().getColor(R.color.holo_light_green))

One thing more, Starting from Android Support Library-23 getColor method has been deprecated, so instead use this

ContextCompat.getColor(context, R.color.your_color);

Thus to support all versions use this,

public static final int getColor(Context context, int id) 
{
    final int version = Build.VERSION.SDK_INT;
    if (version >= 23) {
        return ContextCompatApi23.getColor(context, id);
    } 
    else {
        return context.getResources().getColor(id);
    }
}
Komal12
  • 3,119
  • 4
  • 13
  • 25
gautam
  • 1,371
  • 2
  • 13
  • 32