48

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?

user207421
  • 305,947
  • 44
  • 307
  • 483
Justin k
  • 1,104
  • 5
  • 22
  • 33
  • Please include a [SSCCE](http://www.sscce.org) if you want any help. We can't do much without a SSCCE. Thanks. Oh, and consider changing the title of your question to something a little more descriptive please. – kentcdodds May 05 '12 at 18:08
  • The stacktrace gives you the exact line where the `NullPointerException`occurs. You just have to understand why you got that and fix it... – Amokrane Chentir May 05 '12 at 18:08
  • 4
    It is solved by **identifying the problem** and then **correcting the identified problem**. NPEs [should] only occur in the case of: `someNullExpression.someMember`. That is, the `null` value must be the *target* expression of a member/method for this exception to naturally occur. In the posted example there is only *one* expression that can meet this requirement. (Hint: what does the `compiler` variable evaluate to?) –  May 05 '12 at 18:09
  • 21
    Relax, guys. He is asking what a null pointer exception is. – Jeremy May 05 '12 at 18:10
  • 1
    @pst Since the exception is in "AnotherClassLoader.loadClass", doesn't that imply the null pointer is further downstream? If compiler was null it would just be a null pointer exception right there in main. – Jeremy May 05 '12 at 18:19
  • @Jeremy It's something on line 58 of test.java... I don't think a real classloader can throw an NPE out like that, but I'm not sure now. –  May 06 '12 at 05:13
  • http://stackoverflow.com/questions/19234008/java-lang-nullpointerexception-error-how-to-figure-out – user2856095 Oct 07 '13 at 20:23
  • `@SuppressWarnings("null") public void WelcomeToNPE_MethodLand() throws NullPointerException { Object NPE_Wisher = null; try { if (!"Java".equals(NPE_Wisher)) System.out.println("Am I executed....? - User"); if (NPE_Wisher.equals(null)) //Here NPE_Wisher object will call mrNPE_Obj System.out.println("Shall I good till here...!"); } catch (NullPointerException mrNPE_Obj) { System.out.println("You already had understand me... - NullPointerException\n"); throw mrNPE_Obj; } }` – ArifMustafa Feb 02 '18 at 15:02

3 Answers3

83

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!

kenju
  • 5,866
  • 1
  • 41
  • 41
Jeremy
  • 5,365
  • 14
  • 51
  • 80
7

This error occures when you try to refer to a null object instance. I can`t tell you what causes this error by your given information, but you can debug it easily in your IDE. I strongly recommend you that use exception handling to avoid unexpected program behavior.

Ehsan Khodarahmi
  • 4,772
  • 10
  • 60
  • 87
2

Just a shot in the dark(since you did not share the compiler initialization code with us): the way you retrieve the compiler causes the issue. Point your JRE to be inside the JDK as unlike jdk, jre does not provide any tools hence, results in NPE.

aviad
  • 8,229
  • 9
  • 50
  • 98