-2

i keep getting nullpointerexception no matter what i tried. can provide more code if required.

public int howManyBetweenTheseYears(int startYear, int endYear){
    ArrayList<Lamborghini> selectedCars = new ArrayList<Lamborghini>();

    for(Lamborghini c : inventory){
        if(c == null){
            continue;
        }
        if(c.getModelYear()>= startYear && c.getModelYear()<= endYear){
            selectedCars.add(c);
        }
    }
    return selectedCars.size();
}
daniyar
  • 9
  • 4

6 Answers6

0

It looks like inventory is null.

Jens
  • 67,715
  • 15
  • 98
  • 113
0

if getModelYear returns null then you will get NullPointerException

mokarakaya
  • 723
  • 1
  • 11
  • 22
0

2 Posibilities:--

  1. Mostly your inventory object reference is null

  2. your if code--

    if (c.getModelYear()>= startYear && c.getModelYear()<= endYear)

Try below if instead above --

if(((c.getModelYear()!=null)?c.getModelYear():0)>=startYear&&
        c.getModelYear()<= endYear)

I am Assusming you have some proper value in 'startYear' and 'endYear' apart from Zero.

Ashish Patil
  • 4,428
  • 1
  • 15
  • 36
0

if you can use java 8 the following code should work:

if(inventory==null){
        return 0;
}
return inventory.stream().filter((Lamborghini c)-> c!=null && c.getModelYear()>= startYear && c.getModelYear()<= endYear).count();

but without you telling us WHERE exactly the nullpointer is happening, it's just a shot in the dark.

griFlo
  • 2,084
  • 18
  • 28
0

You don't two ifs. because "for" loop will only continue for each c value.

public int howManyBetweenTheseYears(int startYear, int endYear)
{
    ArrayList<Lamborghini> selectedCars = new ArrayList<Lamborghini>();

    for(Lamborghini c : inventory)
    {       
        if(c.getModelYear()>= startYear && c.getModelYear()<= endYear)
        {
            selectedCars.add(c);
        }
    }
    return selectedCars.size();
 }

Also have a variable that holds the return value and initiate it by giving size 0. else it will give you nullpointerexception if can't find any matching car.

VD007
  • 267
  • 2
  • 15
0

sorry i couldn't reply to any of you, and I realized the code I left was not compete, but i finally figured out what was wrong, it doesn't make sense to me but it did fix the problem, and the problem wasn't even in the method i posted.

originally I had (ArrayList Lamborghini) when i was initializing the inventory arraylist:

public class LamborghiniCarLot
{
private String lotName;
private ArrayList<Lamborghini> inventory;

public LamborghiniCarLot()
{
 ArrayList<Lamborghini> inventory = new ArrayList<Lamborghini>();
}

public LamborghiniCarLot(String lotName){
  ArrayList<Lamborghini>inventory = new ArrayList<Lamborghini>();
    setLotName(lotName);

then I tried to remove the (ArrayList Lamborghini) before inventory and it fixed everything:

public LamborghiniCarLot()
{
    inventory = new ArrayList<Lamborghini>();
}

public LamborghiniCarLot(String lotName){
    inventory = new ArrayList<Lamborghini>();
    setLotName(lotName);

It would be appreciated if someone can explain to me what that changed and why it wouldn't work with arraylist lamborghini before inventory. I am new to programming so please be detailed.

daniyar
  • 9
  • 4