Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App (2014) Improving Our Code Adding More Colors

Jeremy Daines
Jeremy Daines
890 Points

Error parseColor(color)

Hi there, after having tried everything you guys said, I still can't figure out how to get out of this error. It says: Length must be at least 1. Thank you for your help.

Ben Deitch
Ben Deitch
Treehouse Teacher

Could you post the code? :)

I am getting the same thing. I don't see the difference between mine and the video. The error seems to be looking for an integer when setting the colorAsInt variable with parseColor(color). Here's my code:

package com.teamtreehouse.funfactsexercise;

import android.graphics.Color;

import java.util.Random;

/**
 * Created by Brandon on 9/8/2015.
 */
public class ColorWheel {
    public String[] mColors = {
            "#39add1", // light blue
            "#3079ab", // dark blue
            "#c25975", // mauve
            "#e15258", // red
            "#f9845b", // orange
            "#838cc7", // lavender
            "#7d669e", // purple
            "#53bbb4", // aqua
            "#51b46d", // green
            "#e0ab18", // mustard
            "#637a91", // dark gray
            "#f092b0", // pink
            "#b7c0c7"  // light gray
    };
    public int getColor(){
        String color = "";
        //select a random color
        Random randomGenerator= new Random();
        int randomNumber= randomGenerator.nextInt(mColors.length);
        color= mColors[randomNumber];
        int colorAsInt = Color.parseColor(color);
        return colorAsInt;
    }
}

3 Answers

Harry James
Harry James
14,780 Points

Hey Brandon Ashcraft and Jeremy Daines!

I've managed to reproduce your issue on API 23 and here's how to solve it:

Go ahead and remove the initializing line and move the String type to its definition:

// Remove this: String color = "";
String color = mColors[randomNumber];
// ^ Add String keyword here.

So now, why is this happening? Well, it looks like in the latest build tools, Android Studio is getting a little confused and thinks because when we initialize the color String, it has no length (It is an empty string) and that the code will not work. However, we are actually setting the value later on so it should in fact work and it does.

If we run the project with the error, you'll notice that everything works fine, that's because it should do. For whatever reason, in the latest build tools it flags this code up as an error.


Hope this helps and I'll tag Ben Jakuben here to take a look at the issue. Perhaps a Teacher Note would help out here since this is a Beginner course :)

Tommy Binkley
Tommy Binkley
18,584 Points

I have the same problem and I tried what you said and now it is saying cant resolve for randomNumber

Harry James
Harry James
14,780 Points

Hey Tommy Binkey!

Could you please post your code over here and I'll take a look for you? :)

package info.cyberdlearning.funfact;

import java.util.Random;

public class ColorWheel { private String[] mColorWheel= { "#39add1", // light blue "#3079ab", // dark blue "#c25975", // mauve "#e15258", // red "#f9845b", // orange "#838cc7", // lavender "#7d669e", // purple "#53bbb4", // aqua "#51b46d", // green "#e0ab18", // mustard "#637a91", // dark gray "#f092b0", // pink "#b7c0c7" // light gray };

public int getColor(){

    //select a random color
    Random randomGenerator= new Random();
    int randomNumber= randomGenerator.nextInt(mColorWheel.length);
    String color= mColorWheel[randomNumber];
    int colorAsInt = color.parsecolor(color);
    return colorAsInt;
}

}

and im getting the same thing

i found it it was i did this int color= mColors.getColor();

vr this int color= mColorWheel.getColor();

guys just set, String Color; tats it..... instead of writing, String Color = ""; it means variable from parseColor thinks that the color shold be choose from 1 or 0 because of the string which we intiaized.