-4

I cannot add any (or maybe just the first?) Objects to my Arraylist

A Bikestore is an Object which contains a name and an Arraylist of all it's bikes

bikes have 3 different attributes (2 Strings, 1 double)

Bikes are added to the Store within an "addbiketocollection()" method and within this method I use the .add function.

 public class Bikes 
    String brand ;
String color;
double price;

Bike(String brand, String color, double price){
    this.brand = brand;
    this.color = color;
    this.price = price;
}

public class Bikestore {

String name;
ArrayList<Bike> Collection = new ArrayList<>();

Bikestore (String name, ArrayList<Bike> Collection){
    this.name = name;
    this.Collection = Collection;
}


public void AddBikeToCollection (Bike NewBike) {
    Collection.add(NewBike);


}

  Mainclass
    Bike Bike1 = new Bike ("Cube", "Black", 400);

    Bikestore SellingBikes = new Bikestore ("SellingBikes", null);

    SellingBikes.AddBikeToCollection(Bike1);

}

when I try to add bikes to the bikestore I get a nullpointerxception Exception in thread "main" java.lang.NullPointerException

I have already googled my problem and watched some videos but none of these contained an arraylist with objects.

jcshine
  • 3
  • 1
  • 3
    [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Sudhir Ojha Jun 06 '19 at 11:20
  • 2
    Take care of java naming conventions. Variable and method names should start with lower case character – Jens Jun 06 '19 at 11:21
  • 3
    `new Bikestore ("SellingBikes", null);` <--- you should provide an arraylist here, not null. You changed `this.Collection` in the constructor. BTW : camelCase gives a more readable code and is the standard in variable names. – AxelH Jun 06 '19 at 11:22
  • I will apply your naming conventions, I actually don't program like that but I thought it would be easier to look through @AxelH how do I do this? How do I pass an arraylist? ArrayList Collection[]; Bikestore SellingBikes = new Bikestore ("SellingBikes", ArrayList Collection); this doesn't work How can I create the Object with a Arraylist which is empty for now and not use the "null" keyword? – jcshine Jun 06 '19 at 11:31
  • `ArrayList Collection[]` this is an array of `ArrayList`. Simply with `new Bikestore ("SellingBikes", new ArrayList())` or you remove the parameter in the constructor and keep the instance already instanciated during the declaration of `Collection` – AxelH Jun 06 '19 at 11:33
  • 1
    "*A Bikestore is an Object which contains a name and an Arraylist*"- no, it does not contain any `ArrayList` since in your code none is being created (missing `new ArrayList<>()`) – user85421 Jun 06 '19 at 11:42

3 Answers3

0

Problem is in Mainclass you are passing null for Bikestore constructor's collection

Bikestore SellingBikes = new Bikestore ("SellingBikes", null);

Either pass an ArrayList of Bike object or remove that argument completely. Since you are initializing an arrayList in BikeStore class passing another is redundent

public class Bikestore {

    String name;
    ArrayList<Bike> collection;

    Bikestore (String name){
         this.name = name;
         this.Collection = new ArrayList<>();
    }
}
lahiruk
  • 433
  • 3
  • 13
0

Your problem is this line of code

Bikestore SellingBikes = new Bikestore ("SellingBikes", null);

Inside a constructor you set the list of Bike to null, so even if you have initialized your list of Bike to a new ArrayList<>(), it does not matter

To fix this you should create the list of bikes first then pass to the Bikestore object

ArrayList<Bike> bikes = new ArrayList<>(); 
Bikestore SellingBikes = new Bikestore ("SellingBikes", bikes);

Or simple:


public void AddBikeToCollection (Bike NewBike) {
if(list == null) {
    list = new ArrayList<>(); 
}
    list.add(NewBike);
}

Anyway don't declare the name as a reserved keyword: Collection

Son Nguyen
  • 13
  • 7
0

It appears that when you create your BikeStore you passing in null instead of an ArrayList. So what you can do is either change the line to:

 Bikestore SellingBikes = new Bikestore ("SellingBikes", this.Collection);

Or in the BikeStore constructor make it just

Bikestore (String name){
    this.name = name;
}

and the when creating your bike store: Bikestore SellingBikes = new Bikestore ("SellingBikes");

jwanger
  • 74
  • 7