How to solve java.lang.NullPointerException error?

Justin k picture Justin k · May 5, 2012 · Viewed 825.5k times · Source

When I run my Java program, it gives me an error on this line

compiler.getTask(null, null, new DiagnosticCollector<JavaFileObject>(), null, null, compilationUnits);

Error I am getting is:

Exception in thread "main" java.lang.NullPointerException
    at AnotherClassLoader.loadClass(test.java:58)
    at test.main(test.java:30)
    at Main.main(Main.java:68)

Can you please tell me how can I solve this error?

Answer

Jeremy picture Jeremy · May 5, 2012

A NullPointerException means that one of the variables you are passing is null, but the code tries to use it like it is not.

For example, If I do this:

Integer myInteger = null;
int n = myInteger.intValue();

The code tries to grab the intValue of myInteger, but since it is null, it does not have one: a null pointer exception happens.

What this means is that your getTask method is expecting something that is not a null, but you are passing a null. Figure out what getTask needs and pass what it wants!