Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8780269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:59:53+00:00 2026-06-13T19:59:53+00:00

I am writing a program that takes in input various lecture requests that need

  • 0

I am writing a program that takes in input various lecture requests that need to be scheduled.
The lectures have start time and an end time.
The requirement is to schedule the maximum number of lectures. (The algorithm is to schedule them by end time and select the non overlapping lectures–Greedy Strategy).
In order to do so I have a “lecture” class and a “LectureScheduling” class.
I create a input array (of lectures). Then I request the user to input the various requests.
However I am getting the error “Exception in thread “main” java.lang.NullPointerException”.
Kindly help.
Thank you.
PS:
I have the error excatly at the line “input[i].time[0] = in.nextInt();”
The exact error is:
Exception in thread “main” java.lang.NullPointerException
at lecturescheduling.LectureScheduling.main(LectureScheduling.java:116)

// The lecture class.. time[0] is start time and time[1] is end time of the lecture

 class lecture{
     int[] time= new int[2];

     lecture (int a, int b){
          time[0]=a;
          time[1]=b;        
     }
 }

// Part of LectureScheduling Class

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Input number of lectures ");
    int arraylength = in.nextInt();
    lecture [] input= new lecture[arraylength] ; 
    for (int i=0; i<arraylength; i++){
        System.out.println("Input start time of lecture "+ i);
        input[i].time[0] = in.nextInt();        
        System.out.println("Input end time of lecture "+ i);
        input[i].time[1] = in.nextInt();        
        System.out.println();
    }
    input=SortByStartTime(input);
    input=CreateSchedule(input);
    PrintSchedule(input);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:59:54+00:00Added an answer on June 13, 2026 at 7:59 pm

    When allocating an array of objects, you are actually only allocating the references, and not the object itself (lecture [] input= new lecture[arraylength] ; does not create objects of type lecture, unlike value type languages such as C++). Thus when you access:

    input[i].time[0] = in.nextInt(); 
    

    Without to first create an instance of lecture for input[i], you get an NPE.

    To solve it, you need to use the new operator on each object of lecture before you try to access it (assuming the empty constructor is visible and defined):

    input[i] = new lecture();
    

    P.S. There is a convention in java that type names (such as lecture, which is a type) – start with upper case letter. So I would recommend renaming lecture into Lecture.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im writing a program that should read input via stdin, so I have the
I'm writing a program that takes some input of names and looks at a
I am making a program that takes input from files that I have called
I have a completely non-interactive python program that takes some command-line options and input
Looking for help on writing a Perl program that takes an input file and
I am writing a program that takes in an input file from the user.
I'm writing a tiny program that takes user input using Getops, and based on
So im writing c++ program, that takes a integer from input file, multiply it
I am writing a drum machine program. I need a function that takes the
I'm writing a program that lets the user input 6 temperature readings, and then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.