Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. I want check if one of my variables is null
Forum Updated to NodeBB v4.3 + New Features

I want check if one of my variables is null

Scheduled Pinned Locked Moved Solved C++ Gurus
7 Posts 4 Posters 8.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Armin
    wrote on last edited by kshegunov
    #1

    Hi,
    I want check if one of my variables is null
    some code will execute .
    This is my Code :

    if (ui->spinBox->text != &NULL )
    {
        {
        ary[counter]=price;
               counter++;
        }
    ui->spinBox->clear();
    }
    }
    

    But i get this error:

    error: lvalue required as unary '&' operand
     if (ui->spinBox->text != &NULL )
                               ^
    

    Why?
    Thanks.

    [Moved to C++ Gurus ~kshegunov]

    kshegunovK 1 Reply Last reply
    0
    • A Armin

      Hi,
      I want check if one of my variables is null
      some code will execute .
      This is my Code :

      if (ui->spinBox->text != &NULL )
      {
          {
          ary[counter]=price;
                 counter++;
          }
      ui->spinBox->clear();
      }
      }
      

      But i get this error:

      error: lvalue required as unary '&' operand
       if (ui->spinBox->text != &NULL )
                                 ^
      

      Why?
      Thanks.

      [Moved to C++ Gurus ~kshegunov]

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      &NULL means nothing. You can't take the address of a constant, so the compiler is complaining about missing lvalue. Just use the implicit boolean interpretation:

      if (ui->spinBox->text)  {
          // It is not null
      }
      

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Because null doesn't have any address. It's not a pointer.

        QSpinBox::textreturns a QString. If you want to check if it's null then use QString::isNull.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • A Offline
          A Offline
          Armin
          wrote on last edited by
          #4

          Thanks

          i use this code:

          QString s1 = "" ;

          if ((ui->spinBox->text) != s1 )
          
          

          and still get error

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Because text is a function. Why not use isNull or isEmpty ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            3
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              text is a method, not a member, if ((ui->spinBox->text()) != s1 ) yet again, as said above, the correct way would be if (ui->spinBox->text().isEmpty())

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              3
              • A Offline
                A Offline
                Armin
                wrote on last edited by
                #7

                Thanks for all
                i solved my problem with this code :

                if (ui->spinBox->text().isEmpty())

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved