0

I'm making an application in which an ArrayList is represented by two ListViews in both portrait and landscape orientations. To do this, I have been using a single custom adapter of which to inflate my listViews. But I keep receiving a NullPointer Exception whenever I instantiate the custom adapter for my landscape ListView. Any suggestions? Line which throws the exception:

listLand.setAdapter(customAdapter);

MainActivity

public class MainActivity extends AppCompatActivity {
    ListView listPort;
    ListView listLand;
    ArrayList<Flick> flicks;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listLand = findViewById(R.id.listLand);
        listPort = findViewById(R.id.listPort);

        flicks = new ArrayList<>();
        flicks.add(new Flick("Wizard of Oz", "Metro-Goldwyn-Mayer", "VARIOUS","1939", "", 98));
        flicks.add(new Flick("Star Wars", "LucasFilm", "George Lucas", "1977","",93));
        flicks.add(new Flick("Psycho","Paramount", "Alfred Hitchcock", "1960", "", 97));
        flicks.add(new Flick("King Kong", "RKO Pictures", "Merian C. Cooper & Ernest B. Schoedsack", "1933", "", 98));
        flicks.add(new Flick("2001: A Space Odyssey", "Metro-Goldwyn-Mayer", "Stanley Kubrick", "1968", "", 93));
        flicks.add(new Flick("Citizen Kane", "RKO Pictures", "Orson Welles", "1941", "", 100));
        flicks.add(new Flick("Snow White and the Seven Dwarfs", "Walt Disney Pictures", "VARIOUS", "1937", "",98));
        flicks.add(new Flick("Casablanca", "Warner Bros.", "Michael Curtiz", "1942", "", 97));
        flicks.add(new Flick("The Godfather", "Paramount", "Francis Ford Coppola", "1972", "", 98));
        flicks.add(new Flick("Jaws", "Universal", "Steven Spielberg", "1975", "", 97));

        CustomAdapter customAdapter = new CustomAdapter(this, R.layout.custom_layout, flicks);
        listPort.setAdapter(customAdapter);
        listLand.setAdapter(customAdapter);
    }
    public class CustomAdapter extends ArrayAdapter<Flick>
    {
        Context context;
        int resource;
        List<Flick> list;

        public CustomAdapter(@NonNull Context context, int resource, @NonNull List<Flick> objects) {
            super(context, resource, objects);
            this.context=context;
            this.resource=resource;
            list=objects;
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            LayoutInflater layoutInflater=(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            View adapterView=layoutInflater.inflate(resource,null);


            TextView title = adapterView.findViewById(R.id.title);
            ImageView imageView=adapterView.findViewById(R.id.imageView);
            Button up=adapterView.findViewById(R.id.upButton);
            Button down=adapterView.findViewById(R.id.downButton);

            title.setText(list.get(position).getTitle());

            switch(list.get(position).getStudio())
            {
                case "Warner Bros.":
                    imageView.setImageResource(R.drawable.warnerbros);
                    break;
                case "Paramount":
                    imageView.setImageResource(R.drawable.warnerbros);
                    break;
                case "Universal":
                    imageView.setImageResource(R.drawable.warnerbros);
                    break;
                case "LucasFilm":
                    imageView.setImageResource(R.drawable.warnerbros);
                    break;
                case "RKO Pictures":
                    imageView.setImageResource(R.drawable.warnerbros);
                    break;
                case "Walt Disney Pictures":
                    imageView.setImageResource(R.drawable.warnerbros);
                    break;
                case "Metro-Goldwyn-Mayer":
                    imageView.setImageResource(R.drawable.warnerbros);
                    break;
            }

            return adapterView;
        }
    }
}
  • 1
    Which line throws the `NullPointerException`? – Petter Hesselberg Dec 06 '18 at 16:48
  • The answer linked to as "duplicate" is not a duplicate, IMO. That question/answer concerns `NullPointerException`s in general, whereas this question is really about Android lifecycle events. I've voted to reopen the question. – Petter Hesselberg Dec 10 '18 at 08:07

1 Answers1

0

At a guess: You have two different layouts -- one for portrait and one for landscape. Only one of those will be loaded through findViewById, depending on the device orientation. Remember that the activity is recreated (and onCreate called) when you rotate the device. You should just check listPort and listLand for null before setting the adapter. You're not using both at the same time.

Something like this:

if (listPort != null) {
    listPort.setAdapter(customAdapter); // Portrait mode
} else {
    listLand.setAdapter(customAdapter); // Landscape mode
}

One of them should be non-null, provided everything else works as it should.

If, as I'm guessing, you have one values/activity_main.xml and one values-land/activity_main.xml, you could make life easier for yourself by using the same list id both places. E.g.,

public class MainActivity extends AppCompatActivity {
    ListView list;
    ArrayList<Flick> flicks;

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

        list = findViewById(R.id.list);
        flicks = new ArrayList<>();
        flicks.add(new Flick("Wizard of Oz", "Metro-Goldwyn-Mayer", "VARIOUS","1939", "", 98));
        ...
        flicks.add(new Flick("Jaws", "Universal", "Steven Spielberg", "1975", "", 97));

        CustomAdapter customAdapter = new CustomAdapter(this, R.layout.custom_layout, flicks);
        listt.setAdapter(customAdapter);
    }
    ...
}
Petter Hesselberg
  • 5,062
  • 2
  • 24
  • 42