Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Qasim Albaqali
Courses Plus Student 259 PointsBuilding a bloag reader - Parsing Data JSON
So I ran into problems and I saw that there are fixes for it which is in https://teamtreehouse.com/forum/trying-to-retrieve-data-from-my-own-blog-help
I used Hubert Maraszek fix which was
int nextCharacter; // read() returns an int, we cast it to char later
String responseData = "";
while(true){ // Infinite loop, can only be stopped by a "break" statement
nextCharacter = reader.read(); // read() without parameters returns one character
if(nextCharacter == -1) // A return value of -1 means that we reached the end
break;
responseData += (char) nextCharacter; // The += operator appends the character to the end of the string
}
but then in the next video Parsing Data Returned in JSON Format when we have to add
JSONArray jsonPosts = jsonResponse.getJSONArray("posts");
for (int i = 0; i < jsonPosts.length(); i++) {
JSONObject jsonPost = jsonPosts.getJSONObject(i);
String title = jsonPost.getString("title");
Log.v(TAG, "Posts" + i + ":" + title);
I get the following errors in my Logcat
11-10 02:57:29.774 1688-1688/qax.khabaralyoum E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: qax.khabaralyoum, PID: 1688
java.lang.NullPointerException
at qax.khabaralyoum.MainListActivity.handleBlogResponse(MainListActivity.java:84)
at qax.khabaralyoum.MainListActivity$GetBlogPostsTask.onPostExecute(MainListActivity.java:183)
at qax.khabaralyoum.MainListActivity$GetBlogPostsTask.onPostExecute(MainListActivity.java:134)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Here is my MainActivity.java
package qax.khabaralyoum;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.ListActivity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;
public class MainListActivity extends ListActivity {
protected String[] mBlogPostTitles;
public static final int NUMBER_OF_POSTS = 20;
public static final String TAG = MainListActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_list);
if (isNetworkAvailable()) {
GetBlogPostsTask getBlogPostsTask = new GetBlogPostsTask();
getBlogPostsTask.execute();
}
else {
Toast.makeText(this, "Network is unavailable!", Toast.LENGTH_LONG).show();
}
//Toast.makeText(this, getString(R.string.no_items), Toast.LENGTH_LONG).show();
}
private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()) {
isAvailable = true;
}
return isAvailable;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_list, menu);
return true;
}
private class GetBlogPostsTask extends AsyncTask<Object, Void, String> {
@Override
protected String doInBackground(Object... arg0) {
int responseCode = -1;
try {
URL blogFeedUrl = new URL("http://esouqbh.com/khabar/?json=get_recent_posts" + NUMBER_OF_POSTS);
HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection();
connection.connect();
responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
Reader reader = new InputStreamReader(inputStream);
int nextCharacter;
String responseData = "";
while (true) {
nextCharacter = reader.read();
if (nextCharacter == -1)
break;
responseData += (char) nextCharacter;
JSONObject jsonResponse = new JSONObject(responseData);
String status = jsonResponse.getString("status");
Log.v(TAG, status);
JSONArray jsonPosts = jsonResponse.getJSONArray("posts");
for (int i = 0; i < jsonPosts.length(); i++) {
JSONObject jsonPost = jsonPosts.getJSONObject(i);
String title = jsonPost.getString("title");
Log.v(TAG, "Post " + i + ": " + title);
}
}
}
else {
Log.i(TAG, "Unsuccessful HTTP Response Code: " + responseCode);
}
}
catch (MalformedURLException e) {
Log.e(TAG, "Exception caught: ", e);
}
catch (IOException e) {
Log.e(TAG, "Exception caught: ", e);
}
catch (Exception e) {
Log.e(TAG, "Exception caught: ", e);
}
return "Code: " + responseCode;
}
}
}
1 Answer
Ben Jakuben
Treehouse TeacherThe logcat errors show that it's trying to run the onPostExecute() method of your AsyncTask, but I don't see that in your code. Is this the right version of MainListActivity?
java.lang.NullPointerException
at qax.khabaralyoum.MainListActivity.handleBlogResponse(MainListActivity.java:84)
at qax.khabaralyoum.MainListActivity$GetBlogPostsTask.onPostExecute(MainListActivity.java:183)
at qax.khabaralyoum.MainListActivity$GetBlogPostsTask.onPostExecute(MainListActivity.java:134)
David Hope
19,876 PointsDavid Hope
19,876 PointsSorry to piggyback on your question Qasim, but I'm experiencing an similar issue and need some help with it as well.
https://teamtreehouse.com/forum/no-value-found-for-json-array