title: Solving NullPointerException Error in Android
body: <p>I am new to Android and Java as well. I am coming from PHP.
  The following code is taken from a Book (Pro Android 4 App Dev - Reto Meier):
  Here is an Android Code where I am facing problem:</p>
  
  <p>File: EarthquakeListFragment.java</p>
  
  <pre><code>public class EarthquakeListFragment extends ListFragment {
  
      ArrayAdapter&lt;Quake&gt; aa;
      ArrayList&lt;Quake&gt; earthquakes = new ArrayList&lt;Quake&gt;();
  
      private static final String TAG = "EARTHQUAKE";
      private Handler handler = new Handler();
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
          int layoutID = android.R.layout.simple_list_item_1;
          aa = new ArrayAdapter&lt;Quake&gt;(getActivity(), layoutID, earthquakes);
          setListAdapter(aa);
  
          Thread t = new Thread(new Runnable() {
              public void run() {
                  refreshEarthquakes();
              }
          });
          t.start();
  
      }
  
      public void refreshEarthquakes() {
  
          // Get the XML
          URL url;
          try {
              String quakeFeed = getString(R.string.quake_feed);
              url = new URL(quakeFeed);
  
              URLConnection connection;
              connection = url.openConnection();
  
              HttpURLConnection httpConnection = (HttpURLConnection) connection;
              int responseCode = httpConnection.getResponseCode();
  
              if (responseCode == HttpURLConnection.HTTP_OK) {
  
                  InputStream in = httpConnection.getInputStream();
  
                  DocumentBuilderFactory dbf = DocumentBuilderFactory
                          .newInstance();
                  DocumentBuilder db = dbf.newDocumentBuilder();
  
                  // Parse the earthquake feed
                  Document dom = db.parse(in);
                  Element docEle = dom.getDocumentElement();
  
                  // clear the old earthquake
                  earthquakes.clear();
  
                  // Get a list of each earthquake entry.
                  NodeList nl = docEle.getElementsByTagName("entry");
                  if (nl != null &amp;&amp; nl.getLength() &gt; 0) {
  
                      for (int i = 0; i &lt; nl.getLength(); i++) {
                          Element entry = (Element) nl.item(i);
                          Element title = (Element) entry.getElementsByTagName(
                                  "title").item(0);
                          Element g = (Element) entry.getElementsByTagName(
                                  "georss:point").item(0);
                          Element when = (Element) entry.getElementsByTagName(
                                  "updated").item(0);
                          Element link = (Element) entry.getElementsByTagName(
                                  "link").item(0);
  
                          String details = title.getFirstChild().getNodeValue();
                          String hostname = "http://earthquake.usgs.gov";
                          String linkString = hostname
                                  + link.getAttribute("href");
  
                          String point = g.getFirstChild().getNodeValue();
                          String dt = when.getFirstChild().getNodeValue();
                          SimpleDateFormat sdf = new SimpleDateFormat(
                                  "yyyy-MM-dd'T'hh:mm:ss'Z'");
                          Date qdate = new GregorianCalendar(0, 0, 0).getTime();
  
                          try {
                              qdate = sdf.parse(dt);
                          } catch (ParseException e) {
                              Log.d(TAG, "Date parsing exception.", e);
                          }
  
                          String[] location = point.split(" ");
                          Location l = new Location("dummyGPS");
                          l.setLatitude(Double.parseDouble(location[0]));
                          l.setLongitude(Double.parseDouble(location[1]));
  
                          String magnitudeString = details.split(" ")[1];
                          int end = magnitudeString.length() - 1;
                          double magnitude = Double.parseDouble(magnitudeString
                                  .substring(0, end));
  
                          details = details.split(",")[1].trim();
  
                          final Quake quake = new Quake(qdate, details, l,
                                  magnitude, linkString);
  
                          // Process a newly found earthquake
                          handler.post(new Runnable() {
                              public void run() {
                                  addNewQuake(quake);
                              }
                          });
  
                      }
  
                  }
              }
          }
          catch (HttpHostConnectException e) {
              Log.d(TAG, "HttpConnection Error.");
          }
  
          catch (MalformedURLException e) {
              Log.d(TAG, "MalformedURLException");
          } catch (IOException e) {
              Log.d(TAG, "IOException");
          } catch (ParserConfigurationException e) {
              Log.d(TAG, "Parser Config Exception");
          } catch (SAXException e) {
              Log.d(TAG, "SAX Exception");
          } finally {
  
          }
      }
  
      private void addNewQuake(Quake _quake) {
          // add the new quake to our list of earthquake
          earthquakes.add(_quake);
  
          // Notify the array adapter of a change
          aa.notifyDataSetChanged();
  
      }
  
  }
  </code></pre>
  
  <p>Here is part of the LogCat, where error is showing:</p>
  
  <blockquote>
    <p>09-09 20:03:15.689: W/dalvikvm(1095): threadid=11: thread exiting with
    uncaught exception (group=0x40a71930) 09-09 20:03:15.735:
    E/AndroidRuntime(1095): FATAL EXCEPTION: Thread-111 09-09
    20:03:15.735: E/AndroidRuntime(1095): java.lang.NullPointerException
    09-09 20:03:15.735: E/AndroidRuntime(1095):   at
    com.satyaweblog.earthquake.EarthquakeListFragment.refreshEarthquakes(EarthquakeListFragment.java:106)
    09-09 20:03:15.735: E/AndroidRuntime(1095):   at
    com.satyaweblog.earthquake.EarthquakeListFragment$1.run(EarthquakeListFragment.java:50)
    09-09 20:03:15.735: E/AndroidRuntime(1095):   at
    java.lang.Thread.run(Thread.java:856) 09-09 20:03:20.015:
    I/Process(1095): Sending signal. PID: 1095 SIG: 9</p>
  </blockquote>
  
  <p>So, at one place, the line of code is:</p>
  
  <pre><code>String point = g.getFirstChild().getNodeValue();
  </code></pre>
  
  <p>In PHP, I would have checked this way:
  var_dump(g);
  How to check here in Android + Java?</p>
  
  <p>I think, 2nd error will be due to first!</p>