473,385 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.ClassCastException: whoops2 cannot be cast to whoops2
java.lang.ClassCastException: whoops2 cannot be cast to whoops2
at whoops2.takeTurn(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
Dec 9 '07 #1
7 1489
JosAH
11,448 Expert 8TB
Can we see a bit of the actual code and the actual exception message?

kind regards,

Jos
Dec 9 '07 #2
well you asked for it :-P code, leaving out irrelevant functions is
Expand|Select|Wrap|Line Numbers
  1. import java.awt.geom.*;
  2. import java.awt.*;
  3. import java.util.*;
  4. //import java.lang.reflect.*;
  5. public class whoops2 extends ClobberBot
  6. {
  7.     private static final int BULLET_RANGE = 40;    //40!!!
  8.     private static final int BOT_RANGE = 40;    
  9.     private static final int BOT_WINDOW = 80; 
  10.  
  11.     private boolean tooClose; 
  12.     private int movement;
  13.     private int shotclock;
  14.     private int target;
  15.     private String action; 
  16.     private ClobberBotAction currAction;
  17.     private ImmutablePoint2D[] previousBullets;
  18.         /**
  19.     * Constructor
  20.     * @param game the Clobber object in which the game is played
  21.     */
  22.     public whoops2(Clobber game)
  23.     {
  24.         super(game);
  25.       previousBullets = new ImmutablePoint2D[0];
  26.         action = "nothing";
  27.         movement = 0;
  28.     target=-1;
  29.         tooClose = false;
  30.         shotclock = 0;
  31.     }
  32.  
  33.     /**
  34.      * Determines the best course of action to destroy all other bots while staying alive.
  35.      * @return ClobberBotAction The decided upon action
  36.      * @param currState The current world state
  37.      */
  38.     public void settarget(int n)
  39.     {
  40.         target=n;
  41.     }
  42.     public ClobberBotAction takeTurn(WhatIKnow currState)
  43.     {
  44.         shotclock--;
  45.       double closestBullet = 5000;
  46.         double closestBot = 5000;
  47.         int closestBulletIndex = 0;
  48.         int closestBotIndex = 0;
  49.     boolean opponent=false;
  50.  
  51.             for(int x = 0; x<currState.bullets.size(); x++)//Find closest Bullet
  52.             {
  53.                     double bulletDist = currState.me.distance((ImmutablePoint2D)currState.bullets.get(x));
  54.                     if(bulletDist < closestBullet) { closestBullet = bulletDist; closestBulletIndex = x;}
  55.             }
  56.     if(target==-1)
  57.     {
  58.  
  59.             for(int x=0; x<currState.bots.size();x++) //Find closest Bot
  60.             {
  61.                     double botDist = currState.me.distance((ImmutablePoint2D)currState.bots.get(x));
  62.             boolean team=false;
  63.             //System.err.println(""+teammates.size());
  64.             for(int i=0;i<teammates.size();i++)
  65.             {
  66.                 if(teammates.get(i).getID()==currState.bots.get(x).getID())
  67.                 {
  68.                     team=true;
  69.                     System.err.println("teammate found "+currState.bots.get(x).getID() +" " +teammates.get(i));
  70.                     break;
  71.                 }
  72.             }
  73.                     if(botDist < closestBot&&!team)
  74.              {
  75.             opponent=true;            
  76.             closestBot = botDist; 
  77.             closestBotIndex = x;
  78.             }
  79.             }
  80.         for(int i=0;i<teammates.size();i++)
  81.         {
  82.             whoops2 temp=(whoops2)teammates.get(i);
  83.             temp.settarget(currState.bots.get(closestBotIndex).getID());            
  84.  
  85.         }
  86.     }else
  87.     {
  88.         for(int i=0;i<currState.bots.size();i++)
  89.         {
  90.             if(currState.bots.get(i).getID()==target)
  91.             {
  92.                 closestBotIndex=i;
  93.                 closestBot=currState.me.distance((ImmutablePoint2D)currState.bots.get(i));
  94.                 opponent=true;
  95.                 break;
  96.             }
  97.         }    
  98.     }
  99.  
  100.         if(shotclock<=0&&opponent)//If able to shoot, shoot at closest Bot
  101.         {
  102.             action = "shoot";
  103.             shotAction(currState, closestBotIndex);
  104.         }
  105.         else //unable to shoot
  106.         {
  107.             action = "move";
  108.  
  109.             if(closestBullet > BULLET_RANGE)//not in danger
  110.             {
  111.                 moveAction(currState, closestBotIndex, closestBot);
  112.             }
  113.             else    //closest bullet is within BULLET_RANGE 
  114.             {
  115.                 dodgeAction(currState, closestBulletIndex, closestBotIndex, closestBot);
  116.             }
  117.         }
  118.         previousBullets = new ImmutablePoint2D[currState.bullets.size()];
  119.         currState.bullets.copyInto(previousBullets);
  120.  
  121.         currAction = createAction(action, movement);
  122.     target=-1;
  123.         return currAction;
  124.     }
  125.  
the bolded underlined line is where the error occurs.
The full exception message is:
java.lang.ClassCastException: whoops2 cannot be cast to whoops2
java.lang.ClassCastException: whoops2 cannot be cast to whoops2
at whoops2.takeTurn(whoops2.java:92)
at Clobber.getBotActions(Clobber.java:548)
at Clobber.play(Clobber.java:719)
at Clobber.main(Clobber.java:1077)

Thanks agian
Dec 9 '07 #3
JosAH
11,448 Expert 8TB
What is the type of 'teammates'?

kind regards,

Jos
Dec 9 '07 #4
JosAH
11,448 Expert 8TB
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
Dec 9 '07 #5
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.
Dec 10 '07 #6
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!
Dec 10 '07 #7
BigDaddyLH
1,216 Expert 1GB
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.
Dec 10 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Grzegorz Kaczor | last post by:
Hello all, I've got a VERY strange network problem with Win2k Server and .NET. I've got one central server (hub) getting raw binary data (files) from many locations. Both server and clients...
7
by: M O J O | last post by:
Hi, I'm developing a asp.net application and ran into a strange css problem. I want all my links to have a dashed underline and when they are hovered, it must change to a solid line. Sounds...
0
by: Grzegorz Kaczor | last post by:
Hello, I've got a VERY strange network problem with Win2k Server and .NET. I've got one central server (hub) getting raw binary data (files) from many locations. Both server and clients are...
14
by: James Wong | last post by:
Hi! everybody, I'm facing a quite strange download problem. I use the following code to download an XML file to client side: With Response ' clear buffer Call .Clear() ' specify the...
1
by: big DWK | last post by:
Hi- We're having a strange caching problem with an ASP.Net app written in VB.Net on Windows 2003 with all the updates. We have two websevers that use a common sql backend and a session server to...
4
by: arcimboldo | last post by:
Hello, I got a strange link problem; here are the simplest files showing the error: a.h:: #include <utility> class a{ public:
9
by: Me | last post by:
Hi, I ran into a malloc problem but I can't find the solution. I try to read a file into a variable with malloc like this: BYTE *lcdata; lcdata = malloc(fsize*sizeof(BYTE));
11
by: Manikandan | last post by:
Hi, I am using the following snippet to compare an object with integer in my script. if ( $forecast < 4 ) { I got the "segmentation fault" error message when i executed this script in CLI....
1
by: shaneal | last post by:
Hello all, I've been trying to learn some javascript and I ran into a strange scoping problem I was hoping someone here could help with. I have a fair amount of experience with functional...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.