0

Can't understand, i have problem with abstract class getOn method, if i split my classes to different files and declare them as public, like that

package test.local;

abstract public class Passenger {
    public void getOn(Transport transport) {
        transport.takePassenger(Passenger.this);
    }
}

package test.local;

public class Dog extends Passenger {
}

everything works fine, i can pass Passenger.this to getOn method, but if i write whole application in single file like that

import java.util.List;

interface Transport {
    public void takePassenger(Passenger passenger);
}

class Bus implements Transport {
    private List<Passenger> passengers;

    public void takePassenger(Passenger passenger) {
        passengers.add(passenger);
    }

    public List<Passenger> getPassenger() {
        return passengers;
    }
}

abstract class Passenger {
    public void getOn(Transport transport) {
        transport.takePassenger(Passenger.this);
    }
}

class Dog extends Passenger {

}


public class Magic {
   public static void main(String []args) {
      Bus bus = new Bus();

      Dog passenger = new Dog();
      passenger.getOn(bus);
   }
}

i am getting Exception in thread "main" java.lang.NullPointerException why?

Exception in thread "main" java.lang.NullPointerException
at Bus.takePassenger(Magic.java:11)
at Passenger.getOn(Magic.java:21)
at Magic.main(Magic.java:35)
MyMomSaysIamSpecial
  • 3,194
  • 9
  • 41
  • 71
  • Sounds like a configuration problem. Make sure you clean your project. What line is throwing the exception? (Please post the stack trace.) Also, the first example has classes `Passenger` and `Dog` in package `test.local`, but the code that's throwing the exception seems (from what you've posted) to have those classes in the default package. That could be part of the configuration problem you're having. – Ted Hopp Nov 08 '18 at 21:32
  • Java exceptions include a stack trace. You should provide that information, or at least provide the line number of the offending line of code. – jarmod Nov 08 '18 at 21:32
  • added trace, thank you – MyMomSaysIamSpecial Nov 08 '18 at 21:34
  • 1
    You have never initialized `passangers`. Change `private List passengers;` to `private List passengers = new ArrayList<>();` – Edwardth Nov 08 '18 at 21:34

4 Answers4

1

Change this:

private List<Passenger> passengers;

to this:

private List<Passenger> passengers = new ArrayList<Passenger>();

In the former line of code, you have not initialized passengers to a value so it is null. Attempting to dereference it, for example with passengers.add(passenger), will yield a NullPointerException.

jarmod
  • 46,751
  • 9
  • 81
  • 86
1

You need to initialize the passengers field in your Bus class. Change this declaration:

private List<Passenger> passengers;

to:

private List<Passenger> passengers = new ArrayList<>();

Moving classes around to separate files has nothing to do with the error you're reporting.

Ted Hopp
  • 222,293
  • 47
  • 371
  • 489
0

It will always throw a NullPointerException until you initialize your list:

private List<Passenger> passengers;

to

private List<Passenger> passengers = new ArrayList<Passenger>();
0

its because you have not initialized

private List<Passenger> passengers;

make a default constructor in you Bus class like this:

public Bus(){
   passengers = new ArrayList<>();
}
AppleCiderGuy
  • 1,027
  • 1
  • 7
  • 13