PhilipsHue / PhilipsHueSDK-Java-MultiPlatform-Android

The Software Development Kit for Philips Hue Java Mulfi-Platform and Android (beta)
273 stars 214 forks source link

NullPointerException when updating light state #12

Closed yeutterg closed 9 years ago

yeutterg commented 9 years ago

Fellow hue devs,

I've been implementing the SDK into a desktop app over the past week or so, and I've been able to connect to the bridge, list all lights, and add parameters like Ct and brightness to new light states. However, when I go to update the light state using either bridge.updateLightState or bridge.setLightStateForDefaultGroup, execution halts on this line and Eclipse throws out a null pointer exception.

The (somewhat out of date) javadoc says this will occur if either the light identifier or the state are null, at least for setLightStateForDefaultGroup. I don't believe either of these are null in my case: running lightState.validateState returns no errors, the same values are returned that I entered with commands like lightState.getCt and getBrightness, the lights are already on (and are verified to be on), and I can spit out the same light identifier that I request with light.getIdentifier. Additionally, if I use the CLIP debugger in the browser, I can control the lights just fine.

Here are the steps I am taking in a bit more detail, with the output I get in comments:

lightState = new PHLightState();

lightState.setOn(true);
lightState.setBrightness(120);
lightState.setCt(250);

System.out.println(lightState.isOn()); // prints true
System.out.println(lightState.getBrightness()); // prints 120
System.out.println(lightState.getCt()); // prints 250
System.out.println(lightState.validateState()); // prints null, meaning no errors

// Assuming lightID = 1:
public static void updateLightState(int lightID) {
    PHLight light = lightList.get(lightID - 1); // with lightList a List(PHLight) of all the lights I have
    System.out.println(light.getIdentifier()); // prints 1
    bridge.updateLightState(light, lightState, lightListener); // returns java.lang.NullPointerException
}

The same thing happens if I try to update all the lights:

bridge.setLightStateForDefaultGroup(lightState); // returns java.lang.NullPointerException

Now, I could see setLightStateForDefaultGroup failing in my case, because I have 3 full-color hue A19s, 1 hue BR30, and one hue lux A19 - the hue lux could theoretically throw an error (although a different one?) because it can't accept Ct values. However, the three A19 full-color bulbs are numbered 1-3 in my case, and if I try individually addressing any of these bulbs with updateLightState, I get the same NullPointerException.

Any ideas about what could be going wrong here?

Thanks in advance! Greg

SteveyO commented 9 years ago

@yeutterg Hi Greg,

I have just checked your code, and can't immediately see anything wrong with it. The presence of a hue lux bulb should not matter here. If you call updateLightState on a lux bulb (sending both bri and ct), you should expect the bri call to work, and the ct to return an error from the bridge (onError 6 parameter, ct, not available, in your light listener).

I tested the setLightStateForDefaultGroup and worked ok for me also (on a system with a lux bulb).

A few things to try:-

To get a light object (if the identifier is known, 3 in the below example), you can use:- PHLight light = bridge.getResourceCache().getLights().get("3"); Make sure you use a String here and not an int.

I will take another look when I get your response, as am out of ideas. If anything else occurs to me I will let you know.

Regards Steve

yeutterg commented 9 years ago

@SteveyO Hi Steve,

Thanks for all the feedback on what to look into here. It seems that my issue had something to do with referring to the wrong bridge, i.e. I was calling phHueSDK.getSelectedBridge() before I actually connected to it, then when I went to update the light state it was trying to access a bridge that did not exist anymore. That makes sense as to why I was getting a null pointer exception. So far, my code seems to run well with the hue bulbs at the correct Ct and brightness.

Also, while the lightList code I had did work fine, I will be changing it to handle strings instead, as per your recommendation.

Thanks again! Greg