• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

null reference on array

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
started making a simple oil changing app but I've hit a wall with it. I'm not sure if it a fault in my logic or code. You add a car to the app with it's details these go into an array list and the car is displayed on a card view on the main screen showing the name, make, model, etc. When you click on a car from the main screen it opens another screen populated with the car and details with a list of existing oil changes and a button to add new oil change. When an oil change is added it creates another list containing details on the oil change including the name of the car. I have it so whichever car you pick from the main screen shows its own oil change history. The problem I can't figure out is, I preform a simple calculation to show when the next oil change is due based on miles I would like this value to be displayed on the main screen car card. I can pass a value to the card but the same value keeps going to every car I have listed. I can't figure out how to tie the mileage due value to the correct car.

adding a car

adding an oil change

Where I populate the oil change on cars card but it goes to all cars

Display car cards and open new activity when car clicked

i was given this advice on another site and i've tried to follow it but now when my app launches it crashes i get an null object reference
i'm pretty sure it's the oilChange array from this line but i don't know how to fix it.

1) Create an extra field in the Car model

private String changeOil;
//create setter and getter for changeOil
then obtain the current car to be updated and update the changeOil of the car while saving the oilChange in adding oil change

2) Pass the OilChange array list to the CarAdapter

ArrayList<OilChange> oliChanges;

public CarAdapter(Context context, int resource, ArrayList<Cars> cars, ArrayList<OilChange> oilChanges) {
   super(context, resource, cars);
    this.oilChanges = oilChanges;


Cars cars = getItem(position);
OilChange oilChange = oilChanges.get(position);
   if(cars != null){
       TextView txtName = convertView.findViewById(R.id.txtName);
       TextView txtMake = convertView.findViewById(R.id.txtMake);  //from car_list reads what's there (i think)
       TextView txtModel =convertView.findViewById(R.id.txtModel);
       TextView txtMiles = convertView.findViewById(R.id.txtMiles);
       TextView txtOilChangeDue = convertView.findViewById(R.id.txtOilChangeDue);
      // TextView txtOilChangeDueMiles = convertView.findViewById(R.id.txtOilChangeDueMileage);


//obtain the oilchange from the model ensure you have created a getOilChange() method in OilChange class to obtain the oilchange
       txtOilChangeDue.setText(oilChange.getOilChange());  //replaces value each time and on
    Bookmark Topic Watch Topic
  • New Topic