访问日历类型作为字段值java

时间:2017-04-26 04:18:10

标签: java eclipse calendar

import java.util.Calendar;
import java.util.GregorianCalendar;


public class Station
{
    private Calendar arrival;
    private Calendar departure;
    private int day;
    private String city;

    public Station(String city, Calendar arrival, Calendar departure, int day)
    {
    city = this.city;
    arrival = this.arrival;
    departure = this.departure;
    day = this.day;
    };

    public String getArrival()
    {

    int hr = arrival.get(Calendar.HOUR);
    int mn = arrival.get(Calendar.MINUTE);

    String dep = "" + hr + ":" + mn;
    return dep;
    };

    Calendar arto = new GregorianCalendar(2000,1,5,9,30,0);
    Calendar deto = new GregorianCalendar(2000,1,5,16,10,0);
    Station toronto = new Station("Toronto", arto, deto, 5);

    public static void main(String[] args)
    {
    toronto.getArrival();
    };

您好,我刚开始学习,我不明白为什么会导致nullPointerException。如果我更改方法直接访问Calendar对象(arto),那么它似乎工作正常。我的Station类中有什么东西让这个变得不可能吗?我承认我对Calendar课程的理解有限。如果在我不确定如何询问或找到答案之前已经接近问题,我道歉。

4 个答案:

答案 0 :(得分:0)

首先,在Station()中你使用“this”错误:

public Station(String city, Calendar arrival, Calendar departure, int day)
    {
       this.city = city;
       this.arrival = arrival;
       this.departure = departure;
       this.day = day;
    };

“在实例方法或构造函数中,这是对当前对象的引用”更多信息:here

第二,你试图从静态方法“main”访问一个非静态变量“toronto”:

static Calendar arto = new GregorianCalendar(2000,1,5,9,30,0);
static Calendar deto = new GregorianCalendar(2000,1,5,16,10,0);
static Station toronto = new Station("Toronto", arto, deto, 5);

答案 1 :(得分:0)

这看起来只是一个简单的错字。构造函数应该这样写:

public Station(String city, Calendar arrival, Calendar departure, int day)
{
    this.city = city;
    this.arrival = arrival;
    this.departure = departure;
    this.day = day;
};

(您将空字段cityarrivaldepartureday分配给参数。)

答案 2 :(得分:0)

问题出在Station类的构造函数中。看看正确的解决方案

package com.stackoverflow.json;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class Station
{
    private Calendar arrival;
    private Calendar departure;
    private int day;
    private String city;

    public Station(String city, Calendar arrival, Calendar departure, int day)
    {
    this.city=city;
    this.arrival=arrival ;
    this.departure=departure ;
    this.day=day;
    };

    public String getArrival()
    {

    int hr = arrival.get(Calendar.HOUR);
    int mn = arrival.get(Calendar.MINUTE);

    String dep = "" + hr + ":" + mn;

    return dep;
    };

   static  Calendar arto = new GregorianCalendar(2000,1,5,9,30,0);
    static Calendar deto = new GregorianCalendar(2000,1,5,16,10,0);
    static Station toronto = new Station("Toronto", arto, deto, 5);

    public static void main(String[] args)
    {
    String arrival2 = toronto.getArrival();
    System.out.println(arrival2);
    }
}

打印:

9:30

答案 3 :(得分:0)

此关键字是对当前对象的引用。

https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

您已经创建了错误的构造函数,您已将对象添加到构造函数内的局部变量中。

import java.util.Calendar;
import java.util.GregorianCalendar;

public class Station {
    private Calendar arrival;
    private Calendar departure;
    private int day;
    private String city;

    public Station(String city, Calendar arrival, Calendar departure, int day) {
        this.city = city;
        this.arrival = arrival;
        this.departure = departure;
        this.day = day;
    }

    public String getArrival() {
        int hr = arrival.get(Calendar.HOUR);
        int mn = arrival.get(Calendar.MINUTE);

        String dep = "" + hr + ":" + mn;
        return dep;
    }

    public static void main(String[] args) {
        Calendar arto = new GregorianCalendar(2000, 1, 5, 9, 30, 0);
        Calendar deto = new GregorianCalendar(2000, 1, 5, 16, 10, 0);
        Station toronto = new Station("Toronto", arto, deto, 5);
        System.out.println(toronto.getArrival());
    }
}