• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.lang.NullPointerException using javax.script package  RSS feed

 
VikasKumar Gupta
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • 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: 18
1
  • Mark post as helpful
  • send pies
  • 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: 18
1
  • Mark post as helpful
  • send pies
  • 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
 
Tim Moores
Saloon Keeper
Posts: 3961
94
  • Mark post as helpful
  • send pies
  • 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: 18
1
  • Mark post as helpful
  • send pies
  • 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: 18
1
  • Mark post as helpful
  • send pies
  • 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: 3961
94
  • Mark post as helpful
  • send pies
  • 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: 18
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 56223
171
  • Mark post as helpful
  • send pies
  • 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: 18
1
  • Mark post as helpful
  • send pies
  • 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: 56223
171
  • Mark post as helpful
  • send pies
  • 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: 18
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Campbell.

Regards,
Vikas
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!