线程“main”java.lang.NullPointerException中的异常 在MovieSeating.assignCustomerAt(MovieSeating.java:27) 在Assignment8.main(Assignment8.java:84)
我从带有名字和姓氏列表的txt文件中提取数据后,在尝试将名称分配给位于2d数组范围内的某个元素后,它会给出错误
谢谢!
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();
}
}
答案 0 :(得分:4)
O.K。,这是你的问题:
你有一个名为seats
的数组。它属于String
类型。所以它可以包含像“???”这样的字符串或“免费座位”。但是,您尝试将客户放入其中。这正是错误消息所说的内容:error: incompatible types tempCustomer = Seats[row][col]; ^ required: Customer found: String 1 error
现在你有几个选择。一个非常简单的方法是将数组的类型更改为Customers。而不是把“???”在免费座位上,什么都不给他们。创建数组后,每个“槽”都是null
,实际上意味着“没有”。
BTW:您违反了许多编写Java代码的建议规则。这只是简单的事情,不会影响您的代码(无论如何都会运行;)。但是尊重这些不成文的规则是一种很好的做法(比如把{与函数头放在同一行或者用大写字母命名变量no)。
答案 1 :(得分:2)
由于错误消息本身表示您正在尝试将String分配给Customer。第27行
tempCustomer = Seats[row][col];
Seats是一个String数组,它将返回尝试分配给Customer
的String答案 2 :(得分:1)
将客户置于座位的代码应为:
private Customer getCustomerAt(int row, int col)
{
System.out.println("Customer at row " + row + " and col " + col + "." );
System.out.println(Seats[row][col]);
/** return customer */
return new Customer(Seats[row][col]);
}
public boolean assignCustomerAt(int row, int col, Customer tempCustomer)
{
if (Seats[row][col].equals("?.?"))
{
/** put a customer to the seat */
Seats[row][col] = tempCustomer.toString();
return true;
}
else
{
System.out.println("Seat taken..");
return false;
}
}