0

I'm using a java web framework (Vraptor) and I'm having the following problem: The frameworks uses Reflection to instantiate the Controllers parameters, and for some reason one of the Parameters is returing <java.lang.NullPointerException>.

My question is: the framework works normally for a while, and for some reason the "cachedConstructor" of the type I want to instantiate is filled with <java.lang.NullPointerException> instead of what is expected. What could possible make that change?

I'm not asking if the framework is doing this, I just want to know what could do this in Java

getClassType().getDeclaredConstructors() is return java.lang.NullPointerException (returning, not throwing)
Rafael Teles
  • 2,708
  • 2
  • 16
  • 32

1 Answers1

1

Most likely getClassType() is returning a null value. If you debug through your code and put some console output for values, you'll see which value is coming back as null. Do some null value checking before you use an object, if required.

See What is a NullPointerException, and how do I fix it?

Community
  • 1
  • 1
ManoDestra
  • 6,325
  • 6
  • 26
  • 50
  • The thing is, I can't do checks, because this is an internal code of the framework. getClassType() is not returning null, the getDeclaredConstructors is return java.lang.NullPointerException (not null) – Rafael Teles Mar 28 '16 at 13:46
  • Yes, because the class type is almost certainly null. Or something inside the getDeclaredConstructors() method is returning null. But more likely that the getClassType() method is returning a null. Which means that you're trying to getDeclaredConstructors() on a null, which raises the NullPointerException. Without seeing more of your code, then it's difficult to say for certain where that null is coming from. What do you pass into this "framework" code that attempts to get a class/constructors thereafter? It may be a typo in a classname, or something missing from your classpath. – ManoDestra Mar 28 '16 at 13:49
  • I know this may be confuse, but the getDeclaredConstructors call is not throwing an expecption, like I said, getClassType is returning the expected type, but the getDeclaredConstructors is return java.lang.NullPointerException (not throwing), like if it was return the constructor for the expection (or something like that) – Rafael Teles Mar 28 '16 at 13:53
  • I would be immensely surprised to find getDeclaredConstructors() returning an exception. It has to be throwing it. What would be the point of a method called getDeclaredConstructors() returning an exception? That makes zero sense. One of the constructors may have that it throws an exception as part of its signature, but that's a different matter entirely. But in that case, you have no issue to fix here. – ManoDestra Mar 28 '16 at 14:40
  • I also don't undestand... I made a few more tests and found out the even calling MyEntity.class.getDeclaredConstructors() is returning the same way. So when I call MyEntity.class.newInstance() a NullPointerException is throw, but the class has a default contrusctor, is like something is changing (at some point that I can't indetify) the class cachedConstructor or something like that... I'm 100% sure is my code, and if it is the framework it has to be with reflection (I debugged the only point that can change the cachedConstructor reference without using reflection) – Rafael Teles Mar 28 '16 at 15:10
  • Test your binary class with javap.exe. javap -cp YourJar.jar; com.example.namespace.YourClassName. Ensure that it's on the classpath. Ensure that whatever you are passing to this framework is the fully qualified classname (correctly cased, etc). The only way you would then get a NullPointerException would be if getClassType() is returning a null and causing the exception when you try to call getDeclaredConstructors() on it. Or getDeclaredConstructors() is throwing it. It's the only two possibilities here. You need to check what you're passing in and your runtime classpath. – ManoDestra Mar 28 '16 at 15:25