0

been struggling with this problem since a couple of hours. Including looking through the previous questions answered. (Im really new in Java, basicly started a couple of days ago)

it keeps giving me NullpointerExceptions between Register.läggTillhund(nyHund); in the first class and hundRegister.add(nyHund); in the second class.

I have no idea what might be causing them. Does anyone have Ideas?

The purpose of the code (when finished) is to add an object from the 3rd class, into a list in the secondclass using the firstclass as the "main program".

Thanks for the help! Oskar

First Class (Main)

public class testning {

    public static void main(String[] args) {
        Hund nyHund = new Hund("Daisy", 13, "labrador", 22.3);
        System.out.println(nyHund.toString());
        System.out.println(nyHund);
        Register.läggTillHund(nyHund);      

    }

}

Second Class:

import java.util.ArrayList;

public class Register {


    private static ArrayList<Hund> hundRegister;



    public static void läggTillHund(Hund nyHund){
        hundRegister.add(nyHund);
        System.out.println(nyHund);
        }



         public Register(){
             hundRegister = new ArrayList<Hund>();
    }


}

Third Class

public class Hund {

    private String namn;
    private int ålder;
    private double vikt;
    private String ras;

    public Hund(String hundnamn, int hundålder, String hundras, double hundvikt) {
        this.namn = hundnamn;
        this.ålder = hundålder;
        this.ras = hundras;
        this.vikt = hundvikt;
    }

    public String getNamn() {
        return namn;
    }

    public int getÅlder() {
        return ålder;
    }

    public double getSvanslängd() {
        if ("tax".equals(ras)){
            return 3.7;
        }else{
            return ((vikt*ålder)/10);
        }
    }

    public String toString() {
        return namn + "\n" + ålder + "\n"+ras+"\n"+vikt+"\n"+getSvanslängd();
    }
}
Oskar
  • 11
  • 1
  • You gave us all your code but no exception stack trace. The exception stack trace is key to diagnosing your problem. – Hot Licks Dec 04 '14 at 23:32
  • I don't think the duplicate is very helpful. All he has to do is change `private static ArrayList hundRegister;` to `private static ArrayList hundRegister = new ArrayList();` – August Dec 04 '14 at 23:33
  • As i said, pretty new in this. but is this what you need? Exception in thread "main" java.lang.NullPointerException at Register.läggTillHund(Register.java:11) at testning.main(testning.java:8) – Oskar Dec 04 '14 at 23:35
  • The `hundRegister` variable is only being initalized in the `Regiter` class constructor but you never call this constructor because you are just using a static method on this class: `läggTillHund`. – Roberto Linares Dec 04 '14 at 23:35
  • Thanks all of you! i understand my error now! – Oskar Dec 04 '14 at 23:43

1 Answers1

0

You're accessing static method. In this case constructor never working. Use private static ArrayList<Hund> hundRegister = new Arraylist<>() ;and delete the constructor. To see what's going on add System.out.println line to construct and you'll see it will never works