-
Notifications
You must be signed in to change notification settings - Fork 782
null is not an accepted type so convert it to an accepted type #3521
null is not an accepted type so convert it to an accepted type #3521
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some context
if (item != null && !isAccepted(item, state)) { | ||
for (Class<? extends State> acceptedType : item.getAcceptedDataTypes()) { | ||
State convertedState = state.as(acceptedType); | ||
if (convertedState != null) { | ||
LoggerFactory.getLogger(ItemUtil.class).debug("Converting {} '{}' to {} '{}' for item '{}'", | ||
state.getClass().getSimpleName(), state.toString(), | ||
convertedState.getClass().getSimpleName(), convertedState.toString(), item.getName()); | ||
state.getClass().getSimpleName(), state, convertedState.getClass().getSimpleName(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should not call toString on logging arguments, just pass them on as an object they will be converted by logging framework if the log level applies
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
if (state == null) { | ||
return UnDefType.NULL; | ||
} | ||
|
||
if (item != null && !isAccepted(item, state)) { | ||
for (Class<? extends State> acceptedType : item.getAcceptedDataTypes()) { | ||
State convertedState = state.as(acceptedType); | ||
if (convertedState != null) { | ||
LoggerFactory.getLogger(ItemUtil.class).debug("Converting {} '{}' to {} '{}' for item '{}'", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
imho this is a real strange solution to overcome a static logger. Now you are going to re-fetch the logger from the factory each time you pass by this debug (!!!) statement which is really often for this specific line. The coding guidelines leave room to have static loggers if required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kaikreuzer what is your view on this specific solution
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
static logger would definitely be ok for me here
@@ -75,14 +75,18 @@ public static void assertValidItemName(String itemName) throws IllegalArgumentEx | |||
} | |||
} | |||
|
|||
public static State convertToAcceptedState(final State state, final Item item) { | |||
public static State convertToAcceptedState(State state, Item item) { | |||
if (state == null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the core of the actual change :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder how null
actually gets in here. IMHO we should fix the root cause of it first. Still, it's valid to make it fail-safe here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@martinvw I think we should not silently swallow this situation, but log an error (incl. stacktrace) nonetheless to highlight that there is some bug somewhere in the code as we should never get here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a log statement
if (itemName != null && !itemName.isEmpty()) { | ||
return itemName.matches("[a-zA-Z0-9_]*"); | ||
} | ||
return StringUtils.isNotEmpty(itemName) && itemName.matches("[a-zA-Z0-9_]*"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry could not leave it like that :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, do we think that itemName of 0 characters is valid....?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 and no, AFAIK it needs to be a least one character long
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and no, AFAIK it needs to be a least one character long
@kaikreuzer agree? Should it be a separate PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Certainly agree. Yes, to be clean, you can do it in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just see that we just did a isNotEmpty check so only the regex looked misleading but nothing will change :-D
@kaikreuzer imho this should solve the problem which was mentioned in the issue #3520 how were you able to reproduce it? And on the community forum: https://community.openhab.org/t/java-lang-nullpointerexception-every-few-seconds/28944 |
The main question remains: What is the root-cause? Items should initially have a |
GroupItems without a function maybe? Then it could have been #3411 |
I think this is a likely a correct assumption... |
If I follow up the call-stack, I arrive here: Why can't it be a binding which just call's:
Items do initially have the state |
Because then (as bad as it is...) it would have failed with an NPE in |
Well spotted, yes I agree on that, so my change most likely doesn't fix the issue |
I have my sister visiting, but I'll add changes later to fix setting of the state of e group. |
if (itemName != null && !itemName.isEmpty()) { | ||
return itemName.matches("[a-zA-Z0-9_]*"); | ||
} | ||
return StringUtils.isNotEmpty(itemName) && itemName.matches("[a-zA-Z0-9_]*"); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey, you missed to remove this empty line :-)
I don't see it coming from the mentioned PR either (no call to setState which might contain |
Then I'm most likely missing something because my branch contains no function which can return null either. Can you point out the line where you think its actually assigning null to the Maybe equality can, but then all items must have state null :-S |
Another likely suspect, #3438 IMHO we should also add logging for this case, because it means there is a conversion requested which is not possible. For example requesting conversion of an |
@triller-telekom can you take a look at NPE in combination with the change and line to which I referred, do you think this might be the cause of this? Thanks! |
You are right, the issue is definitely in 6939315#diff-543ecd419447346e6a177db8638e3ad0R74 - the |
So do you mean an UNDEF is converted to null, should we change the default implementation in that case? Or should UnDefType override that method with |
Fixes #3520 Signed-off-by: Martin van Wingerden <martinvw@mtin.nl>
Not sure how we best get back the behavior we had with the "Convertibles" - I'd leave it to @SJKA & @triller-telekom to decide as they both worked on that. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks - so let's merge this as a first fix, but have @triller-telekom come up with a follow-up PR that fixes the underlying issue.
null is not an accepted type so convert it to an accepted type
Overcomes NPE's in a places where the state is broadcasted to
Fixes #3520