Results 1 to 3 of 3
  1. #1
    mayhewj7 is offline Member
    Join Date
    Dec 2008
    Posts
    39
    Rep Power
    0

    Default NullPointerException I NEED HELP

    Could someone pleaaaase help with figure out why i get a null pointer exception. I can not figure it out. PLLEAASEE HELLPP. I have attached the code and highlighted the areas that give me the nullpointerexception. I dont know what is wrong. I NEED HELP FAST. Thanks so much. I get the nullpointed exception everything i run a method from the interface.

    import java.io.*;
    import java.util.*;
    public class collectEmployeeInfo
    {
    private int counter;
    private Employee[] employees = new Employee[10];
    private final int DEFAULT_CAPACITY = 10;
    private final int NOT_FOUND = 1;
    private Scanner sc = new Scanner(System.in);
    GregorianCalendar gc;
    int month, day, year;
    String name;
    public int find;





    public void readFile(){

    FileInputStream read;
    DataInputStream in;
    BufferedReader br;
    String name;
    int ID;
    int month;
    int day;
    int year;
    GregorianCalendar cal;
    try{

    read = new FileInputStream("H://employeefile.dat");
    in = new DataInputStream(read);
    br = new BufferedReader(new InputStreamReader(in));
    String fileRead;


    while((fileRead = br.readLine()) !=null){
    Scanner scan = new Scanner(br.readLine());
    name = scan.next();
    ID = scan.nextInt();
    month = scan.nextInt();
    day = scan.nextInt();
    year = scan.nextInt();
    cal = new GregorianCalendar(month,day,year);
    Employee radford = new Employee(name, ID, cal);

    employees[counter] = radford;
    counter++;
    //scan = new Scanner(br.readLine());
    System.out.println(read.toString());
    }
    in.close();
    }
    catch (IOException e){
    System.out.println("File Not Found");
    }


    }

    public String searchEmp(int find){

    int search = NOT_FOUND;

    try{
    if(isEmpty())
    throw new EmptyStackException();

    for(int i = 0; i < counter && search == NOT_FOUND; i++){
    if(employees[i].getID() == find){

    search = i;
    System.out.println(employees[search]);
    }

    System.out.println("Employee not found");
    }
    }

    catch (EmptyStackException a){
    System.out.println("Empty");
    }
    try{
    if(search == NOT_FOUND)
    throw new NoSuchElementException();
    }
    catch(NoSuchElementException a){
    System.out.println("Not Found");
    }

    return employees[search].toString();
    }

    public int size(){
    return counter;
    }
    public boolean isEmpty(){
    return(counter==0);
    }


    public boolean contains(Employee avail){
    int search = NOT_FOUND;

    for(int i = 0; i < counter && search == NOT_FOUND; i++)
    if(employees[i].equals(avail))
    search = i;

    return (search != NOT_FOUND);
    }

    public void add(String name, int find, GregorianCalendar calendar)
    {
    employees[counter] = new Employee(name, find, calendar);
    counter++;
    }

    /**if(!(contains(newEmployee))){
    if(size() == employees.length)
    expandCapacity();

    employees[counter] = newEmployee;
    counter++;
    }
    }*/

    public void expandCapacity(){
    Employee[] bigger = new Employee[employees.length*2];

    for(int i = 0; i < employees.length; i++)
    bigger[i] = employees[i];

    employees = bigger;
    }

    public Employee remove(int find) throws EmptyStackException,
    NoSuchElementException
    {

    int search = NOT_FOUND;

    if(isEmpty())
    throw new EmptyStackException();

    for( int i = 0; i < counter && search == NOT_FOUND; i++)
    {
    if(employees[i].getID() == find)
    {
    search = i;
    System.out.println("The employee has been deleted!");
    }
    }

    if(search == NOT_FOUND)
    throw new NoSuchElementException();

    Employee result = employees[search];
    employees[search] = employees[counter-1];
    employees[counter-1] = null;
    counter--;

    return result;
    }
    public String listEmployees(){
    String list = "";
    for(int i = 0; counter > i; i++){
    if(employees[i] !=null){
    list = list + employees[i].toString() + "\n";

    }
    }
    return list;
    }

    public collectEmployeeInfo(){
    GregorianCalendar calendar;
    int month;
    int day;
    int year;
    int find;
    String name;
    employees = new Employee[10];
    counter = 0;
    try{
    Scanner radford = new Scanner(new File("h://employeefile.dat"));
    while(radford.hasNextLine());
    {
    if(counter == employees.length)
    expandCapacity();

    name = radford.next() + radford.next() + radford.next();
    find = radford.nextInt();
    month = radford.nextInt();
    day = radford.nextInt();
    year = radford.nextInt();
    calendar = new GregorianCalendar(month, day, year);
    employees[counter] = new Employee(name, find, calendar);
    counter++;
    }
    radford.close();
    }
    catch (IOException a){
    System.out.println("Not Found");
    }
    }




    public void exitProgram(){

    try{
    String ex = "";
    BufferedWriter bw = new BufferedWriter(new FileWriter(new File("H://employeefile.dat")));

    for( int i = 0; i < counter; i++){
    bw.write(ex + employees[i]);
    bw.newLine();
    }
    bw.close();
    }


    catch (IOException e){
    System.err.println(e);
    }
    System.out.println("Thank You");
    System.exit(0);
    }

    public String toString(){

    String result = "";
    for(int i = 0; i < counter; i++)
    result = result + employees[i].toString() + "\n";

    return result;
    }
    }









    import java.util.*;
    import java.io.*;
    public class Interface{

    private Scanner sc = new Scanner(System.in);
    private int option;
    private String name;
    private String first;
    private String last;
    private int month;
    private int day;
    private int year;
    //collectEmployeeInfo bacon = new collectEmployeeInfo();
    private boolean stop = false;
    GregorianCalendar calendar;
    //Employee radford = new Employee("", 0, calendar);

    private int find;
    private collectEmployeeInfo employees;



    public void begin(){


    System.out.println("This is the Employee Program: " + "\n");
    System.out.println("Press 1 to search for an employee");
    System.out.println("Press 2 to add an employee");
    System.out.println("Press 3 to delete an employee");
    System.out.println("Press 4 to list all employees");
    System.out.println("Press 5 to exit the program" + "\n");
    option = sc.nextInt();
    if(option == 1){
    this.beginSearch();
    }
    else if(option == 2){
    this.addEmp();
    }
    else if(option == 3){
    this.deleteEmp();
    }
    else if(option == 4){
    this.listEmps();
    }

    else if(option == 5){
    this.exitSys();
    }
    else{
    System.out.println("Invalid Option");
    }


    }

    public void start(){
    employees = new collectEmployeeInfo();
    this.begin();
    }

    public void beginSearch() {
    //int someNumber = 0;
    System.out.println("Enter employeeID: ");
    find = sc.nextInt();
    employees.searchEmp(find); System.out.println(find);
    this.begin();

    }

    public void addEmp(){


    System.out.println("Enter employees first name: ");
    String first = sc.next();
    System.out.println("Enter employees last name: ");
    String last = sc.next();
    name = first + last;
    System.out.println("Enter new employee ID: ");
    find = sc.nextInt();
    System.out.println("Enter employees hire date: ");
    month = sc.nextInt();
    day = sc.nextInt();
    year = sc.nextInt();
    calendar = new GregorianCalendar(month, day, year);
    employees.add(name, find, calendar);
    this.begin();


    }

    public void deleteEmp(){
    Scanner delete = new Scanner(System.in);
    System.out.println("Enter a employee to delete: ");
    find = sc.nextInt();
    employees.remove(find);
    this.begin();
    }

    public void listEmps(){
    Scanner list = new Scanner(System.in);
    employees.listEmployees(); System.out.println();
    this.begin();
    }


    public void exitSys(){
    employees.exitProgram(); System.exit(0);
    }


    }

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    looks like you never initialize your employees variable.

  3. #3
    mayhewj7 is offline Member
    Join Date
    Dec 2008
    Posts
    39
    Rep Power
    0

Similar Threads

  1. I get a NullPointerException and don't know why
    By hendrix79 in forum New To Java
    Replies: 9
    Last Post: 12-14-2008, 06:18 AM
  2. NullPointerException
    By Aika in forum New To Java
    Replies: 8
    Last Post: 11-18-2008, 11:34 PM
  3. NullPointerException
    By adeeb in forum AWT / Swing
    Replies: 3
    Last Post: 06-11-2008, 08:42 AM
  4. NullPointerException
    By mensa in forum Java 2D
    Replies: 5
    Last Post: 05-03-2008, 11:19 PM
  5. NullPointerException
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 04:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •