0

I'm trying to write a piece of code that can allow people to add data about different earthquakes in to different observatories, and allow you to pull data about them.

When I run through the terminal I can add observatories, but when it comes to adding an earthquake and putting them in to specific observatories I get a java.lang.Nullpointerexception error.

public void AddQuake(String name, int magnitude, int latitude, int longitude, int year) {
    Observatory myObservatory = getObservatory(name);
    // specify an observatory to store it in every time a quake is created.
    Earthquake currentQuake = (new Earthquake(name, magnitude, latitude, longitude, year)); //setting local variable
    allQuakes.add(currentQuake); //adding to  arraylist
    if (currentQuake.GetMagnitude() > 4) {
        bigQuakes.add(currentQuake);
    }
    if (currentQuake.GetMagnitude() >= biggestMagnitudeEver) {
        biggestQuakeEver = currentQuake; // decides the biggest earthquake ever recorded.
    }

    myObservatory.addQuakeHere(currentQuake); //adding to specified observatory
}

the getObservatory method is an iterator that selects observatories from an array list of observatory objects, it works fine everywhere else.

this all works fine until the last line. addQuakeHere is a method pulled from the observatory class to add and hold earthquakes in an arraylist, but when I run it through the terminal and get to this part I get a Java.lang.nullpointerexception message. Why?

as requested, the code for the observatory class:

public class Observatory
{
  private ArrayList <Earthquake> quakes;

  public Observatory(String name, String country, int startYear, int area) {
    quakes = new ArrayList<Earthquake>(); //array list containing quakes.
    //other constructors and things omitted.

  }
  public void addQuakeHere(Earthquake quake){
    quakes.add(quake);
  }
}

as requested, the getObservatory method:

public Observatory getObservatory(String name){ // method to select a certain observatory.
  Iterator iter = allObservatories.iterator(); 
  while(iter.hasNext()) { //while there is another observatory in the queue.
    Observatory currentObserve =(Observatory) iter.next(); //move on to the next one.
    if(name == currentObserve.GetName()){
      return currentObserve; //if the name matches the input parameter, return the observatory.
    }
  }
  return null;
}
Quince
  • 14,790
  • 6
  • 60
  • 69
Finlay
  • 41
  • 10
  • 1
    `getObservatory` must be returning null, can we see the code inside that method – Quince Nov 05 '14 at 21:02
  • Either myObservatory is null or addQuakeHere is throwing the Exception. Can we see the code for the Observatory class to see why? – Robert Bain Nov 05 '14 at 21:03
  • 1
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) –  Nov 05 '14 at 21:14
  • what about getObservatory ? – Quince Nov 05 '14 at 21:16
  • apologies, there is getObservatory – Finlay Nov 05 '14 at 21:19
  • do a quick log before the return null, my money is on it not finding a matching name and returning null, if you put a log there you will more than likely see it printed – Quince Nov 05 '14 at 21:20
  • I checked and it is returning null but I can't for the life of me work out why. – Finlay Nov 05 '14 at 21:47
  • You get a NullPointerException because you have a null pointer. The exception info tells you which line. If necessary you can add printlin statements to figure out which pointer. Then you follow the history of that pointer back to where it was generated to figure out why. – Hot Licks Nov 05 '14 at 21:55

1 Answers1

0

if(name == currentObserve.GetName()){

That line.

You're comparing Strings, but not using .equals(). So it's comparing the memory locations of the two String objects, which is not equal, so it just keeps going... And never finds any, so returns null. (Not sure why it apparently works elsewhere...)

In general, you'll want to put a check for null if the method you're calling has the capability of returning null. This one can potentially fail to find an Observatory, and can thus return null. So to prevent NPE in the future, checking that getObservatory() actually returned something will be Good Practice® going forward.

  • that's nailed it, thanks so much. Been grappling with that one for so long now, such a relief. – Finlay Nov 05 '14 at 22:00