ClassCastException (Thing to Bridge)

Hello community!

Setup:
jdk1.8.0_201
openhab 2.4.0 (Windows 10)

Problem:
Currently I’m working on a binding for Studer inverters. I took the Fronius binding as a template because I’m very new to binding development. Unfortunately I’m stuck with a problem which is, from what I have seen, pretty common but I can’t find a solution out there.

I need to cast a Thing to Bridge but it always throws a ClassCastException:
org.eclipse.smarthome.core.thing.internal.ThingImpl cannot be cast to org.eclipse.smarthome.core.thing.Bridge

Here is my bridge.xml

<?xml version="1.0" encoding="UTF-8"?>
<thing:thing-descriptions bindingId="studerbinding"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:thing="http://eclipse.org/smarthome/schemas/thing-description/v1.0.0"
	xsi:schemaLocation="http://eclipse.org/smarthome/schemas/thing-description/v1.0.0 http://eclipse.org/smarthome/schemas/thing-description-1.0.0.xsd">

	<!-- Sample Thing Type -->
	<bridge-type id="bridge">
		<label>Studer bridge</label>
		<description>Bridge to connect to Studer inverters</description>

		
		<config-description>
			<parameter name="usr" type="text" required="true">
				<label>Username</label>
				<description>Please enter your username here.</description>
			</parameter>
			<parameter name="pwd" type="text" required="true">
                <label>Password</label>
                <description>Please enter your password here.</description>
            </parameter>
            <parameter name="refreshintv" type="integer" min="2">
                <label>Refresh interval</label>
                <description>Specifies the refresh interval in seconds</description>
                <default>10</default>
            </parameter>
		</config-description>

	</bridge-type>

</thing:thing-descriptions>

My thing-types.xml

<?xml version="1.0" encoding="UTF-8"?>
<thing:thing-descriptions bindingId="studerbinding"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:thing="http://eclipse.org/smarthome/schemas/thing-description/v1.0.0"
	xsi:schemaLocation="http://eclipse.org/smarthome/schemas/thing-description/v1.0.0 http://eclipse.org/smarthome/schemas/thing-description-1.0.0.xsd">

	<!-- Sample Thing Type -->
	<thing-type id="inverter">
	   <supported-bridge-type-refs>
	       <bridge-type-ref id="bridge" />
	   </supported-bridge-type-refs>
	   
		<label>Studer inverter</label>
		<description>No description.</description>

		<channels>
			<channel id="socharge" typeId="state_of_charge" />  <!-- 3007 -->
			<channel id="ipower" typeId="input_power" />        <!-- 3138 -->
			<channel id="opower" typeId="output_power" />       <!-- 3139 -->
		</channels>

		<config-description>
			<parameter name="deviceid" type="integer" required="true">
				<label>Device ID</label>
				<description>Unique Device ID</description>
			</parameter>
			
		</config-description>
    </thing-type>

	<!-- Sample Channel Type -->
	<channel-type id="state_of_charge">
		<item-type>Number</item-type>
		<label>State of Charge</label>
		<description>Actual state of charge of the batteries in percent.</description>
		<state pattern="%f" readOnly="true"></state>
	</channel-type>
	<channel-type id="input_power">
        <item-type>Number</item-type>
        <label>Input power</label>
        <description>Input power in kVA</description>
        <state pattern="%f kVA" readOnly="true"></state>
    </channel-type>
    <channel-type id="output_power">
        <item-type>Number</item-type>
        <label>Output Power</label>
        <description>Output power in kVA</description>
        <state pattern="%f kVA" readOnly="true"></state>
    </channel-type>

</thing:thing-descriptions>

And HandlerFactory where it throws the exception:

/**
 * Copyright (c) 2014,2019 by the respective copyright holders.
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package org.openhab.binding.studerbinding.internal;

import static org.openhab.binding.studerbinding.internal.StuderBindingConstants.*;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory;
import org.eclipse.smarthome.core.thing.binding.ThingHandler;
import org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory;
import org.openhab.binding.studerbinding.internal.handler.StuderBridgeHandler;
import org.openhab.binding.studerbinding.internal.handler.StuderInverterHandler;
import org.osgi.service.component.annotations.Component;

/**
 * The {@link StuderHandlerFactory} is responsible for creating things and thing
 * handlers.
 *
 * @author Markus Kern - Initial contribution
 */
@NonNullByDefault
@Component(configurationPid = "binding.studerbinding", service = ThingHandlerFactory.class)
public class StuderHandlerFactory extends BaseThingHandlerFactory {

    private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = new HashSet<ThingTypeUID>() {
        private static final long serialVersionUID = 1L;
        {
            add(THING_TYPE_INVERTER);
            add(THING_TYPE_BRIDGE);
        }
    };

    @Override
    public boolean supportsThingType(ThingTypeUID thingTypeUID) {
        return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
    }

    @Override
    protected @Nullable ThingHandler createHandler(Thing thing) {
        ThingTypeUID thingTypeUID = thing.getThingTypeUID();

        if (thingTypeUID.equals(THING_TYPE_INVERTER)) {
            return new StuderInverterHandler(thing);
        } else if (thingTypeUID.equals(THING_TYPE_BRIDGE)) {
            return new StuderBridgeHandler((Bridge) thing);
        }

        return null;
    }
}

Let me know if you need more information.
I hope someone can help me.

Greetings,
m4k3r

Check org.openhab.binding.studerbinding.internal.StuderBindingConstants. Make sure that you have distinct and matching values for THING_TYPE_INVERTER and THING_TYPE_BRIDGE.

If you require a bridge from thing then you might look into thing.getBridge(). Some things might function without bridges thus you will get null there, however from what I see in your thing-types.xml, your inverter comes with some form of bridge.