• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Using my own types on a Script Engine....

 
Ranch Hand
Posts: 85
1
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ppls how are you doing?
Im trying to expose some of my own types using the 'put' method from the java Script Engine but its guive-me a error. See my code:

See, the Actor class is a custom class of mine so when I try to use getY() method java return an error tell me that the method does not exists. Whats wrong ? Can I use my own object or only the java native ones?
Thanks in advance!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried your code. I had to make up my own class Actor because you didn't post yours. It worked without any errors and printed 200.

Does your class Actor have a public method called getY?

Here's my Actor class, very simple:
 
Vinicius Souza
Ranch Hand
Posts: 85
1
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its weird because my Actor class is on the same package com.xcretor and the method getX() is public. Im getting this error:
"sun.org.mozilla.javascript.internal.EcmaError: TypeError: Cannot find function getY in object com.xcreator.Actor@53187f60. (<Unknown source>#1) in <Unknown source> at line number 1"
Its really weird....
Here is my actor class:

 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vinicius Souza wrote:Its weird because my Actor class is on the same package com.xcretor and the method getX() is public.


Yes, but this is about the getY method, not the getX method.

And looking at your code, the getX and getY methods are not public, they have default access.

You're writing in a kind of C++-style. Specifying access specifiers in Java does not work the same as in C++. Look at this:


Do you understand what's happening here? Only the variable x has private access. All the other variables have default access. The indentation you've used is misleading - as if all the variables are private. This is the same as:


You are doing the same with the constructor and methods:

The constructor is public, but all the methods have default access.

You have to specify private or public for all variables and methods separately.
 
Vinicius Souza
Ranch Hand
Posts: 85
1
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hummm, thanks a lot friend. Solved my problem! I really was thinking like a C programmer ehehehehe.
 
It will give me the powers of the gods. Not bad for a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!