0

In logcat i'm getting this information: "java.lang.NullPointerException: Attempt to invoke interface method 'int

java.util.List.size()' on a null object reference"
package com.example.kuba.seriale;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;
import com.example.kuba.seriale.models.MovieModel;
import java.util.List;
public class MainActivity extends AppCompatActivity {
    SearchView src;
    Button btn;
    private TextView txv;
    ListView lvm;
    String api_key = "***************";
    JSONTasks json = new JSONTasks();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        src = (SearchView) findViewById(R.id.searchView);
        txv = (TextView) findViewById(R.id.textView);
        btn = (Button) findViewById(R.id.button);
        lvm = (ListView)findViewById(R.id.lvMovies);



        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String zapytanie = src.getQuery().toString();
                if((zapytanie==null)||(zapytanie=="")){
                    zapytanie = "Homeland";
                }
                String Link = "http://api.themoviedb.org/3/search/tv?query="+zapytanie+"&api_key="+api_key;

                MovieAdapter movieAdapter = new MovieAdapter(getApplicationContext(), R.layout.row, json.mvc(Link));
                try{
                    lvm.setAdapter(movieAdapter);
                } catch (Exception e){
                    txv.setText(e.getMessage());
                }



            }
        });
        src.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {

                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {


                return false;
            }
        });
    }
    public void Search(){
        String zapytanie = src.getQuery().toString();
        if((zapytanie==null)||(zapytanie=="")){
            zapytanie = "Homeland";
        }
        String Link = "http://api.themoviedb.org/3/search/tv?query="+zapytanie+"&api_key="+api_key;
        MovieAdapter movieAdapter = new MovieAdapter(getApplicationContext(), R.layout.row, json.mvc(Link));
        lvm.setAdapter(movieAdapter);
    }

}
class MovieAdapter extends ArrayAdapter{

    private List<MovieModel> movieModelList;
    private int resource;
    private LayoutInflater inflater;
    LinearLayout linear;
    Button btn;

    public MovieAdapter(Context context, int resource, List<MovieModel> objects) {
        super(context, resource, objects);
        movieModelList = objects;
        this.resource = resource;

        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView==null){
            convertView = inflater.inflate(R.layout.row,null);
        }
        TextView txv;


        txv = (TextView)convertView.findViewById(R.id.textView2);
        btn = (Button)convertView.findViewById(R.id.button2);
        linear = (LinearLayout)convertView.findViewById(R.id.linear);

        txv.setText(movieModelList.get(position).getName());
        int id = movieModelList.get(position).getId();

        return convertView;
    }
    public void click(String id){
        btn.setText(id);
    }
}
4
  • It looks like you are trying to add objects into the adapter from a network source? In that case, you need to do it with some sort of callback. Otherwise the list will still be null in the main thread. Commented Jan 10, 2016 at 8:52
  • Possible duplicate of Strange nullpointer exception error when i use a List Commented Jan 10, 2016 at 9:01
  • Ok this thread helped i'm not getting error but i don't see any effect, the ListView is empty. Data is downloaded (i checked in network monitor) but list items does not appear. Commented Jan 10, 2016 at 9:49
  • Problem solved, thanks for help :D Commented Jan 10, 2016 at 12:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.