0
Exception in thread "main" java.lang.NullPointerException
    at MovieSeating.assignCustomerAt(MovieSeating.java:27)
    at Assignment8.main(Assignment8.java:84)

I have a text file it reads:

  • John Smith
  • George Bush
  • Random Person
  • Fourth Name

When I call this data in a text file called customerData.txt in the program and assign the name to a cell in the 2d array, I get a nullPoint error and have no idea how to fix this.

Thanks!


    public class Customer
     {
       private String lastName;
       private String firstName;

       // This constructor sets the first name and last name to "???�
       public Customer()
       {
              lastName = "???";
              firstName = "???";
       }

      // This constructor constructs a Customer object  given the last name and first name
       public Customer(String customerInfo)
       {
             int space = customerInfo.indexOf(" ");
             firstName = customerInfo.substring(0, space).trim();
             lastName = customerInfo.substring(space+1).trim();

       }

       // This constructor cConstructs a Customer object using the string containing customer's info.
       // It uses the StringTokenizer to extract first name, last name, id, the number of matinee tickets,
       // and the number of normal tickets.
       public Customer(String lName, String fName)
       {
             lastName = lName;
             firstName = fName;

       }

       // This method sets the last name.
       public void setLastName(String lName)
       {
             lastName = lName;
       }
       // This method sets the first name.
       public void setFirstName(String fName)
       {
             firstName = fName;
       }

       // This method returns the last name.
       public String getLastName()
       {
             return lastName;
        }
       // This method returns the first name.
       public String getFirstName()
       {
             return firstName;
       }

       // This method checks if a customer object passed as a parameter and itself (customer object)
       // are same using their last names and first names.
       public boolean equals(Customer other)
       {
             if (lastName.equals(other.lastName) && firstName.equals(other.firstName))
                 return true;
             else
                 return false;
       }

       // This method returns a string containing a customer's initials
       // (first characters of firstName and lastName.)
       public String toString()
       {
               String result = firstName.charAt(0) + "." + lastName.charAt(0) + ".";
               return result;
       }


     } // end of the class Customer

    class MovieSeating 
    {
        private String[][] Seats;
        public MovieSeating(int rowNum, int columnNum)
        {
            String [][] Seats = new String[rowNum][columnNum];
            for (int r = 0; r < rowNum; r++)
            {
                for (int c = 0; c < columnNum; c++)
                {
                    Seats[r][c] = "?.?";
                }
            }
        }

        private Customer getCustomerAt(int row, int col)
        {
            System.out.println("Customer at row " + row + " and col " + col + "." );
            System.out.println(Seats[row][col]);

        }

        public boolean assignCustomerAt(int row, int col, Customer tempCustomer)
        {
            if (Seats[row][col].equals("?.?"))
            {
                tempCustomer = Seats[row][col];
                return true;
            }
            else {
                System.out.println("Seat taken..");
                return false;
            }

        }

        public boolean checkBoundaries(int row, int col)
        {
            if (col < 0 || row < 0)
            {
                return false;
            }
            else {
                return true;
            }
        }
    }

    import java.io.*;
    import java.util.*;

    public class Assignment8
    {
       public static void main(String[] args) throws IOException
       {

           MovieSeating theatreSeating;
           Customer tempCustomer;
           int requestedRow, requestedCol, row, col, rowNum, columnNum;
           String line, fileName;

           // to read input from a KEYBOARD.
           Scanner stdin = new Scanner(System.in);

           // Ask a user to enter a number of rows for a movie theatre seating from a KEYBOARD.
           System.out.println("Please enter a number of rows for a movie theatre seating.");
           rowNum = stdin.nextInt();

           // Ask a user to enter a number of columns for a movie theatre seating from a KEYBOARD.
           System.out.println("Please enter a number of columns for a movie theatre seating.");
           columnNum = stdin.nextInt();

           // instantiate a MovieSeating object
           theatreSeating = new MovieSeating(rowNum, columnNum);

           // get a file name read from a KEYBOARD.
           System.out.println("Please enter a file name");
           fileName = stdin.next();

           // create FileReader and BufferedReader object to
           // read from a file.
           FileReader fr = new FileReader (fileName);
           BufferedReader inFile = new BufferedReader (fr);

           /*** reading a customer's information from a FILE ***/
           line = inFile.readLine();

           /*** we will read line by line until we read the end of a given file ***/
           while (line != null)
           {
               System.out.println("\nA customer information is read from a file.");
               // printing information read from a file.
               System.out.println(line);

               // creating a customer object using information from a file
               tempCustomer = new Customer(line);

               // Ask a user to decide where to seat a customer by asking for row and column of a seat
               System.out.println("Please enter a row number where the customer wants to sit.");
               requestedRow = stdin.nextInt();
               row = requestedRow -1;

               System.out.println("Please enter a column number where the customer wants to set.");
               requestedCol =  stdin.nextInt();
               col = requestedCol -1;

               // Checking if the row number and column number are valid (exist in the theatre that we created.)
               if (theatreSeating.checkBoundaries(row, col) == false)
               {
                    System.out.println("\nrow or column number is not valid.");
                    System.out.println("A customer " + tempCustomer.getFirstName() + " " + tempCustomer.getLastName() + " is not assigned a seat.");
               }
               else
               {
                  // Assigning a seat for a customer
                  if (theatreSeating.assignCustomerAt(row, col, tempCustomer) == true)
                  {
                    System.out.println("\nThe seat at row " + row + " and column " + col + " is assigned to the customer " + tempCustomer.toString());
                    System.out.println(theatreSeating);
                  }
                  else
                  {
                    System.out.println("\nThe seat is taken.");
                  }
                }
               // Read next line in a FILE
               line = inFile.readLine();

           }//end of the while loop
             // Closing the file
           inFile.close();

         }

      }
2
  • I think I answered you in your last question.. Commented Nov 26, 2013 at 9:59
  • 2
    Please don't call the field Seats. Variables should start with a lower-case letter: seats. Commented Nov 26, 2013 at 9:59

1 Answer 1

6

In your MovieSeating constructor, you're shadowing your Seats variable :

    private String[][] Seats;
    public MovieSeating(int rowNum, int columnNum)
    {
        Seats = new String[rowNum][columnNum]; //<-- remove String [][] 
        for (int r = 0; r < rowNum; r++)
        {
            for (int c = 0; c < columnNum; c++)
            {
                Seats[r][c] = "?.?";
            }
        }
    }

So when doing if (Seats[row][col].equals("?.?")) it throws a NullPointerException.

Sign up to request clarification or add additional context in comments.

10 Comments

Ok, I fixed that but now when I add someone, it outputs.. MovieSeating@403525a2
@Ryan - Override the toString() in your MovieSeating class.
Do I want to make a toString method and then put a for loop to print the 2d array?
@Ryan Yes, and you can also use Arrays.deepToString(Seats)
What am I returning in the toString method?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.