0

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; 
 }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
monolith937
  • 429
  • 1
  • 4
  • 13
  • 1
    Post the complete stack trace of the exception. – JB Nizet Apr 02 '16 at 15:46
  • I'm sorry, new at Java, didn't quite understand what you're asking me, but this is the error I get: `Exception in thread "main" java.lang.NullPointerException` `at Kuca.toString(Kuca.java:24)` `at Test.main(Test.java:29)` – monolith937 Apr 02 '16 at 15:49
  • So, if you read it, you see that the problem is in `Kuca.toString()`, at line 29 of `Test.java`. It has nothing to do with the toString() method in Stan.java, as you're claiming in your question. What could possibly be null in this line, and thus cause a NullPointerException? Read the duplicate question. – JB Nizet Apr 02 '16 at 15:51
  • What did you provide as input to the program when you ran it? Have you checked that an int and a double was obtained correctly and not a null? – mxsscott Apr 02 '16 at 15:52
  • 2
    You are getting the NullPointerException beause you don't initilize s in Kuca, your contructor looks like this: `s= new Stan(povrsina, brStanara);` but it has to be `this.s= new Stan(povrsina, brStanara);` because otherwise you re-initilize the Stan object you're passing to the constructor. Hope this will help you. – Dimitrios Begnis Apr 02 '16 at 15:52
  • You're right, no it doesn't, but when I remove `s.toString()` in the Kuca.java `toString()` method, the problem disappears, and it traces the problem to the `toString()` in Stan.java @mxsscott yes I did, every input is fine, I put that debig println, to find it, the problem is at line 29, `kuca.toString()` – monolith937 Apr 02 '16 at 15:54
  • Note that you'd also not get a NPE if you used `s` instead of `s.toString()` (but you would see `null` in the returned string). – Andy Turner Apr 02 '16 at 16:29
  • @monolith937 no, the problem is not in Stan.toString(). The problem disappears when you remove s.toString() because the problem is precisely that you're trying to call toString on `s`, and `s` is null. Read the duplicate question again. If the exception was thrown from Stan.toString(), Stan.toString() would appear in the stack trace. – JB Nizet Apr 02 '16 at 17:04

0 Answers0