• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java.lang.NullPointerException using javax.script package

 
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone,
I am trying to use javax.script package. I wrote one small program but it is giving nullPointerException. Below is my code. Please check and let me know what is the problem



i tried using "javascript" and "nashorn" but in all the cases it is throwing nullPointer.  Please give me hint what could be the reason.

Regards,
Vikas
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Print out the returned value from that method in line 3: I presume it displays null.
Have you registered any engines, as required by the ScriptEngineManager#getEngineByName() method?
 
VikasKumar Gupta
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think i mentioned it in my code.
"ScriptEngine engine = factory.getEngineByName("javascript");"
Is there any other way to do it?

Regards,
Vikas
 
VikasKumar Gupta
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used following code to get register scripts and I got null.

List<ScriptEngineFactory> factories = manager.getEngineFactories();
System.out.println(factories.size());

output is : 0.

Not able to understand what is wrong.

Regards,
Vikas
 
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something odd must be going on. The code runs fine for me with both "nashorn" and "javascript", as it should.

Which Java version are you using?
 
VikasKumar Gupta
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:Something odd must be going on. The code runs fine for me with both "nashorn" and "javascript", as it should.

Which Java version are you using?



java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) Client VM (build 25.111-b14, mixed mode)

Do we need to register script engine first and then use it?
 
VikasKumar Gupta
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

VikasKumar Gupta wrote:

Tim Moores wrote:Something odd must be going on. The code runs fine for me with both "nashorn" and "javascript", as it should.

Which Java version are you using?



java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) Client VM (build 25.111-b14, mixed mode)

Do we need to register script engine first and then use it?



One weird thing i noticed. If i run it through eclipse then it is not working but if i running as stand-alone java program through command line it is working.
Any clue on this?

Regards,
Vikas
 
Tim Moores
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Do we need to register script engine first and then use it?


No. As I said, the above code works fine for me.

If i run it through eclipse then it is not working but if i running as stand-alone java program through command line it is working.


Good to hear you sorted it out. Just another reminder that IDEs are not the same runtime environment one would normally encounter. I've no idea why that happens, though.
 
VikasKumar Gupta
Greenhorn
Posts: 26
1
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:

Do we need to register script engine first and then use it?


No. As I said, the above code works fine for me.

If i run it through eclipse then it is not working but if i running as stand-alone java program through command line it is working.


Good to hear you sorted it out. Just another reminder that IDEs are not the same runtime environment one would normally encounter. I've no idea why that happens, though.



Issue is sorted now. When i am calling it from eclipse, I have to pass "null" in "ScriptEngineManager" constructor and it starts working.

eg : ScriptEngine engine = new ScriptEngineManager(null).getEngineByName("nashorn");

Thanks to all for kind help.

Regards,
Vikas
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So it works without the constructor call from the command line and not from Eclipse? How strange.
Are you using the same version of Java® for both? Get the version from the command line with java -version and for Eclipse from Project→Properties.
 
VikasKumar Gupta
Greenhorn
Posts: 26
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:So it works without the constructor call from the command line and not from Eclipse? How strange.
Are you using the same version of Java® for both? Get the version from the command line with java -version and for Eclipse from Project→Properties.



Yes, I am using the same version of java at both the places. I did not say without constructor but what i meant was in command line I used the constructor without argument and through eclipser I just passed the argument as 'null' to the constructor.

command line i used : ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");

Eclipse : ScriptEngine engine = new ScriptEngineManager(null).getEngineByName("nashorn");
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see what you mean abotu constructors; I think it a bit strange that the get engine by name method is instance rather than static.

Anyway: have a cow for all the effort put into this problem
 
VikasKumar Gupta
Greenhorn
Posts: 26
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Campbell.

Regards,
Vikas
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic