-1

I have 2 classes and in my Driver.java class when i try to run it this pops up after entering the 2 Commercial buildings: "Exception in thread "main" java.lang.NullPointerException at CommercialBuilding.equals(CommercialBuilding.java:39) at Driver.main(Driver.java:8)"

(This is a CSCI hw that I'm stuck on and would appreciate any help)

    class CommercialBuilding {
    
    private String address;
    private double sqFootage;
    private int units;
    
    
    public CommercialBuilding(String add, double footage, int unit) {
        add = address;
        footage = sqFootage;
        unit = units;
    }
    
    
    
    public String getAddress() {
        return address;
    }
    public double getFootage() {
        return sqFootage;
    }
    public int getUnits() {
        return units;
    }
    
    @Override
    public String toString() {
        String s = getAddress() + " " + getFootage() + " " + getUnits();
        return s;
    }
    
    @Override
    public boolean equals(Object obj) {
        if(!(obj instanceof CommercialBuilding)) {
            return false;
        }
        CommercialBuilding c = (CommercialBuilding)obj;
        
        return (this.address.equals(c.address) && this.sqFootage == c.sqFootage && this.units == c.units);
        
    }
    
    @Override
    public int hashCode() {
        int result = Double.hashCode(this.sqFootage);
        result = result * 31 + Integer.hashCode(units);
        result = result * 31 + this.address.hashCode();
        return result;
    }

}

    import java.util.Scanner;
class Driver {
    public static void main(String[] args) {
        
        
        CommercialBuilding c1 = createCommercialBuilding();
        CommercialBuilding c2 = createCommercialBuilding();
        System.out.println("c1 equals c2: " + c1.equals(c2));
        
        
    }
    
    static CommercialBuilding createCommercialBuilding() {
        Scanner kb = new Scanner(System.in);

        System.out.println("Please enter an address");
        String x = kb.nextLine();
        System.out.println("Please enter Square footage");
        double y = kb.nextDouble();
        System.out.println("Please enter number of units");
        int z = kb.nextInt();

        CommercialBuilding com = new CommercialBuilding(x, y, z);
        return com;
    }
}
Michelle
  • 3
  • 1
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zephyr Mar 26 '21 at 05:08
  • 1
    The assignment statements in the `CommercialBuilding` constructor is **assigning in the wrong direction**. – Andreas Mar 26 '21 at 05:16

2 Answers2

0

Change your CommercialBuilding() constructor as the following and give it a try:

public CommercialBuilding(String address, double sqFootage, int units) {
        super();
        this.address = address;
        this.sqFootage = sqFootage;
        this.units = units;
    }

You better use the editor to generate the constructor instead of typing by your own

Phyllis
  • 64
  • 1
  • 11
  • No, they should not rely on the editor to write the code for them; they need to learn how to write it themselves first. IDEs are wonderful for saving time with the "generate code" features, but if you don't know what they're generating or why, you're handicapping yourself. – Zephyr Mar 26 '21 at 05:52
  • @Zephyr Yeah, this is true. Autumn has to understand how to properly build a constructor. Head first java is a good book to start with – Phyllis Mar 26 '21 at 06:02
0

Here is the problem:

class CommercialBuilding {

private String address;
private double sqFootage;
private int units;


public CommercialBuilding(String add, double footage, int unit) {
    add = address;
    footage = sqFootage;
    unit = units;
}

So you need to reverse that to be:

   private String address;
    private double sqFootage;
    private int units;
    
    
    public CommercialBuilding(String add, double footage, int unit) {
        address=add ;
        sqFootage=footage;
        units=unit;
    }

Swapping add = address; to address=add;
Swapping footage = sqFootage; to sqFootage=footage;
Swapping unit = units; to units=unit;

Works Fine:
enter image description here

Mohamed Bdr
  • 967
  • 3
  • 9
  • 20