1

I have 2 toString methods. They all work fine.

//This toString is the attendant toString.The parameter assign is of type car
    public String toString() {
        return "Attendant [staffNum=" + staffNum + ", id=" + id + ", available=" + available + ", name=" + name
                + ", assign=" + assign + "]";
    }

The toString below is from the car class

public String toString() {
            return "Car:" + plateNum + " ID:" + carID + " Position:" + position + " Assigned to:" + assignedTo.getId() 
            + "(" + assignedTo.getName() + ")" + " Parked at:" + convert(currTime);
        }

My problem is that if I print with the toString attendant it prints fine then for the assign variable since it is type car it calls the car toString. I can't change the car toString cause I use it as it is.

I tried some getmethods calling the car ID and position but it calls for all attendants.

I tried a null check. It would check that if assign is not null it would call the getmethods. This one crash my java.

public String strAssignAttCar(Cars assign){
        String result = null;
        if(!(assign.equals(null))){
            result = Cars.getCarID();
        }

and I get this:

Exception in thread "main" java.lang.NullPointerException
    at Attendant.strAssignAttCar(Attendant.java:201)
    at Attendant.toString(Attendant.java:217)
    at java.lang.String.valueOf(Unknown Source)
    at java.io.PrintStream.println(Unknown Source)
    at Attendant.createAtt(Attendant.java:64)
    at Main.main(Main.java:15)

PS:at first and most of the time the attendant parameter assign is null

please help me I dont know what else to try.

Kavi
  • 113
  • 1
  • 8
  • *"My problem is that if I print with the toString attendant it prints fine then for the assign variable since it is type car it calls the car toString. I can't change the car toString cause I use it as it is."* I'm sorry, that's completely unclear. Can you have a friend or colleague help with the wording? – T.J. Crowder Sep 21 '16 at 07:11
  • 3
    Use `assign == null` instead of `assign.equals(null)`. When you call a method (such as `equals`) on a reference that is `null`, you get a `NullPointerException`. – Jesper Sep 21 '16 at 07:11
  • thanks it worked and sorry for my English its only my 3rd language – Kavi Sep 21 '16 at 07:14
  • Kavi, it's not a dupe ofc. I voted to reopen this question. I have the answer. Please ask another question with `Car#toString` only. Describe the problem which is that you don't know what to do when assignedTo is a null. – xenteros Sep 21 '16 at 07:14

0 Answers0