public class Reservation
{
  private String firstname;
  private String lastname;
  private String address;
  private String name;
  private int licenseN;
  private String date;
  private int daysN;
  private String type;
  public Reservation(String f,String l,String a,int lpn,String dt,int nd,String tp)
{
  firstname = f;
  lastname = l;
  address = a;
  licenseN = lpn;
  date = dt;
  daysN = nd;
  type = tp;
}
  public String getName()
{
  return firstname + lastname;
}
  public String getAddress()
{
  return address;
}
  public int getLicenseN()
{
  return licenseN;
}
  public String getDate()
{
  return date;
}
  public int getDaysN()
{
  return daysN;
}
  public String getType()
{
  return type;
}
  public void setFirstname(String f)
{
  firstname = f;
}
  public void setLastname(String l)
{
  lastname = l;
}
  public void setAddress(String add)
{
  address= add;
}
  public void setLicenseN(int ln)
{
  licenseN = ln;
}
  public void setDate(String dt)
{
  date = dt;
}
  public void setDaysN(int dn)
{
  daysN = dn;
}
  public void setType(String tp)
{
  type = tp;
}
  public String toString()
{
  return "Name: "+ name + "\n" + "Address: " + address + "\n" + "License Number: " + licenseN + "\n" + "Arrival Date: " + date + "\n" + "Number of days: " + daysN + "\n" + "type of camping site : " + type +"\n";
}
}
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.Arrays;
public class ReservationBook
{
  private Reservation[] data;
  public Reservation[] readInfo(String filename) throws IOException
{
  FileReader reader = new FileReader(filename);
  try
 {
   Scanner in = new Scanner(reader);
   int numberofR = in.nextInt();
   data = new Reservation[numberofR];
   for(int i =0;i < numberofR;i++)
  {
   String f = in.nextLine();
   data[i].setFirstname(f);
   String l = in.nextLine();
   data[i].setLastname(l);
   String ad = in.nextLine();
   data[i].setAddress(ad);
   int ls = in.nextInt();
   data[i].setLicenseN(ls);
   String dt = in.nextLine();
   data[i].setDate(dt);
   int d = in.nextInt();
   data[i].setDaysN(d);
   String tp = in.nextLine();
   data[i].setType(tp);
  }
 }
  finally
 {
   reader.close();
 }
  return data;
}
  
    //readInfo(in,i);

  /*private  void readInfo(Scanner in,int i)throws IOException
{
   
}
*/
}
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class ReservationReader
{
  public static void main(String[] args)
{
  Scanner in = new Scanner(System.in);
  ReservationBook book = new ReservationBook();

  try
  {
    System.out.println("Please enter the file name: ");
    String filename = in.next();
    Reservation[] data = book.readInfo(filename);
    for(int i =0;i<data.length;i++)
      System.out.println(data[i].toString());
  }

  catch(FileNotFoundException exception)
  {
    System.out.println("File not found");
  }
  catch(IOException exception)
  {
    exception.printStackTrace();
  }
  
  //for(int i =0;i<data.length;i++)
    //data[i].sort();
  
  /*System.out.println("Enter the name you want to search: ");
  String n = in.nextLine();
  data.search(n);
  */
}
}

This is the reservationlist file:
12
liu
yan
elizabeth
111
2001
1
service

yu
xi
halley
222
2002
2
unservice
jia
tianyang
allandale
333
2003
3
service
wu
junxi
newfoundland
444
2004
4
unservice

Thanks very much.

Which line are you getting the null pointer at?

Exception in thread "main" java.lang.NullPointerException
at ReservationBook.readInfo(ReservationBook.java:21)
at ReservationReader.main(ReservationReader.java:16)

Your reservation class header looks like this:

public Reservation(String f,String l,String a,int lpn,String dt,int nd,String tp)

Therefore, to initialize it, supply arguments for each parameter and use the new keyword.

data[i]=new Reservation(<arguments go here>);

Make sure you put that line of code inside the for-loop, but before any other operation involving the Reservation.

import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.Arrays;
public class ReservationBook
{
  private Reservation[] data;
  public Reservation[] readFile(String filename) throws IOException
{
  FileReader reader = new FileReader(filename);
  try
 {
   Scanner in = new Scanner(reader);
   readInfo(in);
 }
  finally
 {
   reader.close();
 }
  return data;
}
  private void readInfo(Scanner in)
{
   int numberofR = in.nextInt();
   data = new Reservation[numberofR];
   String line = in.nextLine();
   Scanner lineScan = new Scanner(line);
   for(int i =0;i < numberofR;i++)
  {
   
   String f = lineScan.nextLine();
   data[i].setFirstname(f);
   String l = lineScan.nextLine();
   data[i].setLastname(l);
   String a = lineScan.nextLine();
   data[i].setAddress(a);
   int lpn = lineScan.nextInt();
   data[i].setLicenseN(lpn);
   String dt = lineScan.nextLine();
   data[i].setDate(dt);
   int nd = lineScan.nextInt();
   data[i].setDaysN(nd);
   String tp = lineScan.nextLine();
   data[i].setType(tp);
   data[i] = new Reservation(f,l,a,lpn,dt,nd,tp);
  }
 }

}

This is right?
I fix some

Your reservation class header looks like this:

public Reservation(String f,String l,String a,int lpn,String dt,int nd,String tp)

Therefore, to initialize it, supply arguments for each parameter and use the new keyword.

data[i]=new Reservation(<arguments go here>);

Make sure you put that line of code inside the for-loop, but before any other operation involving the Reservation.

1. Define a Java class definition called Reservation which includes the following:

* attributes:
o name (first and last names) of the person making the reservation
o address
o license plate number
o arrival date
o number of days for the reservation
o type of camping site (serviced or unserviced)

* constructor(s), accessor, mutator and toString methods

2. Create and initialize a 1-D array of at least 12 reservation objects. Input the reservation data from a text file.

3. Implement the following operations:

* Sort the array into alphabetical order by name, using the Insertion Sort algorithm.
* Given a person's name, search (using the Binary Search algorithm) the array for that name. If found, output all reservation information for that person. If not found, output a suitable message.
* Given a person's name, search (as previously described) the array and delete the associated information, if found.
* Given a person's name, search (as previously described) the array and change the arrival date and/or the length of the stay (ie, number of days). Provide both options and input the new information as required to do the update.

This is the question/
thanks

Could you please fix my code?
I may not figure out these.
I am new to java.

Line 45 must be the first line in the for-loop, because it has to be initialized before you can do anything with it. Thus, you should probably place it at line 30.

By the way, if you have the constructor (=new Reservation(...)), you don't need to make all the mutator calls (data.set...) because the constructor does that for you.

for(int i =0;i < numberofR;i++)
{
data = new Reservation(f,l,a,lpn,dt,nd,tp);
String f = lineScan.nextLine();
String l = lineScan.nextLine();
String a = lineScan.nextLine();
int lpn = lineScan.nextInt();
String dt = lineScan.nextLine();
int nd = lineScan.nextInt();
String tp = lineScan.nextLine();
}

right?

Well now the problem is that f, l, a, and the rest of the variables don't exist when you call the constructor (I think there was a misunderstanding regarding my last post, I was referring to two different solutions). So now, keep everything as it is and move data=new... to the end.

Could you please help me to fix the code?
# Define a Java class definition called Reservation which includes the following:

* attributes:
o name (first and last names) of the person making the reservation
o address
o license plate number
o arrival date
o number of days for the reservation
o type of camping site (serviced or unserviced)

* constructor(s), accessor, mutator and toString methods


# Create and initialize a 1-D array of at least 12 reservation objects. Input the reservation data from a text file.

the question here.
my program cannot read the data from the text file.
thanks very much,

Well now the problem is that f, l, a, and the rest of the variables don't exist when you call the constructor (I think there was a misunderstanding regarding my last post, I was referring to two different solutions). So now, keep everything as it is and move data=new... to the end.

This article has been dead for over six months. Start a new discussion instead.