EDIT: The problem was in the Kuca.java file, in the first constructor, the first parameter should have been Stan _s
or in the body this.s= new Stan(s);
. Then the constructor has something to reference to.
================
Facing the null pointer issue, the only thing I've managed to pinpoint is that the toString()
method in Stan.java is causing the trouble, but I can't seem to figure out why.
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
System.out.println("Unesite adresu");
String adresa= s.nextLine();
System.out.println("Unesite stambeni objekat koji zelite");
String tip= s.next();
if(tip.equals("k")) {
System.out.println("Unesite broj ukucana");
int brUkucana= s.nextInt();
System.out.println("Unesite broj kvadrata");
double P= s.nextDouble();
Kuca kuca= new Kuca(new Stan(P, brUkucana), adresa);
System.out.println("asdasdas");
System.out.println(kuca.toString());
System.out.println("Porez: " + kuca.porez);
}
}
}
The Kuca.java, note the return
in the toString()
method, the s.ToString()
is causing the trouble (when I remove it, the problem is gone(
public class Kuca extends StambeniObjekat {
private Stan s;
public Kuca(Stan s, String adresa) {
super(adresa);
s= new Stan(s);
}
public Kuca(double povrsina, int brStanara, String adresa) {
super(adresa);
s= new Stan(povrsina, brStanara);
}
public Kuca(final Kuca k) {
super(k);
s= new Stan(k.s);
}
public String toString() {
return "Kuca: \n" + "Adresa: " + getAdresa() + "\n" + s.toString();
}
public double porez(double cena_po_kvadratu) {
if(s.getBrStanara() <= 2) {
porez= s.getPovrsina() * cena_po_kvadratu;
}
else if (s.getBrStanara() > 2) {
porez= s.getPovrsina() * cena_po_kvadratu * (1 - (s.getBrStanara() - 2) * 0.05);
}
return porez;
}
}
And the source of the s.toString()
found in the return
of the previus file.
public class Stan {
private double povrsina;
private int brStanara;
public Stan(double povrsina, int brStanara) {
this.povrsina=povrsina;
this.brStanara=brStanara;
}
public Stan(final Stan s) {
povrsina=s.povrsina;
brStanara=s.brStanara;
}
public double getPovrsina() {
return povrsina;
}
public void setPovrsina(double povrsina) {
this.povrsina=povrsina;
}
public int getBrStanara() {
return brStanara;
}
public void setBrStanara(int brStanara) {
this.brStanara=brStanara;
}
public String toString() {
return "Povrsina stana je " + povrsina + " m^2 \n" + "Broj Stanara je " + brStanara;
}
}