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

  • SEARCH
  • Home
  • 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 8766889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:39:42+00:00 2026-06-13T16:39:42+00:00

I have main class where I define a 2d array of type Seat and

  • 0

I have main class where I define a 2d array of type Seat and then use a method to randomly select a seat element and set it as “booked”

However if it has to randomly select more than 1 (Sometimes even if it only needs to randomly select one) seat element it returns this error:

"Exception in thread "main" java.lang.NullPointerException
    at cinemasystem.Seat.bookSeat(Seat.java:173)
    at cinemasystem.CinemaSystem.main(CinemaSystem.java:70)
Java Result: 1"

I have no clue what I am doing wrong, probably something with my random generating method, anyhow any help would be appreciated.

Main class:

public static void main(String[] args) {


        Seat cinemaactual = new Seat("Cinema");
        Seat[][] cinema = new Seat[12][23];
        Seat bookedSeat = new Seat("");

        Ticket ticket = new Ticket();
        Scanner scan = new Scanner(System.in);
        String answer, contiune, list;
        cinemaactual.CreateTheatre(cinema);
        int number, check = 0, category, id;

        do {

            System.out.print("Welcome to the Theatre Booking System. (QUIT to exit)"
                    + "\nWould you like to purchase tickets or list available seats?"
                    + "(/Purchase/List/Help)");
            answer = scan.nextLine();

            if (answer.equalsIgnoreCase("purchase")) {
                do {

                    System.out.println("Please choose the cateogry of ticket "
                            + "you would like, followed by who the ticket is for"
                            + "and the amount required. (separated by a space)\n"
                            + "1. Gold\n2. Silver\n3. Bronze\n\n1. Adult\n2."
                            + " Child\n3. Concession");
                    category = scan.nextInt();
                    check = category;
                    id = scan.nextInt();
                    number = scan.nextInt();
                    if (category == 1 || category == 2 || category == 3 && id == 1 || id == 2 || id == 3) {

                        ticket.SetType(category);
                        if (category == 1) {
                            ticket.SetName("Gold");
                        } else if (category == 2) {
                            ticket.SetName("Siler");
                        } else {
                            ticket.SetName("Bronze");
                        }
                        ticket.SetNumber(number);
                        ticket.SetID(id);
                        if (id == 1) {
                            ticket.SetCategory("Adult");
                        } else if (id == 2) {
                            ticket.SetCategory("Child");
                        } else {
                            ticket.SetCategory("Bronze");
                        }
                        System.out.print("You have selected "
                                + ticket.GetNumber() + " " + ticket.GetName()
                                + " ticket(s) at the " + ticket.GetCategory() + " price.");
                        System.out.println();
                        ticket.BuyTicket(category, id);
                        bookedSeat = Seat.bookSeat(cinema, number);


                    } else {

                        System.out.print("Sorry, incorrect input, please enter an apropriate value.");
                        check = scan.nextInt();
                    }
                } while (check == 0 || check > 3);

                do {
                    System.out.print("Would you like to perchase more tickets? (Yes/No)");
                    contiune = scan.nextLine();


                    if (contiune.equalsIgnoreCase("Yes")) {
                        System.out.print("Would you like to list available seats? (Yes/No) (A/G/S/B)");
                        list = scan.nextLine();
                        cinemaactual.DisplayTheatre(cinema);
                        if (list.equalsIgnoreCase("Yes")) {
                            cinemaactual.DisplayTheatre(cinema);

                        }
                    }

The method that randomly selects a seat, in my seat class:

 public static Seat bookSeat(Seat[][] x, int number){
        int count = 0;
        Seat book = new Seat("");
        Random rand = new Random();
        while(count < number){
    if (x != null) {

        do {
        book = x[rand.nextInt(x.length)][rand.nextInt(x.length)];
        book.bookSeat(true);}

        while (book.isBooked());
    }   count++; }
    return book;
}

Line 172 and 173 of Seat Class

172: book = x[rand.nextInt(x.length)][rand.nextInt(x.length)];

173: book.bookSeat(true);}

CreateTheatre Method in class Seat:

 public Seat[][] CreateTheatre(Seat[][] x) {

        for (int row = 0; row < 8; row++) {
            for (int col = 0; col < 4; col++) {
                x[row][col] = new Seat("B");
            }
        }
        for (int row = 8; row < 12; row++) {
            for (int col = 0; col < 4; col++) {
                x[row][col] = new Seat("S");
            }
        }

        for (int row = 0; row < 8; row++) {
            for (int col = 19; col < 23; col++) {
                x[row][col] = new Seat("B");
            }
        }
        for (int row = 8; row < 12; row++) {
            for (int col = 19; col < 23; col++) {
                x[row][col] = new Seat("S");
            }
        }
        for (int row = 3; row < 5; row++) {
            for (int col = 4; col < 9; col++) {
                x[row][col] = new Seat("B");
            }
        }
        for (int row = 3; row < 5; row++) {
            for (int col = 14; col < 19; col++) {
                x[row][col] = new Seat("S");
            }
        }
        for (int row = 9; row < 12; row++) {
            for (int col = 7; col < 4; col++) {
                x[row][col] = new Seat("S");
            }
        }
        for (int row = 3; row < 5; row++) {
            for (int col = 14; col < 20; col++) {
                x[row][col] = new Seat("B");
            }
        }

        for (int row = 5; row < 9; row++) {
            for (int col = 4; col < 9; col++) {
                x[row][col] = new Seat("S");
            }
        }

        for (int row = 5; row < 9; row++) {
            for (int col = 14; col < 20; col++) {
                x[row][col] = new Seat("S");
            }
        }
        for (int row = 6; row < 9; row++) {
            for (int col = 9; col < 14; col++) {
                x[row][col] = new Seat("G");
            }
        }
        for (int row = 9; row < 12; row++) {
            for (int col = 7; col < 16; col++) {
                x[row][col] = new Seat("G");
            }
        }

        for (int row = 9; row < 12; row++){
            for (int col = 4; col < 7; col++){
                x[row][col] = new Seat("S");
            }
        }

        for (int row = 9; row < 12; row++){
            for (int col = 16; col < 19; col++){
                x[row][col] = new Seat("S");
            }
        }
        return x;
            }
  • 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-13T16:39:43+00:00Added an answer on June 13, 2026 at 4:39 pm

    If you checked CreateTheatre logic there are possibilities that values inside cinema can be null. Put default values inside array by having one loop.

    Take a example of row 0 you only fill values from 0 to 4 and then 19 to 23. Apart from that I do not see you filling values.

    for(int i = 0; i<cinema.length;i++)
        for(int j=0; j<cinema[i].length;j++)
          cinema[i][j] = new Seat("");
    

    Update:
    If don’t want to put empty seats then you can just add null check before calling book.bookSeat(true)

    if(book != null){
     book.bookSeat(true);
    }
    

    You can print multidimentional arrays using Arrays.deepToString(cinema).

    As pointed out in other thread use book = x[rand.nextInt(12)][rand.nextInt(23)]

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

Sidebar

Related Questions

I have the next scenario: I define an int[][] variable in my main class.
I have a Class(call it main class) and the method in main class calls
I'm trying to define an array of type Class, for my homework. The classB
I have my main class PayUnit where I create my main object references, such
I have a main class and two extended classes: class Main { public $foo;
I have one main class with several inherited members who all overload the same
I have a main class in a program that launches another class that handles
I have a main class(which is basically a netbeans form;drag and drop) from where
I have a Main class and a HelloWorld class. I have created a button
So I have a main class and another class that has a variable I

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.