In my tree class the user can set the fall color of whatever tree they choose, but it's not taking the colors I type in when I test it.

import java.awt.Color;

public class Tree
{
    private String treeSpecies;
    private int treePrice;
    private Color currentColor;
    private Color fallColor;
    private int treeAge;

    /**
     * Creates a tree and initialises it's values.
     */
    public Tree(String species, Color autumnColor, int price){
        if (species == "") {
            throw new IllegalArgumentException("Please enter a tree species.");
        }
        if (autumnColor == null) {
            throw new IllegalArgumentException("Please enter the tree's Autumn color.");
        }
        if (price == 0) {
            throw new IllegalArgumentException("Please enter a price value greater than zero dollars.");
        }
        species = treeSpecies;
        autumnColor = fallColor;
        price = treePrice;
        currentColor = Color.GREEN;
    }
    
    public Tree(String species, Color autumnColor, int price, int age){
        if (species == "") {
            throw new IllegalArgumentException("Please enter a tree species.");
        }
        if (autumnColor == null) {
            throw new IllegalArgumentException("Please enter the tree's Autumn color.");
        }
        if (price == 0) {
            throw new IllegalArgumentException("Please enter a price value greater than zero dollars.");
        }
        if (age == 0) {
            throw new IllegalArgumentException("Please enter an age value greater than zero.");
        }
        species = treeSpecies;
        autumnColor = fallColor;
        price = treePrice;
        age = treeAge;
        currentColor = fallColor;
    }

The error I'm getting in create object is "Error: cannot find symbol - variable GREEN

Sorry, that image from your IDE doesn't help much.

Can you compile the source and get the error messages from the javac compiler?

Or can you show the source line that has the error in it.

Look in the code you posted. You used the symbol: GREEN there and it was ok. Code it the same why where you are getting the error.

Line 25: autumnColor = fallColor;

You're assigning fallColor, which is null, to your parameter autumnColor. Are you sure that's what you want to do?

Thats the thing I get no errors when I compile, only when I start testing and putting in the colors I want to use does it mess up on me.

Is this a java program that is executing while you are "testing" or is it the IDE?

Please copy and paste here the full text of the error message.

Its the IDE. I compile start a new instance of the class and go to type in my variables, but it always gives me that error with color. I've tried different colors as well, like ORANGE, but it still gives me the same problem.

I compile

This is what confused me. I don't think you are compiling the program. It looks like you are editing the source when you say:

go to type in my variables

.

Sorry, I have no idea how your IDE works.
Is there a forum for it that you could ask for help on?

Edited 6 Years Ago by NormR1: n/a

Paste here everytime you use the word GREEN in your code.

/**
     * Creates a tree and initialises it values.
     */
    public Tree(String species, Color autumnColor, int price){
        if (species == "") {
            throw new IllegalArgumentException("Please enter a tree species.");
        }
        if (autumnColor == null) {
            throw new IllegalArgumentException("Please enter the tree's Autumn color.");
        }
        if (price == 0) {
            throw new IllegalArgumentException("Please enter a price value greater than zero dollars.");
        }
        treeSpecies = species;
        fallColor = autumnColor;
        treePrice = price;
        currentColor = Color.GREEN;
    }

Just here in my first constructor.

Give this to the IDE: Color.GREEN

I tried it but it gave me the same error this time it said that the variable was color.

Attachments error2.jpg 38.37 KB

Did you import the java.awt.* classes? You need it for colors.

Did you import the java.awt.* classes? You need it for colors.

import java.awt.Color;

Yep, I made sure of that as soon as I started.

Color.GREEN has been a Color object since JDK 1.4

What level of JDK is your IDE ?

It says Java version 1.6.0_21

Doesn't really solve your exact problem, but you could have the user feed in RGB values and then make a new color from those values.

This article has been dead for over six months. Start a new discussion instead.