Results 1 to 4 of 4

Thread: Problem with null variable

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Problem with null variable

    Okay for a project at school I'm writing a Library class and a book class. So in the library class I search for a book at a certain time, when its found I want to return just that book and access some methods from it.

    Code:
    book myBook = bookSearcher.searchForTitle("The Lorax"); // searches for a title and returns an object of book class
    System.out.println(myBook.getAuthor());
    The getAuthor code never gets called, neither does searchForTitle. I don't understand what is going on. How can I set myBook to a returned book variable?

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Problem with null variable

    What's the searchForTitle code look like? Your method probably isn't returning anything, so the problem will be there. You are returning a 'book' from searchForTitle, right?

  3. #3

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Problem with null variable

    Quote Originally Posted by System_Error
    What's the searchForTitle code look like? Your method probably isn't returning anything, so the problem will be there. You are returning a 'book' from searchForTitle, right?
    It's something like

    Code:
    public Book searchForTitle(String sTitle){
    for(Book b: books){//arraylist of books
        if(b.getTitle().equals(sTitle){
            return b;
        }
    }
    return null;
    }
    Now reading that it appears to me why this is happening. Excuse any small coding mistakes if there are, no Java here. I think no books are equal to my title and I put null right there. I just thought the code wasn't executing.

  4. #4
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Problem with null variable

    Are you still needing help with this, or do you have it covered?

Posting Permissions

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



Click Here to Expand Forum to Full Width