import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.Arrays;
import java.util.NoSuchElementException;
public class ReservationBook
{
  private Reservation[] data;
  public Reservation[] readFile(String filename) throws IOException
{
  FileReader reader = new FileReader(filename);

    try
  {
     Scanner in = new Scanner(reader);
     boolean done = false;
     while(!done)
    {
      try
     {
       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();
       String l = lineScan.nextLine();
       String a = lineScan.nextLine();
       int lpn = lineScan.nextInt();
       String dt = lineScan.nextLine();
       int nd = lineScan.nextInt();
       String tp = lineScan.nextLine();
       data[i] = new Reservation(f,l,a,lpn,dt,nd,tp);
      }
     }   
      catch(NoSuchElementException exception)
      { 
       done = true;
      }
     }
     } 
      finally
      {
       reader.close();
      }
      return data;
} 
  //sort the data into correct order using the Insertion sort
  public void Sort()
{
   for(int i =1;i<data.length;i++)
 {
   String next = data[i].getName();
   int j = i;
   while(j>0 && data[j-1].getName().compareTo(next) > 0)
  {
   data[j].getName().equals(data[j-1].getName());
   j--;
  }
   data[j].getName().equals(next);
 }
}
  public String search(String n)
{
   int low = 0;
   int high = data.length -1;
   while(low <= high)
  {
    int mid = (low+high) / 2;
    if(data[mid].getName().compareTo(n)==0)
      return "Customer info: " + data[mid].toString();
    else if(data[mid].getName().compareTo(n) < 0)
      low = mid +1;
    else
      high = mid -1;
  }
   return "Customer information not found";
}

}
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();
  boolean done = false;
  while(!done)
  {
   try
   {
    System.out.println("Please enter the file name: ");
    String filename = in.next();
    book.readFile(filename);
    book.Sort();
    System.out.println("Enter the name you want to search: ");
    String n = in.nextLine();
    book.search(n);
    done = true;
   }
  catch(FileNotFoundException exception)
  {
    System.out.println("File not found");
  }
  catch(IOException exception)
  {
    exception.printStackTrace();
  }
 }
  
  
}
}

There are some problems .
typescript:
Please enter the file name:
reservationlist
Exception in thread "main" java.lang.NullPointerException
at ReservationBook.Sort(ReservationBook.java:54)
at ReservationReader.main(ReservationReader.java:19)

reservationlist textfile:

1
ertert
sdfsdf
sdf
123
sf
123
service


THanks.

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";
}
}

Be careful! In your toString() for Reservation class, you did not initialize 'name' but use it in the method. Also, why do you need to return data after readFile()?

Somehow, your data length is greater than 1. As a result, it goes into the loop when you call Sort() and found data is a null pointer. Try to display the size of 'book' before you call Sort() to see what the actual size is?

Edited 6 Years Ago by Taywin: n/a

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