Plus I have been tasked to input date using class GregorianCalendar
INSIDE STUDENT CLASS:
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()
{
}
}
INSIDE DRIVER CLASS:
public class Greg2{
public static void main(String[] args)
{
Student2 starr = new Student2();
starr.Studarry();
}
}
ERRORS :
Exception in thread "main" java.lang.NullPointerException
at oop2/lab5.Student2.<init>(Greg2.java:23)
at oop2/lab5.Greg2.main(Greg2.java:68)
date = Date;
Date
with an uppercase D
is the name of a class, not a variable. Instead you should have defined the name of the argument being passed as date
not Date
. This line becomes date = date ;
. The compiler can distinguish between the argument and the member variable. If you want more clarity for the reader, you can say this.date = date ;
.
But that is a poor name for a variable. Because there is indeed two classes bundled with Java named Date
, both related to GregorianCalendar
, I suggest avoiding the use of date
as a variable name for GregorianCalendar
object – just too confusing.
The GregorianCalendar
is a terrible class. It was supplanted entirely years ago by the java.time classes. Specifically, ZonedDateTime
. Both classes represent a moment as seen through the wall-clock time of some particular region (a time zone).
However, both classes are not meant for your purpose. You want only a date, without a time-of-day and without the context of a time zone or offset-from-UTC. So LocalDate
fits your needs.
LocalDate ld = LocalDate.of( year , month , day ) ;
int years = date.get(Calendar.YEAR);
String year = Integer.toString(years);
String Studno = Integer.toString(80);
…
These lines are floating around, not placed inside a method. They should have been put inside the constructor of your method.
Why is there a class named Greg2
? Did you mean a specific student? If so, Greg should be represented by values assigned to an instance of a Student
class.
What is with all the 2
characters at the end of names? Naming is important; get that straight and you will be half-way to a solution.
So most of this code is a mess. Try again from scratch. Look up other code examples, such as on Stack Overflow, in the Oracle Tutorial, or in the textbook for your class of this homework assignment.
Learn about separation of concerns. One class should be just about representing a student. Another class should represent your app, and hold the main
method. Use a Collection
to gather the newly instantiated Student
classes into a roster, possibly making a class Roster
if you have other roster-related responsibilities.
Lastly, take baby steps. Add one little thing at a time, see that it runs properly. Use System.out.println
to verify values. Do not try to write all the code at once.