简体   繁体   English

如何实现抽象类的方法? (爪哇)

[英]How to implement methods of an abstract class? (Java)

I am trying to use the methods from an abstract class that is implementing an interface.我正在尝试使用实现接口的抽象类中的方法。 I keep getting a null pointer exception when I call upon a method and I am not sure why.当我调用一个方法时,我不断收到空指针异常,但我不知道为什么。 Any ideas?有任何想法吗? Thanks.谢谢。

package start;
public class Automobile extends Vehicle {     // code with main method
public static void main(String[] args) {
         Vehicle[] automobiles = new Vehicle[3];
         automobiles[0].setVehicleName("Corvette");
    }
}

////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////// ////////////////////////

package start;

public abstract class Vehicle implements Movable {

    String name = "Unidentified"; // variables for vehicles
    String manufacturer = "Factory";
    String car = "Unknown";
    int yearOfManufacture = 0000;
    int horsepower = 0;
    static int instances = 0;

    int passengers = 0; // variables for methods below
    int speed = 0;

    public int getNoPassengers() { // returns how many passengers there are
        instances = instances + 1;
        return passengers;
    }

    public void setNoPassengers(int noPassengers) { // sets the number of passengers
        instances = instances + 1;
        passengers = noPassengers;
    }

    public int getTopSpeed() { // returns how fast a movable vehicle is
        instances = instances + 1;
        return speed;
    }

    public void setTopSpeed(int topSpeed) { // changes the speed of a movable vehicle
        instances = instances + 1;
        speed = topSpeed;
    }

    public void setVehicleName(String title) { // changes the name of a vehicle
        instances = instances + 1;
        name = title;
    }

    public String getVehicleName(String car){
        return car;
    }

    public void setManufacturer(String creator) { // changes the manufacturer
        instances = instances + 1;
        manufacturer = creator;
    }

    public String getManufacturer(String type){
        return type;
    }
}

////////////////////////////////////////////// //////////////////////////////////////////////

package start;

interface Movable {      // interface

    int getNoPassengers(); // returns how many passengers there are

    void setNoPassengers(int noPassangers); // sets the number of passengers

    int getTopSpeed(); // returns how fast a movable vehicle is

    void setTopSpeed(int topSpeed); // changes the speed of a movable vehicle
}

The issue is that you have only created the array of vehicles in the following lines -问题是您只在以下几行中创建了车辆数组 -

Vehicle[] automobiles = new Vehicle[3];

You still need to initialize the variables to objects by using new Vehicle (or new Automobile , since Vehicle is an abstract class and cannot be instantiated) , before they can be accessed.在访问变量之前,您仍然需要使用 new Vehicle (或 new Automobile ,因为 Vehicle 是一个抽象类并且无法实例化)将变量初始化为对象。

Example -例子 -

Vehicle[] automobiles = new Vehicle[3];
automobiles[0] = new Automobile();
automobiles[0].setVehicleName("Corvette");

Your code in mail is as below:您在邮件中的代码如下:

Vehicle[] automobiles = new Vehicle[3];
automobiles[0].setVehicleName("Corvette");

Here you just allocated array but element within it is still null (and hence null pointer exception when calling setter on null object) and needs to initialized as well like:在这里,您刚刚分配了数组,但其中的元素仍然为空(因此在空对象上调用 setter 时出现空指针异常)并且需要初始化,例如:

automobiles[0] = new ....;
//then access method from within automobiles[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM