How can I can solve my NullPointerException problem?

Hi all,
So I'm playing around with some code and was wondering why I get the following?
Exception in thread "main" java.lang.NullPointerException
     at lecture_examples.Library.searchForTitle(Library.java:38)
     at lecture_examples.LibraryManager.main(LibraryManager.java:24)The problem seems to occur when I call searchForTitle() in the library class. Here is my code:
package lecture_examples;
public class LibraryManager
     public static void main(String []args)
          Library lib = new Library(150);
          Book book1 = new Book("Africa", "Ray Mears", "1111-1111-1111-1", "Penguin Books", 2007);
          Book book2 = new Book("North America", "Ray Mears", "2222-2222-2222-2", "Penguin Books", 2007);
          Book book3 = new Book("South America", "Ray Mears", "3333-3333-3333-3", "Penguin Books", 2007);
          Book book4 = new Book("Europe", "Ray Mears", "4444-4444-4444-4444-4", "Penguin Books", 2007);
          Book book5 = new Book("Asia", "Ray Mears", "5555-5555-5555-5555-5", "Penguin Books", 2007);
          Book book6 = new Book("Australasia", "Ray Mears", "6666-6666-6666-6666-6", "Penguin Books", 2007);
          Book book7 = new Book("India", "Ray Mears", "7777-7777-7777-7", "Penguin Books", 2007);
          Book book8 = new Book("South Pacific", "Ray Mears", "8888-8888-8888-8", "Penguin Books", 2007);
          lib.addBook(book1);
          lib.addBook(book2);
          lib.addBook(book3);
          lib.addBook(book4);
          lib.addBook(book5);
          lib.addBook(book6);
          lib.addBook(book7);
          lib.addBook(book8);
          lib.searchForTitle("India");
     //other static methods used by main
package lecture_examples;
import java.util.Vector;
public class Library
     private int currSize = 0;
     private Book[] books;
     public Library()
          books=new Book[1000];
     public Library(int maxSize)
          books=new Book[maxSize];
     public void addBook(Book newBook)
          if(currSize<books.length) //if there the book array is not full
               books[currSize++]=newBook;//Puts the newBook into an element of book array
          else
               System.out.println("Your Library is Full");//Output error message
     public Book[] searchForTitle(String aTitle)
          Vector v=new Vector();//create an expandable Vector called v.
          for(int i=0;i<books.length;i++)//loops through the array of books
               if(aTitle.equals(books.getTitle()))//compares the searched Title with each title stored in each element of the book array
                    v.add(books[i]);//if the book lexigraphically matches a book in the book array it adds the existing book to the vector
          Book[] results=new Book[v.size()];//creates an book-result array which is the same size as the vector
          for(int i=0;i<v.size();i++)
               results[i]=(Book)(v.elementAt(i)); //loops through the results vector and casts them as books into the book array
          return results;     //returns results     
     public void removeBook(String isbn)
          int index=getIndex(isbn);//gets the index of isbn and assigns it to the index integer variable
          for (int i=index;i<currSize-1;i++) //loops through the books
               books[i]=books[i+1]; //moves each book up one element in the book array
          currSize--;//decrements the number of books in the library
     public int getIndex(String isbn)
          int index = 0;
          for(int i=0;i<currSize;i++)//loops through the number of books
               if(isbn.equals(books[i].getIsbn()))//If the isbn is in the book array
                    index = i; //assign index to i
          return index;
package lecture_examples;
public class Book
     private String title = "Invalid";
     private String author = "Invalid";
     private String isbn = "Invalid";
     private String publisher = "Invalid";
     private int publicationYear = 0000;
     public Book(String title, String author, String isbn, String publisher, int publicationYear)
          this.title = title;
          this.author = author;
          this.isbn = isbn;
          this.publisher = publisher;
          this.publicationYear = publicationYear;          
     public String getTitle()
          return title;
     public String getAuthor()
          return author;
     public String getIsbn()
          return isbn;
     public String getPublisher()
          return publisher;
     public int getPublicationYear()
          return publicationYear;
     public void setTitle(String newTitle)
          title = newTitle;
     public void setAuthor(String newAuthor)
          author = newAuthor;
     public void setisbn(String newisbn)
          isbn = newisbn;
     public void setPublisher(String newPublisher)
          publisher = newPublisher;
     public void setPublicationYear(int newPublicationYear)
          publicationYear = newPublicationYear;
     public String toString()
          String fullDetails = "\nTitle = \t" + title
          + "\nAuthor = \t" + author
          + "\nISBN = \t\t" + isbn
          + "\nPublisher = \t" + publisher
          + "\nYear = \t\t" + publicationYear;
          System.out.println(fullDetails);
          return fullDetails;

>
Exception in thread "main" java.lang.NullPointerException
     at lecture_examples.Library.searchForTitle(Library.java:38)
     at lecture_examples.LibraryManager.main(LibraryManager.java:24)Simple, line #38 in your Library.java in giving you NullPointerException. In the constructor of your Library.java, you are creating books array of size 1000, which will be initialized to an Books array with 1000 null values. Are you having a reference to a Book object at all 1000 array positions? If no, then in your searchForTitle() method you are iterating through the entire books array using a for loop and at every array object you are calling books.getTitle(). But, if your array has null values then it will throw NullPointerException. So do something like this...
if(books!=null && aTitle.equals(books[i].getTitle()))//compares the searched Title with each title stored in each element of the book array
                    v.add(books[i]);//if the book lexigraphically matches a book in the book array it adds the existing book to the vector

Similar Messages

  • I have a problem in my iphone 4 with wi-fi after update to IOS6 please can help my how can solve this problem?

    I have a problem in my iphone 4 with wi-fi after update to IOS6 please can help my how can solve this problem?

    Nope
    One needs to press the home and sleep / wake keys together for the phone to reset
    The other thing I could recommend
    Let the battery run out and the phone completely "die"
    It may take a day or two with it not being used
    Then plug it in to charge and see if there is any change in behavior
    If you cannot get this to work - you may need to bring it to an Apple store

  • When i go to new sms typing their is always show a text, how can solve this problem?

    when i go to new sms typing their is always show a text, how can solve this problem?

    Delete them and then download them from the desired Apple ID. This may require repurchasing paid applications.
    (95675)

  • Hi my iphone 4 is disabled how can solve this problem? help me please

    hi my iphone 4 is disabled how can solve this problem? help me please

    What do you mean by "disabled"? Disabled by entering the wrong passcode? If so, you'll have to force the phone into recovery mode & restore it to get it working again & remove the passcode:
    Turn your phone off, then force it into recovery mode & restore it:
    Leave the USB cable connected to your computer, but NOT your phone, iTunes running, press & hold the home button while connecting the USB cable to your dock connector, continue holding the home button until you see “Connect to iTunes” on the screen. You may now release the home button. iTunes should now display that it has detected your phone in recovery mode, if not quit and reopen iTunes. If you still don’t see the recovery message repeat these steps again. iTunes will give you the option to restore from a backup or set up as new.

  • HT4995 My iPad 2 Location Service works fine but does not work on my home Airport router. How can I fix the problem?

    My iPad 2 Location Service works fine but does not work on my home Airport router. How can I fix the problem?

    lbryan1987 wrote:
    I dont want the button problem solved i need to know how to restore the phone without using that button or going into settings
    You don't in the condition it's in. You will either have to get the phone replaced by Apple or pay a 3rd party to repair it.
    there seriously should be more than two ways to solve this other wise apple is useless and we will never buy another apple product.
    Seriously? It's physically broken!

  • My enternet exploerer facebook page start's up by it's self how can i fix the problem?

    help. my enternet exploerer facebook page start's up by it's self how can i fix the problem?

    Hi readytogo. welcome to the HP Forums. I am not sure exactly what you mean. You say Facebook loads automatically in Internet Explorer (IE). Does this mean, when you first open IE, that Facebook opens? Or do you mean when you turn your computer on, it loads Facebook without being told to? Or are you saying that it is remembering your Facebook login information?
    What is the model number of your product? What version of Windows is installed?
    Guide to finding your product number
    Which Windows operating system am I running?
    Let me know, I look forward to your response.
    TwoPointOh
    I work on behalf of HP
    Please click “Accept as Solution ” if you feel my post solved your issue, it will help others find the solution.
    Click the “Kudos, Thumbs Up" on the bottom to say “Thanks” for helping!

  • Ajax submit failed: error = 403, forbidden----how can solve this error

    Ajax submit failed: error = 403, Forbidden--how can solve this error to create a new eprintcenter account

    I am having the SAME problem.  This is so frustrating.   If i try to log on with teh user account I already have, I get the above message.  If i try to create a new accoutn it says the account is already existing.  If i click on forgot password, nnothing happens.  HELP!  I am stuck in a loop!!

  • HT201412 Hi , my Ipod touch4 not turning on even not connecting to computer... How can i fix this problem !!!! plz

    Hi , my Ipod touch4 not turning on even not connecting to computer... How can i fix this problem !!!! plz

    lbryan1987 wrote:
    I dont want the button problem solved i need to know how to restore the phone without using that button or going into settings
    You don't in the condition it's in. You will either have to get the phone replaced by Apple or pay a 3rd party to repair it.
    there seriously should be more than two ways to solve this other wise apple is useless and we will never buy another apple product.
    Seriously? It's physically broken!

  • After our computer crashed,itunes no longer recoginizes our ipods.How can I fix this problem?

    After our computer crashed,itunes no longer recoginizes our ipods.How can I fix this problem?

    Okay, update on my previous message:
    I have replaced my iTunes-folder with a back-up version of the old one. Luckily this turned my podcasts back to normal and replaced the new 'library' with the old one.
    This also solved the problem of having to erase your iPod or iPhone, which is no longer necessary.
    So my solution is:
    1. back-up your iTunes folder (located at 'User/Music/iTunes/')
    2. remove iTunes 9.0.3 completely (http://support.apple.com/kb/HT1224)
    3. drag the iTunes folder back to the Music folder
    4. Re-install iTunes 9.0.3.
    It worked for me.

  • HT4623 how can i fix the problem on ios 6.1.3 in my bluetooth and wifi connection?

    how can i fix the problem on mi ipad mini. When i updated the ios to 6.1.3 the bluetooth and wifi didn't work properly.

    Hey maricel737
    There are two different articles that go over troubleshooting steps for solving issues with Wi-Fi and Bluetooth. This first one goes over Wi-Fi on your iPad:
    iOS: Troubleshooting Wi-Fi networks and connections
    http://support.apple.com/kb/ts1398
    This next one outlines suggestions for Bluetooth on your iPad:
    iOS: Third-party Bluetooth headsets, headphones, and keyboards
    http://support.apple.com/kb/ht1664
    On the Bluetooth one, I'd just skip ahead to the "Additional Information" section where the troubleshooting steps start.
    Best,
    David

  • On my iPad 2 I can nolonger view maps.  It says there are JavaScript errors.  How can I fix this problem?

    On my iPad 2 I can nolonger view maps.  It says there are JavaScript errors.  How can I fix this problem?

    First, try a system reset.  It cures many ills and it's quick, easy and harmless...
    Hold down the on/off switch and the Home button simultaneously until the screen blacks out or you see the Apple logo.  Ignore the "Slide to power off" text if it appears.  You will not lose any apps, data, music, movies, settings, etc.
    If the Reset doesn't work, try a Restore.  Note that it's nowhere near as quick as a Reset.  Connect via cable to the computer that you use for sync.  From iTunes, select the iPad/iPod and then select the Summary tab.  Follow directions for Restore and be sure to say "yes" to the backup.  You will be warned that all data (apps, music, movies, etc.) will be erased but, as the Restore finishes, you will be asked if you wish the contents of the backup to be copied to the iPad/iPod.  Again, say "yes."

  • HT1338 I updated my MacBook Pro with Mountain Lion and now it is starting slow and opening with some of the programs. How can I fix the problem?

    I updated my MacBook Pro with Mountain Lion and now it is slow in starting up and loads a number of probrams. How can I fix the problem?

    Have you tried starting in Safe Mode and see if the slowness still occurs?
    Restart holding the "shift" key.
    (Expect it to take longer to start this way because it runs a directory check first.)
    If this works look in System Preferences > Users & Groups > Login items and delete any third party login items (-), you can always add them back with the (+). Also look in /Library/Startup Items. Nothing is put in that folder by default, so anything in there is yours.
    Reboot normally and test.

  • HT1871 Hello i have one IPhone 4S and I use 3G net in my mobile sometime I have problem with temperature in my phone at the SIM card or battery how can I fix this problem. By update or version of 6.1.3

    Hello i have one IPhone 4S and I use 3G net in my mobile sometime I have problem with temperature in my phone at the SIM card or battery how can I fix this problem. By update or version of 6.1.3

    Could it be because the SIM card got damaged while I cut it to fit the IPhone?
    Very likely. Hard to believe the carrier in Mexico doesn't have a micro-SIM card available.
    Or somehow the Chinese carrier found out and locked it?
    No, which is not possible.
    What can I do? Get a new SIM and see if it works again?
    Get a micro-SIM from the carrier in Mexico that you don't have to manually cut down. If that carrier doesn't provide one, switch to another carrier that is not so low-budget.

  • HT1212 My ipad is disabled and says it needs to be connected to itunes, when i connect it to itunes, itunes says that i need to put my passcode in my ipad for it to recognize it, but since it is disabled, i cant do this, how can i resolve this problem?

    My ipad is disabled and it says i need to connect to itunes when i connect it to itunes, it says it wont recognize it until i put my passcode into my ipad, but with my ipad disabled, this is immposible, how can i resolve this problem?

    You may need to put the iPad into recovery mode : http://support.apple.com/kb/ht1808 - you should then be able to reset the iPad an resync your content to it

  • Regarding iMessage, when i want to send text to a person in my contacts app, i've been getting a message saying the number has not been registered with iMessage.  How can one fix this problem?

    Regarding iMessage, when i want to send text to a person in my contacts app, i've been getting a message saying the number has not been registered with iMessage.  How can one fix this problem?  It works on the iPhone.

    It takes two to tango, maybe it's your recipient that has weak Internet/Cellular connections.

Maybe you are looking for