• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sub/Super-class Type casting error after upgrade to Java 1.5  RSS feed

 
Warren Kinny
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

I'm sure someone knowledgable will see this instantly.

I have recently been trying to resurrect some old Java code that used to work fine (Developed in Java 1.4)



The following line of code used to compile but now shows an error in Java 1.5 / 1.6


It gets an error "inconvertible types. Required: ExTemplate, Found: javax.swing.JFrame".

I have searched the forums and read all I can about sub-type / super-type casting, and I cannot understand why the code is considered wrong, seeing as we are telling the compiler that the template_reference variable contains a Sub-type of JFrame, namely am ExTemplate.

I presume it must be some difference between 1.4 and 1.5 in what it accepts as possble casting ??

Any help greatly appreciated !!
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Warren, welcome to CodeRanch.

Please PostRealCode. Your example code is full of errors which makes it hard to figure out what the problem could be. Try posting an SSCCE so we can try it for ourselves.
 
Warren Kinny
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Hi Warren, welcome to CodeRanch.

Please PostRealCode. Your example code is full of errors which makes it hard to figure out what the problem could be. Try posting an SSCCE so we can try it for ourselves.



Hello Stephen and thanks for yor reply.

However you are quite wrong in your assertion that the code is not actual code (other than my obvious additional comment on the RHS) . The code is simply abbreviated (and indeed self-contained) to allow you to see the problem... as I say this code used to happily compile and run in Java SDK 1.4... and the ONLY line of code causing a compile error is the one I have included.

I have also provided the accompanying class structures to enable you to understand the relationship between exTemplate and JFrame (which is the cause of the casting problem).

So if you think there code is "full of errors" then by all menas please point them out !

Thanks,

Warren.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Warren Kinny wrote:So if you think there code is "full of errors" then by all menas please point them out !

It's not a real and compilable Java program. To start with:

The keywords public and class are not spelled with capital letters.

What's this? That's not valid Java code.

Sorry to be so nitpicky, but the more exact you are when asking your question, the better people can help you get to an answer, without being distracted by other things.

With regard to your question, nothing has changed in newer versions of Java with regard to casting. So just using a newer Java version is not the cause of your problem.
 
Warren Kinny
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:It's not a real and compilable Java program. To start with:

The keywords public and class are not spelled with capital letters.

What's this? That's not valid Java code.

Sorry to be so nitpicky, but the more exact you are when asking your question, the better people can help you get to an answer, without being distracted by other things.

With regard to your question, nothing has changed in newer versions of Java with regard to casting. So just using a newer Java version is not the cause of your problem.



Hi, Jasper and thanks for taking the time to point out what must be somewhat tedious for you... However most of it is of course irrelevant to the problem like the fact that when I transcribed code I typed "Public" rather than "public" .. as I said in my original explanation I am advised that the code used to compile so it wouldn't have been a giant leap would it for peoople to ignore those obvious typos in transcription... "wood and trees" is I believe the appropriate expression :-)

Nevertheless, let me assuage any remaining nitpickiness people have about this (and I promise to cut and paste this time lol)

I have 3 classes involved in this compile problem.

Class 1 : XW. It basically is a data structure containing a series of instance variables (IRRELEVANT to this problem) and contains 2 class variables :



Class 2 : ExTemplate. As I understand it, a SUB-Class of JFrame as a result of the "extends" keyword :


Class 3 : Another Class (the name of which is irrelevant but for those people who think not knowing is a distraction) :


IE The compiler is objecting to the attempted downcast of a JFrame Object to an ExTemplate.

Soooo... if that's not the right way to do it, how do I turn a pointer to an ExTemplate Object (currently stored in a variable that is a SuperClass of ExTemplate namely a JFrame) specifically INTO an ExTemplate object (which is what I KNOW it actually is anyway) so that the compiler is happy ?

Like I said originally, a doddle question I am sure for those more experienced than me in such matters ! (And I really did try to make it easy for people to answer the question by posting the essential information... I promise ! :-)

Thanks !

Warren.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a circular dependency between XW and ExTemplate. XW.template_reference will not be an instance of ExTemplate until some instance of ExTemplate is created and initialized in the ExTemplate constructor (an odd thing to do IMO: you are forcing one class's state to be determined by the lifecycle of another class). Until then, it's just a null reference of type JFrame. (Unless, of course, somewhere else in the code, it's getting a JFrame instance assigned to it.) This line ExTemplate Temp = (ExTemplate) XW.template_reference; does NOT invoke a constructor. It merely grabs hold of a static reference and tries to assign its value to another reference.

It's mighty hard to tell what the intent is, since you show so little, but I have to say this seems extremely ill-advised.

Edit: I don't say that this circularity is, itself, the cause of the error you are encountering, but it sure doesn't make debugging a joy. I changed my Eclipse compiler settings to 1.6 and pasted your code fragments into the editor. Eclipse's compiler was able to compile successfully. This leads me to suppose there is a problem in your code that you are not showing us.
 
Warren Kinny
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Deems wrote: XW.template_reference will not be an instance of ExTemplate until some instance of ExTemplate is created and initialized in the ExTemplate constructor. Until then, it's just a null reference of type JFrame.
This line ExTemplate Temp = (ExTemplate) XW.template_reference; does NOT invoke a constructor. It merely grabs hold of a static reference and tries to assign its value to another reference.


Hi Dennis and thanks for your feedback. Much appreciated !

Yes I am well aware that the line with the compile error does not invoke the constructor... the constructor of ExTemplate is essentially called from the Main class of the Application.. but as you can see from the code shown the line XW.template_reference = this; is INSIDE the Constructor and therefore it should be setting the variable XW.template_reference to the current (newly constructed) ExTemplate.

Even if it were not making that setting, your analysis that unless the constructor is called then XW.template_reference contains a null instance of type JFrame would indicate the cause of a Run-Time Error with the logic, but remember the COMPILER has no idea whether the application has successfully set XW.template_reference to a live value of type JFrame or a sub-type..all it knows is that it is type JFrame.

Which means (ignoring the various well-founded concerns about the way the programmer who developed this may have not gone about it very elegantly) that my question remains : How do you cast a Sub-type value held in a variable of it's immediate Super-Type back to the Sub-Type for the benefit of the COMPILER ?. Yes I know that if at run-time it isn't actually containing a variable of the correct type it will generate a cast-exception message, but this is currently a COMPILE-time problem.

Dennis Deems wrote:I changed my Eclipse compiler settings to 1.6 and pasted your code fragments into the editor. Eclipse's compiler was able to compile successfully.


Ok, I'm on 1.5 so I guess I'll change try changing to 1.6 and see if that solves it..

Thanks.

** Queston in Red above still remains outstanding **
 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As stated before, your error is most likely in code you have not shown us yet.
The thing is that you posted your error as: "inconvertible types. Required: ExTemplate, Found: javax.swing.JFrame". So your template reference is actually a JFrame and not an ExTamplate.
Could you please confirm that XW.template_reference.getClass() returns a JFrame and not an ExTemplate.
That would suggest that you are initializing it with a JFrame and not with an EXTemplate at some point. You cannot downcast if its not the type you are expecting.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Warren Kinny wrote:How do you cast a Sub-type value held in a variable of it's immediate Super-Type back to the Sub-Type for the benefit of the COMPILER ?

As you have written it. And the only explanation I can offer is that ExTemplate is not actually on the type hierarchy of JFrame. I can't think of any other reason you would get that compiler error.

Dennis Deems wrote:I changed my Eclipse compiler settings to 1.6 and pasted your code fragments into the editor. Eclipse's compiler was able to compile successfully.


Ok, I'm on 1.5 so I guess I'll change try changing to 1.6 and see if that solves it.

I started with 1.5 and switched to 1.6 (I misread the subject and thought that's how you were upgrading). Eclipse compiled in both environments -- you should have no problems with version 5. I added the necessary import statements, of course.

P.S. Please break up the long lines in your code as it forces the window to expand beyond the boundaries of the screen.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!