if (methodCall.getClassName(cg.getConstantPool()).contains(ALLATORI_CLASS.getClassName())) {
Im trying to fix a java application, and I keep getting a null pointer exception. What is it exactly? And how would I go about fixing this line of code?
if (methodCall.getClassName(cg.getConstantPool()).contains(ALLATORI_CLASS.getClassName())) {
Im trying to fix a java application, and I keep getting a null pointer exception. What is it exactly? And how would I go about fixing this line of code?
Java allows object references to be null
meaning the reference does not refer to a real object.
None of the ways in which you normally use an object reference work with null
:
About the only thing you can do with a null
value is check whether it is null
or whether it has a nominal type (via instanceof
).
A NullPointerException
occurs when you try to use a reference to null
as above -- either calling, or manipulating a field.
Usually the problem occurs because you are trying to use a reference before it has been filled, so often you need to look for operations that are out of order. You might try to identify the code that creates whatever you're trying to use and see why it isn't running before you use it.
If the type of the reference is an interface
type, often you can implement a null-like value that actually has the methods. For example, if there is no configuration for a non-essential database driven component, you might just create a component that does nothing useful but that doesn't blow up and prevent the rest of the system from working.
In
if (methodCall.getClassName(cg.getConstantPool()) .contains(ALLATORI_CLASS.getClassName())) {
there are a few places where the problem could occur.
methodCall
could be null causing the call to getClassName
to failcg
could be null causing the call to getConstantPool
to failALLATORI_CLASS
could be null causing the call to getClassName
to fail which might indicate a class-initialization cyclemethodCall.getClassName(cg.getConstantPool())
might return null causing the call to contains
to failI would add logging for each of these to figure out which are and are not part of the problem.
Tony Hoare calls null
his "billion dollar mistake" because null dereference errors are so frequent.
I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.
He implemented it because making the language guarantee that all pointers pointed to real values often imposes a burden on users and makes constructing cyclic data-types in eager languages tricky.
Other languages use union types like option to get around this problem.
"NullPointerException
" means your targeting something "empy", usually because it does no exist. You first have to declare the variable AND initialize.
From what I can see, you're calling a method that your code can't reach, so Java thinks you want to point to "nothing". First of all, if the method IS EXPECTED to give that Exception (i. e. : Getting a String from user (Direct Input), because user can give an empty entry.), add to the method the throw declaration (i.e. public void method1() throws NullPointerException {
}
But with the only line you provided I'm not able to help you further, sorry.