-1
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 I counted correctly, there are 6 possible ways a null pointer can be thrown on that line. Post a stack trace. –  May 27 '13 at 21:12
  • 6
    Do you want to know what a null pointer exception is? If so, see http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception. If you want to know where in your code the NPE was thrown, then break your line up so method calls appear on separate lines, then see Legend's comment. – Ray Toal May 27 '13 at 21:13
  • @LukasEder It could at least provide some insight at the very least. Or he could put separate statements on new lines. –  May 27 '13 at 21:14

2 Answers2

4

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:

  1. You can't call a method on a reference that doesn't refer to an actual object that implements those methods.
  2. You can't read or write a field via a reference that doesn't refer to an actual object with fields.

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.

  1. methodCall could be null causing the call to getClassName to fail
  2. cg could be null causing the call to getConstantPool to fail
  3. ALLATORI_CLASS could be null causing the call to getClassName to fail which might indicate a class-initialization cycle
  4. methodCall.getClassName(cg.getConstantPool()) might return null causing the call to contains to fail

I 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.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
0

"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.

Akryllax
  • 140
  • 1
  • 1
  • 6