Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: java.lang.nullpointer exception in beginner code

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java.lang.nullpointer exception in beginner code

    Hi there writing a small program for borrowing and lending books, the code compiles but alot of the time when i run this code i get "java.lang.NullPointerException" and the program stops working.

    in this particular instance its relating to these lines of codes:

    public void displayBooks(){
    for (int i=0;i<books.length;i++){
    System.out.println("code: " + books[i].getCode());
    System.out.println("Title: " + books[i].getTitle());
    if (books[i].getStatus() == null){
    System.out.println("Status: Available");
    }else{
    System.out.println("Return date: " + books[i].getReturnDate());
    }
    System.out.println("************************");
    }
    }

    i get the following :

    java.lang.NullPointerException
    at Library.displayBooks(Library.java:76)
    at AssignmentTwo.start(AssignmentTwo.java:28)
    at Application.main(Application.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:271)

    In this same code I cannot also figure out how to make i so it displays the main menu again after executing this code.

    Any help would be much appreciated.

    Thanks.


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: java.lang.nullpointer exception in beginner code

    Hello Bobba!
    Which line is Library.displayBooks(Library.java:76)? You should print the variables in that line to find out which is null.
    Until you provide us with further information, I am only guessing that you have not initialized the books[] array.

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: java.lang.nullpointer exception in beginner code

    try using a try/catch statement here:

     
    if (books[i].getStatus() == null){
    System.out.println("Status: Available");
    }else{
    System.out.println("Return date: " + books[i].getReturnDate());
    }

    catch a NullPointerException for it.

  4. #4
    Junior Member
    Join Date
    May 2012
    Posts
    3
    My Mood
    Cool
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.nullpointer exception in beginner code

    hii,
    please insert try/catch block and and insert
    catch(Exception e)
    {System.out.println( e);}
    then the code will work fine..
    Reagrds,
    Devesh

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: java.lang.nullpointer exception in beginner code

    Quote Originally Posted by aesguitar
    catch a NullPointerException
    Quote Originally Posted by deveshinjiit@gmail.com
    catch(Exception e)
    {System.out.println( e);}
    then the code will work fine..
    Sorry, but this is bad advice. Catching it is like treating the symptoms of a disease when you can treat the disease itself - and will almost always result in a bug in the code. Figure out why it is being thrown and treat the disease, as andreas90 alluded to in post #2.

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.nullpointer exception in beginner code

    hello sorry i havent been able to get back to this, been working.

    public void displayBooks(){
        for (int i=0;i<books.length;i++){
          System.out.println("Code: " + books[i].getCode());    <---------------------------- 99 is this line here
          System.out.println("Title: " + books[i].getTitle());
          if (books[i].getStatus() == null){
            System.out.println("Status: Available");
          }else{  
            System.out.println("Return date: " + books[i].getReturnDate());  
          }
            System.out.println("************************");
        }
      }
     
    java.lang.NullPointerException
    	at Library.displayBooks(Library.java:99)
    	at AssignmentTwo.start(AssignmentTwo.java:28)
    	at Application.main(Application.java:11)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:601)
    	at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)
    its a different line because I have been doing more work on it.

    It also happens here:

     public void searchByTitle(){
        System.out.println("Please enter the book title that you would like to search: ");
        String userSelection = Keyboard.readInput();
        BookItem book = findTitle(userSelection);
        for(int i=0;i<books.length;i++){
          if (books[i].getTitle() == book.getTitle()){   <--------------------------------------------------- line 78
            System.out.println("Record found in the collection: ");
            System.out.println("Code: " + books[i].getCode());  
            System.out.println("Title: " + books[i].getTitle());
            if (books[i].getStatus() == null){
              System.out.println("Status: Available");
            }else{
              System.out.println("Status: " + books[i].getStatus());
            }if (books[i].getReturnDate() == null){                        
              System.out.println("Return Date: N/A");
            }else{
              System.out.println("Return Date: " + books[i].getReturnDate());  
            }
          }else if (book == null){
            System.out.println("Sorry, this book is not in the collection.");
            i = 1001;
          }
        }
      }
     
    java.lang.NullPointerException
    	at Library.searchByTitle(Library.java:78)
    	at AssignmentTwo.lending(AssignmentTwo.java:52)
    	at AssignmentTwo.start(AssignmentTwo.java:25)
    	at Application.main(Application.java:11)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:601)
    	at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)
    thanks again help is always appreciated
    >
    Last edited by Bobba; May 6th, 2012 at 06:47 PM.

  7. #7
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: java.lang.nullpointer exception in beginner code

    Quote Originally Posted by Bobba View Post
    hello sorry i havent been able to get back to this, been working.
    i have edited the array slightly but still the same error, it works correctly but still shows the following issue,
    Are you getting an "java.lang.NullPointerException"? Did you try what I suggested in post#2?
    Where do you define and initialize your books array?

  8. #8
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: java.lang.nullpointer exception in beginner code

    As I told in my other two posts, you have to initialize an array before you use it. Assuming that you haven't initialized your array (if you do initialize it somewhere else please post here the code), the exception is thrown because you are trying to access an index of the array (books[i]) that has an invalid value (null).
    And please wrap your code with code tags.

Similar Threads

  1. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  2. [SOLVED] Nullpointer exception when sending string
    By treshr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 26th, 2011, 04:36 AM
  3. Replies: 8
    Last Post: August 9th, 2011, 08:25 PM
  4. Replies: 6
    Last Post: March 25th, 2011, 03:42 PM
  5. exception in thread main java.lang.Nullpointerexception
    By westandeast in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 6th, 2011, 09:08 AM