Click here to Skip to main content
15,886,036 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Exception in thread "main" java.lang.NullPointerException
	at EnrollmentMain.main(EnrollmentMain.java:211)


Getting this exception while running following program I don't understand what is this could you explain me how? i've search like 1-2hours to resolve this buy i couldn't understand this exception keeps popping up pardon me because I'm new thanks for your understanding and consideration.

What I have tried:

Here's the line 211:
for(int i=0; i<=count;i++){
                   System.out.println(students[i].getID());
               }
Posted
Updated 24-Sep-18 22:56pm

1 solution

If count is the length of the students array then the upper limit of the loop is wrong: Change from
Quote:
for(int i=0; i<=count;i++){
to
Java
for(int i=0; i<count;i++){
 
Share this answer
 
Comments
Member 13996630 25-Sep-18 4:26am    
The same problem sir "Exception in thread "main" java.lang.NullPointerException"
CPallini 25-Sep-18 4:53am    
Then you should show us more code.
Member 13996630 25-Sep-18 5:01am    
mport java.io.*;
import java.util.Scanner;

public class EnrollmentMain {
public static void main(String[] args) {

Subjects[] subjects = new Subjects[100];
System.out.println(java.util.Arrays.toString(subjects));
Students[] students = new Students[100];
System.out.println(java.util.Arrays.toString(students));
Schedules[] schedules = new Schedules[100];
System.out.println(java.util.Arrays.toString(schedules));


char exit ='y';

String input;

int count=0;
int count2=0;
int count3=0;

Scanner scan = new Scanner( System.in );
boolean shouldBreak = false;
while(exit!='n') {
try{

InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

String subid;
String code;
String lecdesc;
String lab;
String units1;
String units2;
String id;
String name;
String address;
String course;
String yr;
String sy;
String sem;
String time1;
String time2;
String day1;
String day2;
String rooms1;
String rooms2;
String sec;

int choice;
while(!shouldBreak){
System.out.println("\nMenu");
System.out.println("[1] Enter Subjects");
System.out.println("[2] Enter Students");
System.out.println("[3] Enter Schedules");


System.out.print("Choice: ");

choice=Integer.parseInt(br.readLine());

switch(choice){
case 0:
shouldBreak = true;
break;
case 1:
subjects[count]= new Subjects();
System.out.print("Enter Subject Code: ");
code = br.readLine();
subjects[count].setCode(code);

System.out.print("Enter Subject Id: ");
subid = br.readLine();
subjects[count].setSubid(subid);

System.out.print("Enter Subject Description(Lec): ");
lecdesc = br.readLine();
subjects[count].setLecdesc(lecdesc);

System.out.print("Enter Subject Description(Lab): ");
lab = br.readLine();
subjects[count].setLab(lab);

System.out.print("Enter Subject Lecture Units: ");
units1 = br.readLine();
subjects[count].setUnits1(units1);

System.out.print("Enter Subject Laboratory Units: ");
units2 = br.readLine();
subjects[count].setUnits2(units1);

count++;
CPallini 25-Sep-18 5:09am    
Add the code to your original question, please.
Member 13996630 25-Sep-18 5:02am    
System.out.println("\nSubject Information");
System.out.println("Code:" + subjects[count].getCode());
System.out.println("Subid:" + subjects[count].getSubid());
System.out.println("Subject Description(Lec):" + subjects[count].getLecdesc());
System.out.println("Subject Description(Lab):" + subjects[count].getLab());
System.out.println("Lecture Units:" + subjects[count].getUnits1());
System.out.println("Laboratory Units:" + subjects[count].getUnits2());
break;

case 2:
students[count2]= new Students();
System.out.print("\nEnter Student ID: ");
id= br.readLine();
students[count2].setID(id);

System.out.print("Enter Student Name: ");
name= br.readLine();
students[count2].setName(name);

System.out.print("Enter Student Address: ");
address= br.readLine();
students[count2].setAddress(address);

System.out.print("Enter Student Course: ");
course = br.readLine();
students[count2].setCourse(course);

System.out.print("Enter Student Year Level: ");
yr = br.readLine();
students[count2].setYr(yr);

count2++;

System.out.println("\nStudent Information");
System.out.println("ID:" + students[count].getID());
System.out.println("Name:" + students[count].getName());
System.out.println("Address: " + students[count].getAddress());
System.out.println("Course:" + students[count].getCourse());
System.out.println("Year Level:" + students[count].getYr());
break;

case 3:
schedules[count3] = new Schedules();
System.out.print("School Year: ");
sy = br.readLine();
schedules[count3].setSy(sy);

System.out.print("Enter Semester Level: ");
sem = br.readLine();
schedules[count3].setSem(sem);

System.out.print("Enter Section: ");
sec = br.readLine();
schedules[count3].setSec(sec);

System.out.print("Enter Subject Time for Lecture: ");
time1 = br.readLine();
schedules[count3].setTime1(time1);

System.out.print("Enter Subject Time for Laboratory: ");
time2 = br.readLine();
schedules[count3].setTime2(time2);

System.out.print("Enter Subject Day for Lecture: ");
day1 = br.readLine();
schedules[count3].setDay1(day1);

System.out.print("Enter Subject Day for Laboratory: ");
day2 = br.readLine();
schedules[count3].setDay2(day2);

System.out.print("Enter Room for Lecture: ");
rooms1 = br.readLine();
schedules[count3].setRooms1(rooms1);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900