Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Mobile Development > BOOK: Professional Android 4 Application Development
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional Android 4 Application Development
This is the forum to discuss the Wrox book Professional Android 4 Application Development Reto Meier; ISBN: 978-1-1181-0227-5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Android 4 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #11 (permalink)  
Old December 1st, 2013, 11:08 PM
Authorized User
Points: 48, Level: 1
Points: 48, Level: 1 Points: 48, Level: 1 Points: 48, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm 99% sure that reinstalling software isn't going to fix this. Could you post what is on line 95? And I assume this is the code in chapter 6?
Reply With Quote
  #12 (permalink)  
Old December 8th, 2013, 06:21 PM
Registered User
Points: 15, Level: 1
Points: 15, Level: 1 Points: 15, Level: 1 Points: 15, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2013
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by uler3161 View Post
I'm 99% sure that reinstalling software isn't going to fix this. Could you post what is on line 95? And I assume this is the code in chapter 6?
Hi,
sorry for the delayed answer. I was waiting for my Android phone to do some more testing and to see if it was something on my end. But I still get the same error message (other examples I have tried work, though).

Here are the error lines:
Code:
47: refreshEarthquakes();   // inside the Thread
55: private void refreshEarthquakes() {
95: String point = g.getFirstChild().getNodeValue();
Reply With Quote
  #13 (permalink)  
Old December 8th, 2013, 06:34 PM
Authorized User
Points: 48, Level: 1
Points: 48, Level: 1 Points: 48, Level: 1 Points: 48, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I haven't looked at the data feed, but my gut feeling says that it has changed. I'm looking at page 208 of the book and see that g is a georss:point element within an entry element. And apparently either g is null or g.getFirstChild() returns null. If it were me, I'd try to view the document returned by that url to see if there are any georss:point elements within an entry element. And if so, see if there is a child within the georss:point element.
Reply With Quote
  #14 (permalink)  
Old December 9th, 2013, 04:03 PM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default NullPointerException

Hello!
I've got a problem with the "earthquake example"-code. When I am debugging the source code, the debugger throws a NullPointerException. I hope that you can help me.

I've marked the critical lines for you. Thanks!

That's the code:

Code:
package com.example.earthquake;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.annotation.SuppressLint;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;

import android.util.Log;
import android.widget.ArrayAdapter;

@SuppressLint("SimpleDateFormat")
public class EarthquakeListFragment extends android.app.ListFragment {

ArrayAdapter<Quake> aa;
ArrayList<Quake> earthquakes = new ArrayList<Quake>();

private static final String TAG = "EARTHQUAKE";
private Handler handler = new Handler();

public void refreshEarthquakes() {
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();

	Document dom = db.parse(in);
	Element docEle = dom.getDocumentElement();

	earthquakes.clear();

	NodeList nl = docEle.getElementsByTagName("entry");
	if (nl != null && nl.getLength() > 0) {
		for (int i = 0; i < nl.getLength(); i++) {
			Element entry = (Element) nl.item(i);
			Element title = (Element) entry.getElementsByTagName("title").item(0);
			Element g = (Element) entry.getElementsByTagName("georrs: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);
			
			handler.post(new Runnable() {
				public void run() {
					addNewQuake(quake);
					}
				});

			}
		}
	}
} catch (MalformedURLException e) {
	Log.d(TAG, "MalformedURLException");
} catch (IOException e) {
	Log.d(TAG, "IOException");
} catch (ParserConfigurationException e) {
	Log.d(TAG, "Parser Configuration Exception");
} catch (SAXException e) {
	Log.d(TAG, "SAX Exception");
} finally {
}
}

	private void addNewQuake(Quake _quake) {
		earthquakes.add(_quake);
		aa.notifyDataSetChanged();
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		int layoutID = android.R.layout.simple_list_item_1;
		aa = new ArrayAdapter<Quake>(getActivity(), layoutID, earthquakes);
		setListAdapter(aa);

		Thread t = new Thread(new Runnable() {
			public void run() {
				refreshEarthquakes();
			}
		});
		t.start();
	}
}
Reply With Quote
  #15 (permalink)  
Old December 9th, 2013, 04:15 PM
Authorized User
Points: 48, Level: 1
Points: 48, Level: 1 Points: 48, Level: 1 Points: 48, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by r6cer View Post
Hello!
I've got a problem with the "earthquake example"-code. When I am debugging the source code, the debugger throws a NullPointerException. I hope that you can help me.

I've marked the critical lines for you. Thanks!
Same issue that Markstar is having. I looked at the xml manually and it looked ok, so I'm not sure what is wrong. I would suggest dumping the contents retrieved from the stream into the log and see what it looks like.
Reply With Quote
  #16 (permalink)  
Old December 9th, 2013, 05:17 PM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2013
Posts: 1
Thanks: 0
Thanked 4 Times in 1 Post
Default

The problem is that the weblink of the Earthquake Hazards Program refers to a deprecated representation. For the example this is no problem, but the first element in the xml file notifies the user that the xml-file is deprecated. This element cannot be parsed correctly with the result, that a Null-Pointer exception is thrown. You can solve this problem by starting the loop at i=1. Here the correct code for the EarthquakeListFragment.java:

Code:
package com.paad.earthquake;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.app.ListFragment;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ArrayAdapter;

public class EarthquakeListFragment extends ListFragment {

	ArrayAdapter<Quake> aa;
	ArrayList<Quake> earthquakes = new ArrayList<Quake>();

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		int layoutID = android.R.layout.simple_list_item_1;
		aa = new ArrayAdapter<Quake>(getActivity(), layoutID, earthquakes);
		setListAdapter(aa);

		Thread t = new Thread(new Runnable() {
			public void run() {
				refreshEarthquakes();
			}
		});
		t.start();
	}

	private static final String TAG = "EARTHQUAKE";
	private Handler handler = new Handler();

	private 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 earthquakes
				earthquakes.clear();

				// Get a list of each earthquake entry.
				NodeList nl = docEle.getElementsByTagName("entry");
				if (nl != null && nl.getLength() > 0) {
					for (int i = 1; i < nl.getLength(); i++) {
						Element entry = (Element) nl.item(i);
						Element title = (Element) entry.getElementsByTagName(
								"title").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 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);
						}

						Location l = new Location("dummyGPS");

						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 (MalformedURLException e) {
			Log.d(TAG, "MalformedURLException", e);
		} catch (IOException e) {
			Log.d(TAG, "IOException", e);
		} catch (ParserConfigurationException e) {
			Log.d(TAG, "Parser Configuration Exception", e);
		} catch (SAXException e) {
			Log.d(TAG, "SAX Exception", e);
		} finally {
		}
	}

	private void addNewQuake(Quake _quake) {
		// Add the new quake to our list of earthquakes.
		earthquakes.add(_quake);

		// Notify the array adapter of a change.
		aa.notifyDataSetChanged();
	}

}
Reply With Quote
The Following 4 Users Say Thank You to BretAntilles For This Useful Post:
Kees (December 11th, 2013), LeBoeuf (January 16th, 2014), Markstar (December 18th, 2013), wolfenr (November 13th, 2014)
  #17 (permalink)  
Old December 18th, 2013, 07:54 AM
Registered User
Points: 15, Level: 1
Points: 15, Level: 1 Points: 15, Level: 1 Points: 15, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2013
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thank you, that works!

So the Handler is there to manage the thread?

Also, I noticed that you removed the geolocation. Any particular reason for that (other than that is not used in this example)?

Last edited by Markstar; December 18th, 2013 at 07:58 AM..
Reply With Quote
  #18 (permalink)  
Old January 16th, 2014, 05:19 AM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2014
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thank you for the solution. Now it works fine.
Reply With Quote
  #19 (permalink)  
Old June 3rd, 2014, 02:42 PM
Registered User
Points: 11, Level: 1
Points: 11, Level: 1 Points: 11, Level: 1 Points: 11, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Removed georss:point

Quote:
Originally Posted by BretAntilles View Post
The problem is that the weblink of the Earthquake Hazards Program refers to a deprecated representation. For the example this is no problem, but the first element in the xml file notifies the user that the xml-file is deprecated. This element cannot be parsed correctly with the result, that a Null-Pointer exception is thrown. You can solve this problem by starting the loop at i=1. Here the correct code for the EarthquakeListFragment.java:

Code:
package com.paad.earthquake;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.app.ListFragment;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ArrayAdapter;

public class EarthquakeListFragment extends ListFragment {

	ArrayAdapter<Quake> aa;
	ArrayList<Quake> earthquakes = new ArrayList<Quake>();

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		int layoutID = android.R.layout.simple_list_item_1;
		aa = new ArrayAdapter<Quake>(getActivity(), layoutID, earthquakes);
		setListAdapter(aa);

		Thread t = new Thread(new Runnable() {
			public void run() {
				refreshEarthquakes();
			}
		});
		t.start();
	}

	private static final String TAG = "EARTHQUAKE";
	private Handler handler = new Handler();

	private 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 earthquakes
				earthquakes.clear();

				// Get a list of each earthquake entry.
				NodeList nl = docEle.getElementsByTagName("entry");
				if (nl != null && nl.getLength() > 0) {
					for (int i = 1; i < nl.getLength(); i++) {
						Element entry = (Element) nl.item(i);
						Element title = (Element) entry.getElementsByTagName(
								"title").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 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);
						}

						Location l = new Location("dummyGPS");

						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 (MalformedURLException e) {
			Log.d(TAG, "MalformedURLException", e);
		} catch (IOException e) {
			Log.d(TAG, "IOException", e);
		} catch (ParserConfigurationException e) {
			Log.d(TAG, "Parser Configuration Exception", e);
		} catch (SAXException e) {
			Log.d(TAG, "SAX Exception", e);
		} finally {
		}
	}

	private void addNewQuake(Quake _quake) {
		// Add the new quake to our list of earthquakes.
		earthquakes.add(_quake);

		// Notify the array adapter of a change.
		aa.notifyDataSetChanged();
	}

}
Seems you have removed : georss:point

There isnt any way to do it by keeping that code itself...
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Earthquake Chapter 6 newbie123 BOOK: Professional Android 4 Application Development 7 January 21st, 2013 07:09 AM
Chapter 7 Earthquake 2, Earthquake.java, error onActivityResult dbomberg BOOK: Professional Android 2 Application Development 2 November 30th, 2012 05:52 AM
Chapter 9 Earthquake 1 MellowFellow BOOK: Professional Android 2 Application Development 14 August 23rd, 2010 04:40 PM
Please help: Chapter 9 Earthquake 4 dahe BOOK: Professional Android 2 Application Development 0 July 21st, 2010 03:41 PM
Chap. 9 Earthquake app does not work. pgmr26 BOOK: Professional Android 2 Application Development 1 March 27th, 2010 08:14 AM



All times are GMT -4. The time now is 12:03 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.