How to Handle NPE
<p>In my application I have to fill the views using web service responses. </p>

<p>I am getting the correct web service response, but at the time of filling the views using the response I get an NPE when running on a device. However, when run in the emulator it's working properly.</p>

<p>I cannot find the cause of the NPE, below is my code, can anyone help?</p>

<p>The exception is raised in the <code>getview()</code> method at <code>r2(holder.r2.setRating(ksrating_value_w_onedecimal);)</code>.</p>

<p>The stack trace:</p>

<pre><code>07-20 17:21:21.357: ERROR/AndroidRuntime(4144): java.lang.NullPointerException

07-20 17:21:21.357: ERROR/AndroidRuntime(4144):     at com.ibkr.roadbrake.RB_StaticSearchResults$StaticSearchResultsListViewAdapter.getView(RB_StaticSearchResults.java:810)

07-20 17:21:21.357: ERROR/AndroidRuntime(4144):     at android.widget.AbsListView.obtainView(AbsListView.java:1385)

07-20 17:21:21.357: ERROR/AndroidRuntime(4144):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1213)

07-20 17:21:21.357: ERROR/AndroidRuntime(4144):     at android.widget.ListView.onMeasure(ListView.java:1126)

07-20 17:21:21.357: ERROR/AndroidRuntime(4144):     at android.view.View.measure(View.java:8020)
07-20 17:21:21.357: ERROR/AndroidRuntime(4144):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
</code></pre>

<p>And the java file:</p>

<pre><code>   public View getView(final int position, View convertView, ViewGroup parent) 
  {           
        class ViewHolder 
        {                
            RatingBar r1,r2,r3;
            TextView ratingpts, ratingpks, ratingprr;               
        }
        ViewHolder holder;

        //if (convertView == null) {
            convertView = mInflater.inflate(R.layout.stsearchresults_eachitem, null);

            // Creates a ViewHolder and store references to the two children views
            // we want to bind data to.
            holder = new ViewHolder();             
            holder.r1 = (RatingBar)convertView.findViewById(R.id.small_ratingbar);
            holder.ratingpts = (TextView)convertView.findViewById(R.id.ratingpts);                
            holder.r2 = (RatingBar)convertView.findViewById(R.id.kssmall_ratingbar);
            holder.ratingpks = (TextView)convertView.findViewById(R.id.ratingpks);
            holder.r3 = (RatingBar)convertView.findViewById(R.id.rrsmall_ratingbar);
            holder.ratingprr = (TextView)convertView.findViewById(R.id.ratingprr);


            /*convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }     */     

        float rating = RB_Constant.filterSearchResultObj.response.get(position).Rating;
        float rating_value = Math.round(rating * 10);           
    float rating_value_w_onedecimal = rating_value / 10;            

        try
        {
            holder.r1.setRating(rating_value_w_onedecimal);
        }
        catch(Exception e){
            System.out.println("Error-&gt;"+e.toString()+" position "+position);
        }            
        holder.ratingpts.setText(""+rating_value_w_onedecimal);     

        float ksrating = RB_Constant.filterSearchResultObj.response.get(position).KidsRating;
        float ksrating_value = Math.round(ksrating * 10);           
        float ksrating_value_w_onedecimal = ksrating_value / 10;
        try{
            holder.r2.setRating(ksrating_value_w_onedecimal);
        }
        catch(Exception e){
            System.out.println("Error-&gt;"+e.toString()+" position "+position);
        }            
        holder.ratingpks.setText(""+ksrating_value_w_onedecimal);  

        float rrrating = RB_Constant.filterSearchResultObj.response.get(position).RestRoomRating;
        float rrrating_value = Math.round(rrrating * 10);           
        float rrrating_value_w_onedecimal = rrrating_value / 10;
        try{
            holder.r3.setRating(rrrating_value_w_onedecimal);
        }
        catch(Exception e){
            System.out.println("Error-&gt;"+e.toString()+" position "+position);
        }            
        holder.ratingprr.setText(""+rrrating_value_w_onedecimal);  
      ------------
      ------------
      ------------
   }
</code></pre>
