InMemoryJavaCompiler icon indicating copy to clipboard operation
InMemoryJavaCompiler copied to clipboard

NullPointerException

Open Perzan opened this issue 6 years ago • 3 comments

            `System.setProperty("java.home", "C:\\Program Files\\Java\\jdk-11.0.1");
	StringBuilder sourceCode = new StringBuilder();
	sourceCode.append("package me.Perzan.compileString;\n");
	sourceCode.append("class HelloClass {\n");
	sourceCode.append("   public static void hello() { System.out.println(\"hello\"); }");
	sourceCode.append("}");

	InMemoryJavaCompiler compiler = InMemoryJavaCompiler.newInstance();
	
	compiler.addSource("HelloClass", sourceCode.toString());
	
	
	
	Map<String, Class<?>> classMap = compiler.compileAll();
	
	Class<?> helloClass = classMap.get("HelloClass");
	Method m = helloClass.getMethod("hello");
	
	m.invoke(null);`

So at the line with 'Map<String, Class<?>> classMap = compiler.compileAll();' I get NullPointerException. I even set the property for JDK at the beginning. I'm not sure why this is happening, for my version is pretty much the same as your example...

(Also the '`' things are not part of it, I was just trying to insert the code)

Also here is the stack trace:

Exception in thread "main" java.lang.NullPointerException at org.mdkt.compiler.InMemoryJavaCompiler.compileAll(InMemoryJavaCompiler.java:80) at me.Perzan.compileString.CompileTest.main(CompileTest.java:26)

Perzan avatar Feb 06 '19 19:02 Perzan

I can confirm nullpointer exceptions. Only one time it worked, but i cannot Recall the difference from code.

fgMAI avatar Feb 09 '19 06:02 fgMAI

For me the Problem is that i generate unique filenames for the .java File,from input Code. With fixed names it does work. Check the names calling.. do it like example, object names are not arbitrary - it has to match!

fgMAI avatar Feb 10 '19 06:02 fgMAI

getting this issue

calebneuf avatar May 22 '21 01:05 calebneuf