When I compile this is what I get: unreported exception java.lang.NoSuchFieldException; must be caught or declared to be thrown

StackOverflow https://stackoverflow.com/questions/23691001

Pregunta

I'm trying to create a chess program on the using the GridWorld case study. You may or may not know what that is, but that is irrelevant to my problem. I've only been coding in java for about a year now in my AP computer science class, so I'm relatively new at this. What I am trying to to is convert a string to a color. I have looked it up online and in numerous places I have found this code:

Field field = Color.class.getField("YELLOW");
Color chosen = (Color)field.get(null);

I would also like to use a variable instead of "YELLOW", so this is the code that I would like to use.

Field field = Color.class.getField("chosenColor");
Color chosen = (Color)field.get(null);

But it does not work, and I don't know why. The message that I get is the title of this question. I have no idea what it means, and quite honestly I'm not all familiar with the field class, so that could be the root of my issue, but I'm just not sure. I will post my code, and I hoping that you can either help me with this error, or give me another way to convert strings to colors.

Like I said, I'm relatively new, so it would be great if you could use smaller words that my limited coding knowledge can understand.

package framework.info.gridworld;

import info.gridworld.actor.ActorWorld;
import info.gridworld.actor.*;
import info.gridworld.grid.*;


import java.awt.Color;
import java.util.*;
import java.lang.reflect.Field;
/**
 * Play a game of chess! Simple, yet oh so complicated.
 * 
 * @author M.A.Williams
 * Version one. Started: 5-15-2014 Finished: N/A
 */
public class ChessRunner
{
    public static void main(String[]args)
    {
        Scanner in= new Scanner(System.in);

        System.out.println("Pick from one of these colors! \nBlue \nCyan\nGreen\nMagenta\nOrange\nPink\nRed\nWhite\nYellow");
        System.out.println("Do not choose Black!!!");
        String chosenColor=in.next();
        chosenColor=chosenColor.toUpperCase();

        while(chosenColor.equals("BLACK"))
        {
            System.out.println("I told you not to choose Black! Choose a different color!");

            chosenColor=in.next();
            chosenColor=chosenColor.toUpperCase();
        }

        Field field = Color.class.getField("YELLOW");
        Color chosen = (Color)field.get(null);

        ActorWorld chessWorld= new ActorWorld();

        Bug playerKing=new Bug(chosen);

        chessWorld.add(new Location(7, 8), playerKing);

        chessWorld.show();
    }
}
¿Fue útil?

Solución

If you have a look at the Color API http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html

you will see that there is a Field called YELLOW http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html#YELLOW

but there is not one called chosenColor.

In your example there is absolutely not need to use reflection, rather try

if (chosenColor.equals("BLUE"))
    chosen = Color.BLUE;
else if (chosenColor.equals("YELLOW"))
    chosen = Color.YELLOW;

Otros consejos

Reflection and exception handling are advanced topics in Java, and it's best to avoid them. Instead, figure out some other way to convert a string to a color, maybe some if/else statements:

if (chosenColor.equals("BLUE"))
    chosen = Color.BLUE;
else if (chosenColor.equals("YELLOW"))
    chosen = Color.YELLOW;
// repeat for all the colors

If you don't know how to catch exceptions it is far too soon for you to be using Reflection.

You don't need it here in any case. You should just use Color.YELLOW.

This is the code that I would like to use.

Field field = Color.class.getField("chosenColor");
Color chosen = (Color)field.get(null);

This code is complete nonsense.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top