0

i've a question about my code. this is my code

Map <String, hewan> hwn = new HashMap<String,hewan>();
 for (int j = 1 ; j<=bacaInginHewan; j++){  
    hewan hewan = new hewan();
    String [] bacaJumlah =br.readLine().split(" ");
    namaHewan=bacaJumlah[0];
    ingin = Integer.parseInt(bacaJumlah[1]);
    if (hwn.containsKey(namaHewan)){
      if (hwn.get(namaHewan).jenisOperasi.equals("vivipar")){
        int jumlahAwal = hwn.get(namaHewan).jumlah;
        int batasAkhir = ingin + jumlahAwal ;
        hewan.setIngin(batasAkhir);
        hewan.recvivipar(jumlahAwal);
        System.out.println("butuh "+hewan.getIterasiVivi()+" siklus untuk "+batasAkhir+" "+namaHewan);
        hewan.setJumlah(batasAkhir);
        hwn.put(namaHewan, hewan);
      } else if (hwn.get(namaHewan).jenisOperasi.equals("ovipar")){
         int jumlahAwal = hwn.get(namaHewan).jumlah;
         int batasAkhir = ingin + jumlahAwal ;
         hewan.setIngin(batasAkhir);
         hewan.recOvipar(jumlahAwal);
         System.out.println("butuh "+hewan.getIterasiVivi()+" siklus untuk "+batasAkhir+" "+namaHewan);
         hewan.setJumlah(batasAkhir);
         hwn.put(namaHewan, hewan);
   }

My question is, why did it turn null pointer for second loop with same namaHewan in this if clause. thanks for the explanation

if (hwn.get(namaHewan).jenisOperasi.equals("vivipar"))
Vaseph
  • 704
  • 1
  • 8
  • 20
  • 1
    on a side note it is always safer to do `if ("vivipar".equals(hwn.get(namaHewan).jenisOperasi))` – singhakash Mar 11 '16 at 10:16
  • did my best to format your code, but it not valid code and it's not correct for me to assume the location of closing braces. – Ross Drew Mar 11 '16 at 10:18

1 Answers1

0

hwn.get(namaHewan).jenisOperasi seems to be null. Try to learn about debugging to fix problems like this more easily.

f1sh
  • 11,489
  • 3
  • 25
  • 51
  • thanks friend, tried the debugging. and get the answer now. it has something to do with my adding to hash map proses. – Deki Satria Mar 11 '16 at 10:19