Exception java.lang.NullPointerException thrown when working with android listviews
<p>I'm still quite new to Android and Java. Was learning about Android Listviews and did so by adapting code from this tutorial: <a href="http://w2davids.wordpress.com/android-listview-with-iconsimages-and-sharks-with-lasers/" rel="nofollow">http://w2davids.wordpress.com/android-listview-with-iconsimages-and-sharks-with-lasers/</a></p>

<p>However, I came into this problem where the java.lang.NullPointerException was thrown when doing <code>stockName.setText(stock.name)</code>.</p>

<p>Here are my codes:</p>

<p><strong>Stocks.java</strong></p>

<pre><code>package sg.jon.stocks;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Stocks extends Activity {

    static final String STOCK_API_URL = "&lt;removed&gt;";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new UpdateStockData().execute();
    }

    private class UpdateStockData extends AsyncTask&lt;Void, Integer, JSONObject&gt;{

        protected JSONObject doInBackground(Void... params) {
            Log.v("stocks", "Starting HTTP request");
            HttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(STOCK_API_URL);
            String response = null;
            try {
                ResponseHandler&lt;String&gt; responseHandler=new BasicResponseHandler();
                response = httpclient.execute(httpget, responseHandler);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.v("stocks", "Finished HTTP request");
            Log.v("stocks", "Starting to parse JSON");
            JSONObject jsonResult = null;
            try {
                jsonResult = new JSONObject(response.replace("{}&amp;&amp; ",""));
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.v("stocks", "JSON parsing complete");
            return jsonResult;
        }

        protected void onPostExecute(JSONObject jsonResult){
            String[] stockNames = null;
            String[] stockPrices = null;
            List&lt;Stock&gt; stocks = null;
            String label = null;
            try {
                // label =&gt; As at dd-mm-yyyy h:mm AM/PM
                label = jsonResult.getString("label");
                JSONArray stocksJSONArray = jsonResult.getJSONArray("items");
                stockNames = new String[stocksJSONArray.length()];
                stockPrices = new String[stocksJSONArray.length()];
                stocks = new ArrayList&lt;Stock&gt;();
                for(int i=0; i&lt;stocksJSONArray.length(); i++){
                    Stock stock = new Stock();
                    stock.name = stocksJSONArray.getJSONObject(i).getString("N");
                    stock.price = stocksJSONArray.getJSONObject(i).getString("LT");
                    stocks.add(stock);
                    stockNames[i] = stocksJSONArray.getJSONObject(i).getString("N");
                    stockPrices[i] = stocksJSONArray.getJSONObject(i).getString("LT");

                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Toast.makeText(Stocks.this, label, Toast.LENGTH_LONG).show();
            ListView stocksListView = (ListView) findViewById(R.id.stocksListView);

            stocksListView.setAdapter(new StockArrayAdapter(
                    getApplicationContext(), R.layout.list_row, stocks)
);

        }


    }

    public class Stock
    {
        public String name;
        public String price;

        public Stock()
            {
                // TODO Auto-generated constructor stub
            }

        public Stock(String name, String price)
            {
                this.name = name;
                this.price = price;
            }

        @Override
        public String toString()
            {
                return this.name;
            }
    }



    public class StockArrayAdapter extends ArrayAdapter&lt;Stock&gt; {
        private Context context;
        private TextView stockName;
        private TextView stockPrice;
        private List&lt;Stock&gt; stocks = new ArrayList&lt;Stock&gt;();

        public StockArrayAdapter(Context context, int textViewResourceId,
                List&lt;Stock&gt; objects) {
            super(context, textViewResourceId, objects);
            this.context = context;
            this.stocks = objects;
        }

        public int getCount() {
            return this.stocks.size();
        }

        public Stock getItem(int index) {
            return this.stocks.get(index);
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            if (row == null) {
                // ROW INFLATION
                Log.d("xml", "Starting XML Row Inflation ... ");
                LayoutInflater inflater = (LayoutInflater) this.getContext()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.list_item, parent, false);
                Log.d("xml", "Successfully completed XML Row Inflation!");
            }

            // Get item
            Stock stock = getItem(position);
            Log.v("stk",stock.name);
            Log.v("stk",stock.price);

            stockName = (TextView) row.findViewById(R.id.stockName);
            stockPrice = (TextView) row.findViewById(R.id.stockPrice);


            stockName.setText(stock.name);
            stockPrice.setText(stock.price);

            return row;
        }
    }

}
</code></pre>

<p><strong>Main.xml</strong></p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" &gt;

    &lt;ListView
        android:id="@+id/stocksListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" &gt;
    &lt;/ListView&gt;

&lt;/LinearLayout&gt;
</code></pre>

<p><strong>list_item.xml</strong></p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp"
    android:textColor="#fff"&gt;
&lt;/TextView&gt;
</code></pre>

<p><strong>list_row.xml</strong></p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"&gt;

    &lt;TextView
        android:id="@+id/stockName"
        android:paddingLeft="10dip"
        android:layout_weight="0.5"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" /&gt;
    &lt;TextView
        android:id="@+id/stockPrice"
        android:layout_gravity="right"
        android:paddingRight="10dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" /&gt;

&lt;/LinearLayout&gt;
</code></pre>

<p>This is the error shown in the logs:</p>

<pre><code>04-30 12:07:16.835: W/dalvikvm(1657): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-30 12:07:16.883: E/AndroidRuntime(1657): FATAL EXCEPTION: main
04-30 12:07:16.883: E/AndroidRuntime(1657): java.lang.NullPointerException
04-30 12:07:16.883: E/AndroidRuntime(1657):     at sg.jon.stocks.Stocks$StockArrayAdapter.getView(Stocks.java:191)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at android.widget.AbsListView.obtainView(AbsListView.java:2033)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at android.widget.ListView.onMeasure(ListView.java:1155)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at android.view.View.measure(View.java:12723)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at android.view.View.measure(View.java:12723)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at android.view.View.measure(View.java:12723)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at android.view.View.measure(View.java:12723)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at android.view.View.measure(View.java:12723)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at android.os.Looper.loop(Looper.java:137)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at android.app.ActivityThread.main(ActivityThread.java:4424)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at java.lang.reflect.Method.invokeNative(Native Method)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at java.lang.reflect.Method.invoke(Method.java:511)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-30 12:07:16.883: E/AndroidRuntime(1657):     at dalvik.system.NativeStart.main(Native Method)
</code></pre>

<p>Thanks (:</p>
