In my plugin, when I click a button it will call the findTheCityNames function which is attached below and get features if there is a particular layer. What I want is if there is no specific layer in which it is getting features, it should give a warning message "No Valid layer found" instead python error "List object has no attribute getFeatures".

def findTheCityNames(self):
    ct = self.dlg.Statecombo.currentText()
    st = "state='"+ct+"'"
    exp = QgsExpression(st)
    fidAndVal = {}        
    #QMessageBox.information(self.dlg, "info", st) 

    layer = self.iface.activeLayer()

    iterFeat = layer.getFeatures()

    for f in iterFeat:
        val = exp.evaluate(f)
        if bool(val):
            attrs = f.attributes()
            idx = layer.fieldNameIndex('name')
            cityName = f.attributes()[idx]
            fidInt = f.id()
            fidAndVal[fidInt] = cityName

    for x in fidAndVal.keys():

1 Answer 1


I think that this is most a python related question rather than a GIS question.

You can try with the "Try and except" in python sort of:

   layer with possible problem
   print this layer doesn't work.

Anyways I think is better to ask or search this in the python related StackExchange.

  • 2
    Python is case sensitive so perhaps change to try and except even though you are only presenting pseudo-code.
    – PolyGeo
    Feb 27, 2015 at 9:21
  • Thank you so much Gsanez. Really great! though my question is simple.
    – User123
    Feb 27, 2015 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.