cannot connect (null)



  • I have an array of type Tile which inherits QLabel

    std::array<PlayableTile*,2>tile
    { {
    ui->playableTile1,
    ui->playableTile2
    } };
    Error: cannot connect (null)::pushTile() to MainWindow::pushTile()

    connect(tile[0],SIGNAL(pushTile()),this,SLOT(pushTile());
    ....................^ this is where the error come froms

    But when I do

    connect(ui->playableTile1,SIGNAL(pushTile()),this,SLOT(pushTile());
    I'm not getting any erorr


  • Moderators

    @Lorence said:

    std::array<PlayableTile*,2>tile
    { {
    ui->playableTile1;
    ui->playableTile2;
    }. };

    That looks like a typo. What's your actual code?



  • Sorry for the late reply and yes it's a typo sorry, I'm just typing on my phone because my PC has a problem on connection

    Its an array, the first semicolon is a comma and forget the second one, I edited it


  • Moderators

    What do you get when you call this?:

    #include <QDebug>
    
    // ...
    
    qDebug() << tile[0];
    


  • Sorry again for the late reply, i was so busy but now im free

    What do you get when you call this?:
    This is what i get
    QObject(0x0)


  • Moderators

    @Lorence said:

    Sorry again for the late reply, i was so busy but now im free

    That's OK :)

    This is what i get
    QObject(0x0)

    You have a null pointer.



  • I fixed it by doing this

    std::array<PlayableTile*,5> playableTileArray;

    playableTileArray[0] = ui->playableTile1;
    playableTileArray[1] = ui->playableTile2;
    playableTileArray[2] = ui->playableTile3;
    playableTileArray[3] = ui->playableTile4;
    playableTileArray[4] = ui->playableTile5;
    

    C++ is now kicking me just because i havent programmed for 2 weeks.
    can you explain to me why this works?

    You have a null pointer.
    I dont get it, why i have a null pointer?


  • Qt Champions 2016

    Hi
    well if you do

    std::array<PlayableTile*,2>tile
    { {
    ui->playableTile1;
    ui->playableTile2;
    }. };
    

    in Class def. (the h file)
    then its very likely that this list is init'ed up BEFORE the
    ui->setupUi ( this );
    in the constructor so you are adding NULL pointers.
    So it's best to add them explicit and not use init lists to be on the safe side.
    So before ui->setupUi ( this ); Nothing is setup yet.
    You second example works as you do not use init list anymore so I bet
    playableTileArray[0] = ui->playableTile1;
    is after ui->setupUi ( this );

    [edit: fixed coding tags SGaist]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.