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

    Default null pointer help

    Could anyone tell me why i might be getting a nullpointerexception on the last line here:



    public String searchEmp(int empID){

    int search = NOT_FOUND;

    if(isEmpty())
    System.out.println("Employee Not Found");

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

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

    }
    return employees[search].toString()

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default empty == null?

    Java Code:
    if(isEmpty())
    What is empty? isEmpty is a method... what object is it checking to be empty? Maybe the employee array? (hint,hint)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

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

    Default

    my program is reading from a file, im trying to use this search method to search for an employee in a file but reading my file into my array but I am having trouble reading the file to the array so i can search for the employee. Any suggestions?

    also isEmpty is a method in my program that is returning my counter of employees return(counter==0);

    Any help would be great, thanks.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    OK... I thought that isEmpty() was a method of an array. So what does isEmpty() return? an int or a boolean? As used in the "if" statement it has to return a boolean. Otherwise, if it returns an int, then the "if" statement has to change to:
    Java Code:
    if(isEmpty()==0)
    As for suggestions, it appears that that you don't read very well. In your other post, there were various suggestions on how to read lines from a file and put in an array (including one from me):
    file/ array question

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

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

    Default

    yeah I did read your post about the reading the file into an array and I want to use the scanner method as you did but i beleive you were adding it to an arrayList and we are not allowed to use a arrayList, we have to specifically read it into an array and im not sure how to go about that. I do thank you for responding and giving help though. I appreciate it.

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    OK, understood (I withdraw the comment about not reading the posts :-). Arrays and ArrayLists are very similiar. Main difference is that for an Array, you need to know the size of your array ahead of time (once the size is established you can't change it). Do you know the size of the files lines? if not, maybe you can count them in the program before creating the array.
    Also in that post I posted a link to an array tutorial which has examples. Do some of the examples to understand how to use arrays.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 07:48 PM
  2. null pointer exeption
    By mj23brm in forum New To Java
    Replies: 4
    Last Post: 02-11-2009, 12:50 AM
  3. Null Pointer Exception
    By demiser55 in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 07:33 PM
  4. null pointer exception
    By cityguy503@yahoo.com in forum New To Java
    Replies: 4
    Last Post: 08-22-2008, 08:22 PM
  5. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 06:56 AM

Posting Permissions

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