0

I've been testing out generic programming in Java.

Here is a snippet of my code:

public class Vehicle<T extends Style> {

    private T make;
    private String color;
    private int year;
    private String brand;
    private String model;

    public Vehicle() {
        this.brand = make.getMake();
    }

    public Vehicle(String c, int yr, String mod) {
        this.brand = make.getMake();
        this.color = c;
        this.year = yr;
        this.model = mod;
    }

    public void printInfo() {
        System.out.println(this.year + " " + this.color + " " + this.brand + " " + this.model);
    }

    public void setColor(String c) {
        this.color = c;
    }
    public void setYear(int yr) {
        this.year = yr;
    }
    public void setModel(String mod) {
        this.model = mod;
    }
}

And here is the interface Style that is being extended in the type parameter:

public interface Style {

    public String getMake();
    public boolean usesGas();
    public boolean usesElectricity();

}

This interface is implement by another class called Ford, where the getMake() method is implement like so:

private String make = "FORD";

public String getMake() {
    return make;
}

In my driver class Test, I am testing all of these things like so:

public class Test{

    public static void main(String[] args){
        Vehicle<Ford> fordFusion = new Vehicle<Ford>();
        fordFusion.setColor("GRAY");
        fordFusion.setModel("FUSION");
        fordFusion.setYear(2015);
        fordFusion.printInfo();
    }
}

Now, in my understanding, one should be able to access the field variables and methods of a generic object by using an interface, which in this case is my Style interface. And the compiler has no problems with this in the IDE. The program should be accessing the getMake() method and printing out:

2015 GRAY FORD FUSION

However, the console shows:

Exception in thread "main" java.lang.NullPointerException at Vehicle.<init>(Vehicle.java:32) at Test.main(Driver.java:16)

which points to the line where I have this.brand = make.getMake();

So what in the world is going on here? What am I doing wrong?

EDIT: The question that this has been marked a duplicate of does not give a solution to my particular situation. Although the error here is a NullPointerException, it is much different than that older question.

Here is a question that address the same issue as mine. If the solution to that question is valid, then I should be able to accomplish what I've been trying to do.

  • 1
    Hint: what is the value of `make` in your second constructor? – Jacob G. Mar 05 '18 at 03:10
  • In the constructors, you haven't assigned any value to `make` yet, so it's null, so `make.getMake()` doesn't work. – Joshua Taylor Mar 05 '18 at 03:12
  • In all constructors. Nope, generics are not what you think they are. – Johannes Kuhn Mar 05 '18 at 03:13
  • 1
    Your generics are not needed here. You can simply replace `T` with `Style` and drop the generics part. And that's why you get a null pointer exception. – Johannes Kuhn Mar 05 '18 at 03:16
  • But can I even assign a value to `make`? It is of the generic type `T`, so I don't see how I could. Does this mean that what I'm trying to do is impossible? Am I unable to access the field variables and methods of a generic object? – Agent Blue Mar 05 '18 at 03:52
  • @AgentBlue: That is completely possible. Your problem is that you need an _instance_, and, due to type erasure, you can't create one yourself in that class. – SLaks Mar 06 '18 at 01:16
  • Instead, you should accept an instance as a parameter, at which point you probably don't need generics. – SLaks Mar 06 '18 at 01:16
  • @SLaks Yes, I see. I had eventually figured out that I would need to create an object of the `Ford` class and then pass through the constructor or a setter method of the generic object, which feels kinda redundant and thus defeats the purpose of generics. Maybe there's a situation where generics actually makes a lot of sense, but in this case it's just kinda silly. – Agent Blue Mar 06 '18 at 03:07

0 Answers0