-1

I am a beginner in android development. I am working on an android app that has a 'verse of the day' feature. I have been trying for a while to parse JSON object from URL to appear in Alert Dialog when the item is selected in my options menu but up till now, i am yet to make any success. Here is my Code:

private TextView textView;


     public boolean onOptionsItemSelected(MenuItem item) {
                    LayoutInflater inflater = (LayoutInflater) 
      getSystemService(Context.LAYOUT_INFLATER_SERVICE);


         View verseofTheDay = inflater.inflate(R.layout.my_verse, null);
                switch (item.getItemId())

     case R.id.bible:
                    AlertDialog.Builder verseofday = new AlertDialog.Builder(this);
                    verseofday.setCancelable(true);
                    verseofday.setView(verseofTheDay);
                    textView = (TextView) findViewById(R.id.verseDay);
                    verseofday.show();
                    new JSONTask().execute("http://app.wordedfm.com/api/scripture");
                    break;
            }
            return super.onOptionsItemSelected(item);
        }

    And here is my JSON class:
    public class JSONTask extends AsyncTask<String, String, String>{

            @Override
            protected String doInBackground(String... params) {
                HttpURLConnection connection = null;
                BufferedReader reader = null;
                try {
                    URL url = new URL(params[0]);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.connect();
                    InputStream stream  = connection.getInputStream();
                    reader = new BufferedReader(new InputStreamReader(stream));
                    StringBuffer buffer= new StringBuffer();
                    String line = "";
                    while ((line = reader.readLine()) != null) {
                        buffer.append(line);
                    }
                    return buffer.toString();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (connection != null) {
                        connection.disconnect();
                    }
                    try {
                        if (reader != null){
                            reader.close();
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return null;
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                textView.setText(result);
            }
        }
    }

But everytime i get this error:

.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
06-02 05:08:46.983 16307-16307/com.musicianfocus.ben.wordedfm E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                java.lang.NullPointerException
                                                                                    at com.musicianfocus.ben.wordedfm.MainActivity$JSONTask.onPostExecute(MainActivity.java:209)
                                                                                    at com.musicianfocus.ben.wordedfm.MainActivity$JSONTask.onPostExecute(MainActivity.java:199)
                                                                                    at android.os.AsyncTask.finish(AsyncTask.java:631)
                                                                                    at android.os.AsyncTask.access$600(AsyncTask.java:177)
                                                                                    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                    at android.os.Looper.loop(Looper.java:137)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                                                    at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                    at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                                    at dalvik.system.NativeStart.main(Native Method)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ben Ajax
  • 668
  • 2
  • 13
  • 27

1 Answers1

0

The string returned in the onPost Method is null. You need to store the result from JSON in different variable. And use the method toString().

Hope this helps.

Sarthak Gandhi
  • 2,130
  • 1
  • 16
  • 23