I keep getting the error: 'Cannot add an object of type object as a layer.'

It is regarding line 1615 of the link below (code is also copied below,

error: mapToChange.layers().set(0, ui.Map.Layer(images[selection]));

I don't understand what's wrong with my script because I have tried to change the script but the result is still an error. What am I doing wrong? please help me. here is my code: mygee script

  • The error tells you what is wrong. You are trying to add something to the map which is not a ee.Image() object. If i print your selection variable it returns 'Nitrogen Dioksida (NO2) Bulan Januari 2020'. So that is definetly not an image. You probably have to try a bit harder on creating a reproducable example and debug your code, print your intermediate outputs is always a good idea.
    – Jobbo90
    Commented Mar 8, 2022 at 7:52

1 Answer 1


The way you constructed your dictionary, containing the images was wrong. I changed line 1576 till 1599 to fix your error for the moment:

var images = {
      'Nitrogen Dioksida (NO2) Bulan Januari 2020'  : (bymonths1.first().clip(DKI_Jakarta)),
      'Nitrogen Dioksida (NO2) Bulan Februari 2020' : (bymonths2.first().clip(DKI_Jakarta)),
      'Nitrogen Dioksida (NO2) Bulan Maret 2020'    : (bymonths3.first().clip(DKI_Jakarta)),
      'Nitrogen Dioksida (NO2) Bulan April 2020'    : (bymonths4.first().clip(DKI_Jakarta)),
      'Nitrogen Dioksida (NO2) Bulan Mei 2020'      : (bymonths5.first().clip(DKI_Jakarta)),
      'Nitrogen Dioksida (NO2) Bulan Juni 2020'     : (bymonths6.first().clip(DKI_Jakarta)),
      'Nitrogen Dioksida (NO2) Bulan Juli 2020'     : (bymonths7.first().clip(DKI_Jakarta)),
      'Nitrogen Dioksida (NO2) Bulan Agustus 2020'  : (bymonths8.first().clip(DKI_Jakarta)),
      'Nitrogen Dioksida (NO2) Bulan September 2020': (bymonths9.first().clip(DKI_Jakarta)),
      'Nitrogen Dioksida (NO2) Bulan Oktober 2020'  : (bymonths10.first().clip(DKI_Jakarta)),
      'Nitrogen Dioksida (NO2) Bulan November 2020' : (bymonths11.first().clip(DKI_Jakarta)),
      'Nitrogen Dioksida (NO2) Bulan Desember 2020' : (bymonths12.first().clip(DKI_Jakarta)),
      'Nitrogen Dioksida (NO2) Bulan Januari 2021'  : (bymonths13.first().clip(DKI_Jakarta)),
      'Nitrogen Dioksida (NO2) Bulan Februari 2021' : (bymonths14.first().clip(DKI_Jakarta)),
      'Nitrogen Dioksida (NO2) Bulan Maret 2021'    : (bymonths15.first().clip(DKI_Jakarta)),
      'Nitrogen Dioksida (NO2) Bulan April 2021'    : (bymonths16.first().clip(DKI_Jakarta)),
      'Nitrogen Dioksida (NO2) Bulan Mei 2021'      : (bymonths17.first().clip(DKI_Jakarta)),
      'Nitrogen Dioksida (NO2) Bulan Juni 2021'     : (bymonths18.first().clip(DKI_Jakarta)),
      'Nitrogen Dioksida (NO2) Bulan Juli 2021'     : (bymonths19.first().clip(DKI_Jakarta)),
      'Nitrogen Dioksida (NO2) Bulan Agustus 2021'  : (bymonths20.first().clip(DKI_Jakarta)),
      'Nitrogen Dioksida (NO2) Bulan September 2021': (bymonths21.first().clip(DKI_Jakarta)),
      'Nitrogen Dioksida (NO2) Bulan Oktober 2021'  : (bymonths22.first().clip(DKI_Jakarta)),
      'Nitrogen Dioksida (NO2) Bulan November 2021' : (bymonths23.first().clip(DKI_Jakarta)),
      'Nitrogen Dioksida (NO2) Bulan Desember 2021' : (bymonths24.first().clip(DKI_Jakarta)),

Like I said in my comment, printing intermediate outputs to debug the code works well. Also trying to reduce the problem as much as possible will help you in the future to get help. Imagine if I had to go through 1600 lines of your code... I can recommend taking the stackexchange tour: https://stackexchange.com/tour

link: https://code.earthengine.google.com/20c1f3c1da19aa86fe2833de2c26aad4

  • Thank You so much for your help. I have tried to fix the error on my script but the result is the same as your work, the whole maps' results are white. What's wrong with the map's result? could you help me please? here is the code below . I'm just wanna say sorry about my script is too long and not efficient
    – Moh Faisal
    Commented Mar 8, 2022 at 15:23
  • here is the script that I have been constructing: code.earthengine.google.com/b0ddc71680a5cb4087972f315386222a
    – Moh Faisal
    Commented Mar 8, 2022 at 15:24
  • 1
    Normally it is common to ask a new question but you have to set your image visualization parameters like this: mapToChange.layers().set(0, ui.Map.Layer(images[selection], parameter)); code.earthengine.google.com/d7220b8cf208112810cef9ea377ef666 If you feel this answer helped you it helps for future reference if you accept this as the answer.
    – Jobbo90
    Commented Mar 8, 2022 at 15:48
  • Once again, Thank You so much Sir for your help. nice to know you, sir. My name is Faisal from Indonesia. I have already checked that these answers helped me.
    – Moh Faisal
    Commented Mar 8, 2022 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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