1

I am using an Aynctask to make an API call to build a chart of bitcoin values using blockchain.info API and the Android achartengine Library.

Through deugging I see that the API call is successful and the JSON data gets pulled, but for some reason it crashes and LogCat gives me the error "An error occurred while executing doInBackground. I know that you cannot access UI elements in doInBackground, but I was not trying to do that, so does anyone know what might be wrong here?

public class GraphActivity extends Activity {

private GraphicalView m_chart;

private XYMultipleSeriesDataset m_dataset = new XYMultipleSeriesDataset();

private XYMultipleSeriesRenderer m_renderer = new XYMultipleSeriesRenderer();

private XYSeries m_currentSeries;

private XYSeriesRenderer m_currentRenderer;

private void initChart()
{
    m_currentSeries = new XYSeries("Bitcoin Price");
    m_dataset.addSeries(m_currentSeries);
    m_currentRenderer = new XYSeriesRenderer();
    m_renderer.addSeriesRenderer(m_currentRenderer);
}

private void addBtcPriceData(xyCoords[] coords)
{
    int length = coords.length;
    for(int i = 0; i < length; ++i)
    {
        m_currentSeries.add(coords[i].x, coords[i].y);
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_graph);
}

@Override
protected void onResume()
{
    super.onResume();
    new RetrieveBTCChartTask().execute("https://blockchain.info/charts/market-price?format=json");
    /*LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
    if(m_chart == null)
    {
        initChart();
        addSampleData();
        m_chart = ChartFactory.getCubeLineChartView(this,m_dataset, m_renderer, 0.3f);
        layout.addView(m_chart);
    }
    else
    {
        m_chart.repaint();
    }*/
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.graph, menu);
    return true;
}

class RetrieveBTCChartTask extends AsyncTask<String, Void, xyCoords[]>
{
    @Override
    protected xyCoords[] doInBackground(String... urls)
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(urls[0]);

        httpget.setHeader("Content-type", "application/json");
        //httppost.setHeader("qty", "1");

        InputStream inputStream = null;
        String result = null;

        try
        {
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();

            inputStream = entity.getContent();

            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();

            String line = null;

            while ((line= reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            result = sb.toString();
        }
        catch (Exception e)
        {}
        finally
        {
            try
            {
                if(inputStream != null)
                {
                    inputStream.close();
                }
            }
            catch(Exception e)
            {}
        }

        xyCoords[] coords = new xyCoords[5000];;

        try
        {
            JSONObject btcPricesJSON = new JSONObject(result);
            JSONArray prices = btcPricesJSON.getJSONArray("values");
            if(prices != null)
            {
                int length = prices.length();
                for(int i = 0; i < length; ++i)
                {
                    JSONObject point = prices.getJSONObject(i);
                    coords[i].x = point.getDouble("x");
                    coords[i].y = point.getDouble("y");
                }
            }
        }
        catch(JSONException e)
        {}

        return coords;
    }

    @Override
    protected void onPostExecute(xyCoords[] coords)
    {   
        LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
        if(m_chart == null)
        {
            initChart();
            addBtcPriceData(coords);
            m_chart = ChartFactory.getCubeLineChartView(GraphActivity.this, m_dataset, m_renderer, 0.3f);
            layout.addView(m_chart);
        }
        else
        {
            m_chart.repaint();
        }
    }

}

class xyCoords
{
    public double x;
    public double y;
}

}

06-21 23:52:18.288: E/AndroidRuntime(26917): FATAL EXCEPTION: AsyncTask #2
06-21 23:52:18.288: E/AndroidRuntime(26917): java.lang.RuntimeException: An error occured while executing doInBackground()
06-21 23:52:18.288: E/AndroidRuntime(26917):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-21 23:52:18.288: E/AndroidRuntime(26917):    at  java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
06-21 23:52:18.288: E/AndroidRuntime(26917):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
06-21 23:52:18.288: E/AndroidRuntime(26917):    at   java.util.concurrent.FutureTask.run(FutureTask.java:239)
06-21 23:52:18.288: E/AndroidRuntime(26917):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-21 23:52:18.288: E/AndroidRuntime(26917):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-21 23:52:18.288: E/AndroidRuntime(26917):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-21 23:52:18.288: E/AndroidRuntime(26917):    at java.lang.Thread.run(Thread.java:841)
06-21 23:52:18.288: E/AndroidRuntime(26917): Caused by: java.lang.NullPointerException
06-21 23:52:18.288: E/AndroidRuntime(26917):    at info.lombardo.crypttrack.GraphActivity$RetrieveBTCChartTask.doInBackground(GraphActivity.java:149)
06-21 23:52:18.288: E/AndroidRuntime(26917):    at info.lombardo.crypttrack.GraphActivity$RetrieveBTCChartTask.doInBackground(GraphActivity.java:1)
06-21 23:52:18.288: E/AndroidRuntime(26917):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-21 23:52:18.288: E/AndroidRuntime(26917):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-21 23:52:18.288: E/AndroidRuntime(26917):    ... 4 more
Dave L
  • 41
  • 1
  • 3
  • Does logcat give you any more data on the exception? Post the full log; it should list the cause (what exception was actually thrown inside `doInBackground()`). – tophyr Jun 22 '14 at 04:04
  • Just posted it. @tophyr – Dave L Jun 22 '14 at 04:09
  • which is line `149` above? – Amulya Khare Jun 22 '14 at 05:01
  • You also have empty `catch` blocks which is **never** a good idea. It means that if you catch an exception then nothing will be done. You should, at the very least, be logging those exceptions so that you know when you have one. – codeMagic Jun 22 '14 at 05:24

1 Answers1

0

You can see in the logcat that the RuntimeException that's crashing your AsyncTask is caused by a NullPointerException on line 149 in GraphActivity.java. I can't tell what line that is from the pasted code, but that should be enough to help you find the problem.

tophyr
  • 1,658
  • 14
  • 20