Weird class cast behavior
So I have this code:
May look a bit messy but essentially the class hierarchy of
Searcher
is
Searcher extends SearcherBase extends Pointer. However, even though Searcher extends Pointer, when the line
runs, it gives me this:
Exception in thread "main" java.lang.ClassCastException: class org.bytedeco.javacpp.Pointer cannot be cast to class com.niki.nativeplayer.Bridge.Searcher (org.bytedeco.javacpp.Pointer and com.niki.nativeplayer.Bridge.Searcher are in unnamed module of loader 'app')
there are two Searcher.alloc()
functions, one is a setter and one is a getter. I think that part is self explanatory.
I tried to replicate this error with this:
But this code ,which should be pretty much equivalent, doesn't throw that exception.32 Replies
⌛
This post has been reserved for your question.
Hey @nikcho-kouhai! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
No, nothing is self explanatory
The error says you have an object that's a Pointer and you're trying to cast it to Searcher. That's not going to work
The second example you show doesn't do that at all
well alright
though how doesn't it do that?
I htink I do pretty much the exact same in the two examples
you can pretty much ignore Searcher.alloc to be honest. It's literally just a function call
that's exactly what I do in the short example code I feel like
I haven't sent the entire Searcher, SearcherBase and Pointer sources because they are somewhat long in the original post but the point is Searcher extends Pointer
The method itself even returns fine. The problem comes when I try to cast it
and I think I forgot to mention that this isn't a compile time error, but a runtime exception. So I know for a fact Searcher extends Pointer, because
return new JSearcher();
doesn't cause issues.Look, you just did not show any "first examples". As you said, what you showed doesn't exactly matter when you investigate a class cast exception
alright, what do you want to see?
The error tells you you have an object that's a Pointer and you try to cast it to Searcher. Your second example does not do that.
well yeah but the initial code doesn't do that either. I have an object of type JSearcher that inherits Searcher that gets returned as a Pointer and then gets casted to a Searcher
Ideally, the relevant code, but considering your structure and various confusion I'm guessing you don't have a clue what's relevant.
So the next best move would be to make your program as tiny as possible so that it has the problem and nothing but that, and you show the entire program.
Maybe, maybe not. How would we know whether you have that?
you can literally see Searcher.alloc().call() which invokes the call() method that's standing literally 10 lines above it
one sec*
Why would I think I have that? It doesn't look so.
this is literally it
And what do you do with it?
this is the method that's invoked in the
line
in short- i invoke it
Maybe, maybe not
Also, how do you know that Searcher is a subtype of Pointer?
I ran it through the debugger
I know I invoke it
I am literally looking at it
I did not run it through the debugger
I am not getting a compile time error at the very least
good point
I am literally telling you I ran it through the debugger
line by line
I can send you a video if you want
And I am literally telling you that I, on the other hand, have not done that
I would create a minimalistic example of this code and send it to you
but I tried and the example works
Videos aren't interactive. While it is not impossible that it would prove your point, I'd rather not risk the frustration
well what can I do then
Most likely, the method calls aren't wired as you expect them
they literally are
one step over and I am in the call function
Rebuild the program from minimalistic towards its real version, until it breaks again
that would be stupidly difficult to be honest
something to note though
one sec
I suppose you could try giving the entire program, but if it's too big only people who aren't me are going to look at it
there's nothing to actually look at in the program. The program itself is the example I gave.. it just involves native code
You seem to understand what's going on. I'll stop bothering you about it
look this is the most minimalistic variant of the program there is
even if I made an example it would be the exact same
I can send you the other classes but I doubt that would help
something to note though
this here works perfectly fine.
(ignore that I am doing new JSearcher twice. it's just for the sake of the example)
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.