strange typecast problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vipersniper5
    New Member
    • Dec 2007
    • 9

    #1

    strange typecast problem

    I am trying to typecast and object to get at methods specific to its class, but am running into a strange error at runtime.
    The line that causes the problem is simply:

    whoops2 temp=(whoops2 ) teammate;

    where teammate is declared as class which whoops2 extends, and is actually a whoops2 object. The error I'm receiving is:

    java.lang.Class CastException: whoops2 cannot be cast to whoops2
    java.lang.Class CastException: whoops2 cannot be cast to whoops2
    at whoops2.takeTur n(whoops2.java: 92)


    I've never run into a problem like this typecasting before. I feel like there must be something stupid I'm missing, but for the life of me I can't figure what. Any help would be greatly appreciated.

    Thank you
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Can we see a bit of the actual code and the actual exception message?

    kind regards,

    Jos

    Comment

    • vipersniper5
      New Member
      • Dec 2007
      • 9

      #3
      well you asked for it :-P code, leaving out irrelevant functions is
      Code:
      import java.awt.geom.*;
      import java.awt.*;
      import java.util.*;
      //import java.lang.reflect.*;
      public class whoops2 extends ClobberBot
      {
          private static final int BULLET_RANGE = 40;    //40!!!
          private static final int BOT_RANGE = 40;    
          private static final int BOT_WINDOW = 80; 
          
          private boolean tooClose; 
          private int movement;
          private int shotclock;
      	private int target;
          private String action; 
          private ClobberBotAction currAction;
          private ImmutablePoint2D[] previousBullets;
      	    /**
          * Constructor
          * @param game the Clobber object in which the game is played
          */
          public whoops2(Clobber game)
          {
              super(game);
      	  previousBullets = new ImmutablePoint2D[0];
              action = "nothing";
              movement = 0;
      	target=-1;
              tooClose = false;
              shotclock = 0;
          }
      
          /**
           * Determines the best course of action to destroy all other bots while staying alive.
           * @return ClobberBotAction The decided upon action
           * @param currState The current world state
           */
      	public void settarget(int n)
      	{
      		target=n;
      	}
          public ClobberBotAction takeTurn(WhatIKnow currState)
          {
              shotclock--;
      	  double closestBullet = 5000;
              double closestBot = 5000;
              int closestBulletIndex = 0;
              int closestBotIndex = 0;
      	boolean opponent=false;
      	
              	for(int x = 0; x<currState.bullets.size(); x++)//Find closest Bullet
              	{
                  		double bulletDist = currState.me.distance((ImmutablePoint2D)currState.bullets.get(x));
                  		if(bulletDist < closestBullet) { closestBullet = bulletDist; closestBulletIndex = x;}
              	}
      	if(target==-1)
      	{
      
              	for(int x=0; x<currState.bots.size();x++) //Find closest Bot
              	{
                  		double botDist = currState.me.distance((ImmutablePoint2D)currState.bots.get(x));
      			boolean team=false;
      			//System.err.println(""+teammates.size());
      			for(int i=0;i<teammates.size();i++)
      			{
      				if(teammates.get(i).getID()==currState.bots.get(x).getID())
      				{
      					team=true;
      					System.err.println("teammate found "+currState.bots.get(x).getID() +" " +teammates.get(i));
      					break;
      				}
      			}
                  		if(botDist < closestBot&&!team)
      		 	{
      			opponent=true;			
      			closestBot = botDist; 
      			closestBotIndex = x;
      			}
              	}
      		for(int i=0;i<teammates.size();i++)
      		{
      			[B][U]whoops2 temp=(whoops2)teammates.get(i);[/U][/B]
      			temp.settarget(currState.bots.get(closestBotIndex).getID());			
      						
      		}
      	}else
      	{
      		for(int i=0;i<currState.bots.size();i++)
      		{
      			if(currState.bots.get(i).getID()==target)
      			{
      				closestBotIndex=i;
      				closestBot=currState.me.distance((ImmutablePoint2D)currState.bots.get(i));
      				opponent=true;
      				break;
      			}
      		}	
      	}
              
              if(shotclock<=0&&opponent)//If able to shoot, shoot at closest Bot
              {
                  action = "shoot";
                  shotAction(currState, closestBotIndex);
              }
              else //unable to shoot
              {
                  action = "move";
      
                  if(closestBullet > BULLET_RANGE)//not in danger
                  {
                      moveAction(currState, closestBotIndex, closestBot);
                  }
                  else    //closest bullet is within BULLET_RANGE 
                  {
                      dodgeAction(currState, closestBulletIndex, closestBotIndex, closestBot);
                  }
              }
              previousBullets = new ImmutablePoint2D[currState.bullets.size()];
              currState.bullets.copyInto(previousBullets);
      
              currAction = createAction(action, movement);
      	target=-1;
              return currAction;
          }
      the bolded underlined line is where the error occurs.
      The full exception message is:
      java.lang.Class CastException: whoops2 cannot be cast to whoops2
      java.lang.Class CastException: whoops2 cannot be cast to whoops2
      at whoops2.takeTur n(whoops2.java: 92)
      at Clobber.getBotA ctions(Clobber. java:548)
      at Clobber.play(Cl obber.java:719)
      at Clobber.main(Cl obber.java:1077 )

      Thanks agian

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        What is the type of 'teammates'?

        kind regards,

        Jos

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          ps. I suspect an out of date class file here; or some esoteric classloading issue
          because every X (loaded by the same classloader) can be cast to itself.

          I suggest to recompile your classes and see what happens then.

          kind regards,

          Jos

          Comment

          • vipersniper5
            New Member
            • Dec 2007
            • 9

            #6
            thats why I'm so confused, is that I know I should be able to cast to itself, and that error says pretty clearly I've got the right object of the right type. I tried recompiling everything I have source for (basically whoops2 and ClobberBot) with no success. Unfortunately everything else I only have the class files and documentation for. I think I may need to break down and use reflection (I was able to get that to work, but I hate too since it shouldn't be necessary).
            Oh and in awnser to your question, teammates is of type ClobberBot (which should be fine since whoops2 extends it) and is and inherited private field from the ClobberBot class)

            If you have any great brainstorms I'd appreciate it if you passed them along but I think I'll move forward with reflection for now.

            Thanks for the help.

            Comment

            • vipersniper5
              New Member
              • Dec 2007
              • 9

              #7
              Well sure enough you were right, at least you gave me the clue I needed. I was able to the code for the main part of the game, and each bot in the game is loaded by the same class of ClassLoader, but a different instantiation.
              Thank you again for the help!

              Comment

              • BigDaddyLH
                Recognized Expert Top Contributor
                • Dec 2007
                • 1216

                #8
                If you really want the best help from this forum I suggest the following. Recreate your error in a short (<1 page) example program that forum members can copy and run, and post that example program to the forum.

                That's how I solve sticky problems. I write the shortest possible program that expresses them, and often the solution is staring me in the face. In any case, when I solve it, I add it to my directory of brain teasers, and if it ever comes up again in another program, I can go back and see exactly how I solved it.

                Comment

                Working...