【问题标题】:"How to pass GregorianCalendar class object to a different class method ?"“如何将 GregorianCalendar 类对象传递给不同的类方法?”
【发布时间】:2019-08-31 02:09:32
【问题描述】:
  1. 我想使用一种能够创建学生记录的方法 从用户那里获取有关学生详细信息的输入。我的班级学生应该 包括以下领域:短学期, 全名, 注册号等 . 学生的注册号 = 年份和学号的串联。 例如,学生加入的年份 = 2023, 学生号= 80, 所以注册号=2380

另外,我的任务是使用 GregorianCalendar 类输入日期

学生课堂内部:

import java.util.*;

class Student2{

    String fullname;
    GregorianCalendar date;
    short semester;

    Student2()
    {

    }

    Student2(String name,short sem, GregorianCalendar Date)
    {
        fullname = name;
        semester=sem;
        date = Date;
    }

    int years = date.get(Calendar.YEAR);

    String year = Integer.toString(years);
    String Studno = Integer.toString(80);

    String y1= year.substring(0,3);
    String Reg = y1.concat(Studno);

    int reg = Integer.parseInt(Reg);

    void Studarry()
    {
         int n=5,i;
         Student2[] stuarry = new Student2[10];
         for(i=0;i<n;i++)
         {
             System.out.println("Enter name sem year month day gpa cgpa\n");
             Scanner sc = new Scanner(System.in);
             String name = sc.nextLine();

             short sem2 = sc.nextShort();
             int year2 = sc.nextInt();
             int month2 = sc.nextInt();
             int day2=sc.nextInt()

             GregorianCalendar gc2 = new GregorianCalendar(year2,month2,day2);


             stuarry[i] = new Student2(name,sem2,gc2);

         }
    }

    void Display()
    {

    }

}

内部驱动程序类:

public class Greg2{

    public static void main(String[] args)
    { 
         Student2 starr = new Student2();
         starr.Studarry();
    }

}

错误:

  • 线程“主”java.lang.NullPointerException 中的异常

    at oop2/lab5.Student2.<init>(Greg2.java:23)
    
    
    at oop2/lab5.Greg2.main(Greg2.java:68)
    

【问题讨论】:

  • 您正在尝试在 date 初始化之前初始化 years。这意味着date 仍然为 null,并且在其上调用方法将导致 NullPointerException。似乎您在 Student2 构造函数和 Studarry 方法之间的代码应该移动到 Student2 构造函数中,因为它们目前是在创建 Student2 的实例后立即执行的。您还必须实际组合这些变量并将其保存到fullname
  • 如果不明白为什么代码不能按原样运行,可以参考this post
  • 我的任务是使用 GregorianCalendar 类输入日期 提出如此可怕要求的老师永远不会离开吗? GregorianCalendar 类设计不佳且过时已久。没有人应该使用它。教师最少。

标签: java class constructor nullpointerexception gregorian-calendar


【解决方案1】:

类名与变量名

date = Date;

Date 带有大写的D 是类的名称,而不是变量。相反,您应该将要传递的参数的名称定义为date 而不是Date。该行变为date = date ;。编译器可以区分参数和成员变量。如果你想让读者更清楚,你可以说this.date = date ;

但这对于变量来说是一个糟糕的名称。因为确实有两个名为 Date 的类与 Java 捆绑在一起,都与 GregorianCalendar 相关,所以我建议避免使用 date 作为 GregorianCalendar 对象的变量名——这太混乱了。

java.time

GregorianCalendar 是一个糟糕的类。它在几年前被 java.time 类完全取代。具体来说,ZonedDateTime。两个类都代表某个特定区域(时区)的挂钟时间所看到的时刻。

但是,这两个类是为您的目的而设计的。您只需要一个日期,没有时间,也没有时区的上下文或与 UTC 的偏移量。所以LocalDate 符合您的需求。

LocalDate ld = LocalDate.of( year , month , day ) ;

构造函数

int years = date.get(Calendar.YEAR);

String year = Integer.toString(years);
String Studno = Integer.toString(80);
… 

这些行是浮动的,而不是放在方法中。它们应该放在方法的构造函数中。

为什么会有一个名为Greg2 的类?你是说一个特定的学生吗?如果是这样,Greg 应该由分配给 Student 类的 instance 的值表示。

名称末尾的所有2 字符是什么意思?命名很重要;直截了当,您将获得一半的解决方案。

所以大部分代码都是一团糟。 从头开始再试一次。 查找其他代码示例,例如 Stack Overflow、Oracle Tutorial 或本次家庭作业的课本。

了解关注点分离。一堂课应该只是代表一个学生。另一个类应该代表您的应用程序,并持有main 方法。使用Collection 将新实例化的Student 类收集到一个名册中,如果您有其他与名册相关的职责,可能会创建一个类Roster

最后,迈出小步。一次添加一个小东西,看看它是否正常运行。使用System.out.println 验证值。不要试图一次编写所有代码。

【讨论】:

    【解决方案2】:

    您的NullPointerException 来自这一行:

        int years = date.get(Calendar.YEAR);
    

    像这样的字段初始化器在构造函数之前执行。因此,当您使用new Student2() 创建Student2 对象时,会在date 字段仍为null 时创建上述行,因此对date.get() 的调用会引发异常。

    在将对象分配给date 之后,将years 的初始化移动到构造函数中。

    正如其他人所说,GregorianCalendar 类设计不佳且早已过时。如果不是一个懒惰的老师,显然不知道过去 5 年多来 Java 发生了什么,你不应该使用它。永远不会。

    【讨论】:

      【解决方案3】:

      错误更正为-

      1) 如 (Basil Bourque, Ole V.V.) 所说,在我的构造函数中购买了字段初始值设定项来摆脱 NullpointerException

      2) 在 main 中创建了一组学生对象,并在它们上调用了 Stduarry 方法。

      3) 变量名 Date 改为 gc

      import java.util.*;
      

      学生课堂内部:

      class Student22{
      
          String fullname;
          GregorianCalendar date;
          short semester;
          int reg;
      
          Student22()
          {
      
          }
      
          Student22(String name,short sem, GregorianCalendar gc)
          {
              fullname = name;
              semester=sem;
              date = gc;
      
               int years = date.get(Calendar.YEAR);
      
               String year = Integer.toString(years);
               System.out.println(year);
               String Studno = Integer.toString(80);
      
               String y1= year.substring(2,4);
      
               System.out.println(y1);
               String Reg = y1.concat(Studno);
               System.out.println(Reg);
      
               reg = Integer.parseInt(Reg);
               System.out.println(reg);
          }
      
      
      
           void Studarry(int n)
          {
      
      
      
                   System.out.println("Enter name sem year month day \n");
                   Scanner sc = new Scanner(System.in);
      
                   fullname = sc.nextLine();
                   System.out.println(fullname);
      
                   semester = sc.nextShort();
                   int year2 = sc.nextInt();
                   int month2 = sc.nextInt();
                   int day2=sc.nextInt();
      
                   GregorianCalendar gc2 = new GregorianCalendar(year2,month2,day2);  
                   date= gc2;
      
      
                   int years = date.get(Calendar.YEAR);
      
                   String year = Integer.toString(years);
                   String Studno = Integer.toString(n);
      
                   String y1= year.substring(0,3);
                   String Reg = y1.concat(Studno);
      
                   reg = Integer.parseInt(Reg);
      
                   Display();
           }
      
          void Display()
          {
               System.out.println(fullname);
               System.out.println(semester);
               System.out.println(reg);
               System.out.println(date.get(Calendar.YEAR));
      
          }
      
      
      }
      

      内部驱动程序类:

       public class Greg2{
      
          public static void main(String[] args)
          { 
      
               System.out.println("Please enter a Firstname , MiddleName & Lastname separated by spaces");
               Scanner sc = new Scanner(System.in);
               String name = sc.nextLine();
      
               GregorianCalendar gc = new GregorianCalendar(2018,7,22);
               Student22 s = new Student22(name,(short)3,gc);
               s.Display();
      
               int i,j,n;
               System.out.println("Enter n\n");
               n = sc.nextInt();
               Student22[] starr = new Student22[n+1];
               for(j=1;j<=n;j++)
               {
                   starr[j]= new Student22(); 
                   starr[j].Studarry(j);
               }
      
      
      
          }
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-28
        • 2014-03-24
        • 2011-09-04
        • 1970-01-01
        相关资源
        最近更新 更多