-2

I'm trying to get the length of seznam2 by using int postle in Stanovanje. However, the variable getPostle() does not pass and I get Exception in thread "main" java.lang.NullPointerException error for this line:

private String[] seznam2 = new String[getStanovanje().getPostle()]. 

I would also like to know when to use:

Stanovanje stanovanje = new Stanovanje(); 

or

private Stanovanje stanovanje1; 

Here is my code:

Potovanje.java

public class Potovanje {

    Stanovanje stanovanje = new Stanovanje();
    private Stanovanje stanovanje1;

    private String datumOdhoda;
    private int trajanje;

    private Popotnik[] popotnik;
    private ArrayList<Popotnik> seznam = new ArrayList<>();
    private String[] seznam2 = new String[getStanovanje().getPostle()];


    public Potovanje(String datumOdhoda, int trajanje){
        this.datumOdhoda = datumOdhoda;
        this.trajanje = trajanje;
    }

    public void setStanovanje(Stanovanje stanovanje1){
        this.stanovanje1 = stanovanje1;
    }

    public Stanovanje getStanovanje(){
        return stanovanje1;
    }

    public void setPopotnik(Popotnik[] popotnik){
        this.popotnik = popotnik;
    }

    public Popotnik[] getPopotnik(){
        return popotnik;
    }

    public ArrayList<Popotnik> getSeznam(){
        return seznam;
    }

    public void setSeznam2(String[] seznam2){
        this.seznam2 = seznam2;
    }

    public String[] getSeznam2(){
        return seznam2;
    }


    public void dodajPotnika(Popotnik[] popotnik){
        //System.out.println("postle: " + stanovanje.getPostle());
        for(int i=0; i<getSeznam2().length; i++){
            //System.out.println("wadap");
            setPopotnik(popotnik);
            seznam.add(getPopotnik()[i]);
        }

    }

    public String toString(){
        return "datumOdhoda: " + datumOdhoda + "\n" + "trajanje: " + trajanje + "\n" + "popotnik: " + getPopotnik();
    }
}

Stanovanje.java

public class Stanovanje {

    private int postle;

    public Stanovanje(){

    }

    public Stanovanje(int postle){
        this.postle = postle;
    }

    public void setPostle(int postle){
        this.postle = postle;
    }

    public int getPostle(){
        return postle;
    }

    public String toString(){
        return "postle: " + postle;
    }
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
norcek10
  • 3
  • 2
  • 5
    Your question is very unclear. Please include the errors you recieve and consider taking some time to formulate your question clearer. – OH GOD SPIDERS Feb 10 '17 at 10:51
  • 1
    The member variable `stanovanje1` is not explicitly initialized so it is `null`; the method `getStanovanje()` returns `null`, and then you try to call `getPostle()` on `null`, which results in a `NullPointerException`. – Jesper Feb 10 '17 at 11:05

1 Answers1

1

The difference between 1 and 2:

/*1*/ Stanovanje stanovanje = new Stanovanje(); 
/*2*/ private Stanovanje stanovanje1; 

is:

1 : has the default visibility (which is "package-private") and is immediately initialized. ( not null)

2 : has private visibility and is not initialized ( thus == null until otherwise initialized )

When to use each of those is hard to answer because there maybe a lot of cases where you'd prefer the one or the other.

If you use the second, you should initialize it in the constructor. Either by creating an instance there or by initialize it to a CTOR-Argument. Otherwise your object is not properly initialized and usage can lead to what you encountered: side-effects like an NPE.

In some cases (heavy objects that are rarely actually needed) it can be useful to leave them uninitialized until really needed (sometimes referred to as "lazy loading"). One would then use a getter that checks if the field has been initialized and do so if not before returning it.

About visibility you can read here: Controlling Access to Members of a Class (Oracle Documentation) Oracles "rule of thumb" there:

Use private unless you have a good reason not to.

Fildor
  • 14,510
  • 4
  • 35
  • 67