Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug error "Cannot read property of null"

Tags:

qt

qml

In a big QML project, sometimes I get the following error :

MyItem.qml:8: TypeError: Cannot read property of null

When I look at the concerned line, it is always in a sub-Item, with a property binding to a parent property like

Item
{
    id: myItem

    ASubItem
    {
         id: subItem
         width: parent.width
    }
}

So it seems like the parent became null and the inner item is trying to update its property after that.

We sometimes delete the items from C++ because we want to create new ones, it seems to be the cause of the error messages. We also create some items from C++ (using QQmlComponent) and set the parent and parent-item to the QQuickItem that contains the items, but it seems that I get this error on other items as well.

But I don't understand why an inner item would try to update itself while the parent is null, shouldn't it be deleted at the same time ?
Is there any way to debug this to see when the parent is deleted and when its child-item is trying to update ?

like image 736
ymoreau Avatar asked Mar 28 '18 09:03

ymoreau


1 Answers

a workaround that worked for me, however my case is a little bit different since I'm creating the objects dynamically from Javascript not c++ but I'm sure you can figure it out on how to do it from c++.

I passed the QGuiApplication instance to the root QML Context

    engine.rootContext()->setContextProperty("KApp",&app);

and then made this connection

    Connections{ 
    target: KApp;
    onAboutToQuit:{
        dynamicallyCreatedObject.destroy();
    }
}

so the point is, you have to destroy the dynamically created QML objects when the aboutToQuit() signal is emitted

like image 116
sadeq Avatar answered Oct 18 '22 02:10

sadeq