-1

I have a problem with a NullPointerException at a part of my code. But I can't find or don't see the solution for this problem.

Here is my coding:

public class Beurs

private HashMap<String, Aandeel> lijstAandelen;
private HashSet<String> lijstKeys;

/**
 * Maak een constructor van de klasse Beurs
 */
public void Beurs()
{
    lijstAandelen = new HashMap<>();
    lijstKeys = new HashSet<>();
}


public void addAandeel(Aandeel aandeel)
{
    if(aandeel != null){
      String code = aandeel.getCode();
      lijstAandelen.put(code, aandeel);
      lijstKeys.add(aandeel.getCode());
    }
}

aandeel contains an object and code also contains a valid String. So the NullPointerException is thrown at : lijstAandelen.put(code, aandeel);

Probably I'm looking over it so I hope you guys can help me out.

Aandeel coding:

public class Aandeel


private String code;
private String naam;
private double dividend;
private double[] waarden;

public Aandeel(String code, String naam, double dividend, double[] tab)
{
    this.code = code;
    this.naam = naam;
    setDividend(dividend);
    waarden = new double[12];
    setWaarden(tab);
}

public String getCode()
{
    return code;
}

nullpointerexception: java.lang.NullPointerException at Beurs.addAandeel(Beurs.java:35) at Hoofd.main(Hoofd.java:31)

beurs.addAandeel(aandeelTabel[i]);


public class Hoofd


public static void main(String[ ] args)
{
   // maak de nodige objecten aan
   Beurs beurs = new Beurs();
   Portefeuille pf = new Portefeuille("Tom Richmann",4,beurs);
   Aandeel[] aandeelTabel = new Aandeel[5];

   double[ ] wAgs = {2.0,1.8,1.7,1.6,1.2,1.3,1.2,1.4,1.3,1.6,1.7,1.6};
   double[ ] wBekb = {80,71,62,50,48,49,47,37,32,30,28,24};
   double[ ] wColr = {40,42,34,35,34,37,30,32,28,30,33,32};
   double[ ] wKbc = {28,27,25,23,21,18,14,12,9,13,15,14};
   double[ ] wUcb = {32,33,31,30,28,32,33,30,31,34,26,40};

   aandeelTabel[0] = new Aandeel("AGS","Ageas",0.2, wAgs);
   aandeelTabel[1] = new Aandeel("BEKB","Bekaert",25,wBekb);
   aandeelTabel[2] = new Aandeel("COLR","Colruyt",10,wColr);
   aandeelTabel[3] = new Aandeel("KBC","KBC",2,wKbc);
   aandeelTabel[4] = new Aandeel("UCB","UCB",0.2,wUcb);

   // voeg de aandelen toe aan het beurs object.
   for(int i=0; i<aandeelTabel.length; i++){ 
       beurs.addAandeel(aandeelTabel[i]);
    }
   // geef een performance lijst
   System.out.println(beurs);

   // maak een aantal lijnen aan in de portefeuille

   pf.maakNieuweLijn("AGS", 2000, 1.8, "BRU", "23/10/2010");
   pf.maakNieuweLijn("COLR", 85, 36, "BRU", "24/10/2010");
   pf.maakNieuweLijn("BEKB", 100, 73, "BRU", "2/5/2010");
   pf.maakNieuweLijn("AGS", 1000, 1.0, "BRU", "24/11/2010");
   // onbekend aandeel
   pf.maakNieuweLijn("SMG", 900, 210, "FRA", "23/10/2010");
   pf.maakNieuweLijn("UCB", 500, 30, "BRU", "6/6/2010");

   // geef een overzicht van de portefeuille
   System.out.println(pf.overzichtPortefeuille());
   // geef de best presterende lijn !
   System.out.println("De best presterende lijn :" + 
   pf.bestperformingLijn());


}
Niels
  • 3
  • 3

3 Answers3

1

Change this line of Beurs class:

public void Beurs()

Into this:

public Beurs()

You can check code in if-statement that is null or not. Something like this :

if(aandeel != null && aandeel.getCode() != null)
SamiAzar
  • 1,260
  • 13
  • 29
  • I added the coding of aandeel in my question. Because with the changed if statement it still doesn't want to work :( – Niels Feb 10 '19 at 23:36
1

Your class is calling a default empty constructor because you created a constructor with a return type, which turns out to have a correct method syntax.

public void Beurs()
{
    lijstAandelen = new HashMap<>();
    lijstKeys = new HashSet<>();
}

This is why your maps are null.

Diego Victor de Jesus
  • 2,575
  • 2
  • 19
  • 30
0

constructor has no return type, so your constructor definition is wrong

public void Beurs() // wrong

so Beurs class has no constructor, so it is calling empty constructor and lijstAandelen, lijstAandelen are not initializing

By the way, you should change the constructor like below in Beurs class

public Beurs()
{
    lijstAandelen = new HashMap<>();
    lijstKeys = new HashSet<>();
}
Mahamudul Hasan
  • 2,745
  • 2
  • 17
  • 26