Reputation: 60
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)
Upvotes: 0
Views: 574
Reputation: 60
Mistakes corrected as-
1) Bought field initializers inside my constructor to get rid of NullpointerException as said by ( Basil Bourque , Ole V.V. )
2) created array of student objects in main and called method Stduarry on them .
3) Variable name Date changed to gc
import java.util.*;
INSIDE STUDENT CLASS:
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));
}
}
INSIDE DRIVER CLASS:
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);
}
}
}
Upvotes: 1
Reputation: 86324
Your NullPointerException
comes from this line:
int years = date.get(Calendar.YEAR);
Field initializers like this one are executed before the constructor. So when you create a Student2
object using new Student2()
, the above line is created while the date
field is still null
, and therefore the call to date.get()
throw the exception.
Instead move the initialization of years
into the constructor, after you have assigned an object to date
.
As others have said too, the GregorianCalendar
class is poorly designed and long outdated. If it wasn’t for a lazy teacher apperently with no clue of what has been going on with Java the last more than 5 years, you shouldn’t use it. Never ever.
Upvotes: 1
Reputation: 339372
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.
Upvotes: 3