Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By jashburn

Thread: Need Help With Programming

  1. #1
    Xeyxl is offline Member
    Join Date
    May 2014
    Posts
    5
    Rep Power
    0

    Default Need Help With Programming

    I came across something while I was programming and I can't seem to see the problem whatsoever. I was wondering if anyone could help me

    Java Code:
    public class Book {
    
    	private int bookIDNum;
    	private String authorName, bookName;
    	private boolean checkedOut;
    	
    	public Book (String author, String book, int bookID)
    	{
    		authorName = author;
    		bookIDNum = bookID;
    		bookName = book;
    	}
    	
    	public void checkOutBook()
    	{
    		checkedOut = true;
    	}
    	
    	public void turnInBook()
    	{
    		checkedOut = false;
    	}
    	
    	public void setAuthor(String author)
    	{
    		authorName = author;
    	}
    	
    	public void setBookName(String book)
    	{
    		bookName = book;
    	}
    	
    	public void setBookID(int ID)
    	{
    		bookIDNum = ID;
    	}
    	
    	public String getAuthor()
    	{
    		return authorName;
    	}
    	
    	public int getBookID()
    	{
    		return bookIDNum;
    	}
    	
    	public String getBookName()
    	{
    		return bookName;
    	}
    	
    	public boolean isCheckedOut()
    	{
    		return checkedOut;
    	}
    	
    	public String toString()
    	{
    		return 	"The name of your book is: " + bookName + 
    				"\nThe author of this book is: " + authorName +
    				"\nThe book's ID number is: " + bookIDNum +
    				"\nIs the book checked out: " + checkedOut;
    	}
    }
    Java Code:
    public class Library {
    	
    	private Book [] library;
    	
    	public Library (int bookCount)
    	{
    		library = new Book [bookCount];
    	}
    	
    	public void setNameOfBook (int bookNumber, String bookName)
    	{
    		library[bookNumber].setBookName(bookName);
    	}
    	
    	public void setIDOfBook (int bookNumber, int bookIDNum)
    	{
    		library[bookNumber].setBookID(bookIDNum);
    	}
    	
    	public void setAuthorOfBook (int bookNumber, String authorName)
    	{
    		library[bookNumber].setAuthor(authorName);
    	}
    	
    	public void checkOutThisBook(int bookNumber)
    	{
    		library[bookNumber].checkOutBook();
    	}
    	
    	public void turnInThisBook(int bookNumber)
    	{
    		library[bookNumber].turnInBook();
    	}
    	
    	public String getInfoOnThisBook(int bookNumber)
    	{
    		return library[bookNumber].toString();
    	}
    }
    Java Code:
    public class LibraryTest {
    
    	public static void main (String [] args)
    	{
    		Library john = new Library(1);
    		
    		john.setNameOfBook(0, "John's Book");
    		john.setAuthorOfBook(0, "John");
    		john.setIDOfBook(0, 32155);
    	}
    }
    and this is the error I'm getting

    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at Library.setNameOfBook(Library.java:13)
    	at LibraryTest.main(LibraryTest.java:8)
    which is referring to the first (and will refer to the rest) of these

    Java Code:
    public void setNameOfBook (int bookNumber, String bookName)
    	{
    		library[bookNumber].setBookName(bookName);
    	}
    	
    	public void setIDOfBook (int bookNumber, int bookIDNum)
    	{
    		library[bookNumber].setBookID(bookIDNum);
    	}
    	
    	public void setAuthorOfBook (int bookNumber, String authorName)
    	{
    		library[bookNumber].setAuthor(authorName);
    	}
    in the class Library, and these

    Java Code:
    john.setNameOfBook(0, "John's Book");
    		john.setAuthorOfBook(0, "John");
    		john.setIDOfBook(0, 32155);
    in the class LibraryTest.

    I'm wondering if anyone can find out what's wrong, and if you can, please respond :)

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Need Help With Programming

    I can't seem to see the problem
    What problem? Please explain why you think there is a problem.

    Whoops, just found this buried in a code section:
    Exception in thread "main" java.lang.NullPointerException
    at Library.setNameOfBook(Library.java:13)
    There is a null value when line 13 is executed. Find the variable with the null value on line 13 and then back track to see why it does not have a valid value.

    Note on defining and using an array of objects. There are two steps:
    1) define the slots to hold objects
    2) assign objects to the slots <<<< This is often missed
    Last edited by Norm; 05-05-2014 at 11:36 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Xeyxl is offline Member
    Join Date
    May 2014
    Posts
    5
    Rep Power
    0

    Default Re: Need Help With Programming

    The thing is, I did define the objects. However, I'm only trying to alter one. The problem being found

    public void setNameOfBook (int bookNumber, String bookName)
    {
    library[bookNumber].setBookName(bookName);
    }

    at that line of code in Library.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Need Help With Programming

    I did define the objects.
    Could you post the line number and the code where the slots in the array are assigned objects.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Xeyxl is offline Member
    Join Date
    May 2014
    Posts
    5
    Rep Power
    0

    Default Re: Need Help With Programming

    I thought I did, can you help me with that? This is something I was trying to do completely on my own, but I might lack the proper knowledge to do so.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Need Help With Programming

    Use the new statement to create an instance of the object that is to be stored in the array
    use an assignment statement to assign that object to a slot in the array.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Xeyxl is offline Member
    Join Date
    May 2014
    Posts
    5
    Rep Power
    0

    Default Re: Need Help With Programming

    Java Code:
    public class Library {
         
        private Book [] library;
         
        public Library (int bookCount)
        {
            library = new Book [bookCount];
        }
    This wouldn't cut it?

  8. #8
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Need Help With Programming

    Norm pretty much summed up the problem in post #2:
    Quote Originally Posted by Norm View Post
    Note on defining and using an array of objects. There are two steps:
    1) define the slots to hold objects
    2) assign objects to the slots <<<< This is often missed
    To diagnose the NullPointerException, break up the code in the body of the setNameOfBook(int bookNumber, String bookName) method into its constituents, and print out each part. E.g.,
    Java Code:
    public void setNameOfBook(int bookNumber, String bookName) {
        Book book = library[bookNumber];
        System.out.println("In setNameOfBook: library[" + bookNumber + "] = " + book);
    
        book.setBookName(bookName);
    
    //	library[bookNumber].setBookName(bookName);
    }
    You'd find that book is a null. To investigate further, print out the library array in the Library(int bookCount) constructor, e.g.,
    Java Code:
    public Library(int bookCount) {
        library = new Book[bookCount];
        
        for (int i = 0; i < library.length; i++) {
            System.out.println("In Library: library[" + i + "] = " + library[i]);
        }
    }
    Basically you declared and created the library array, but you did not initialise the objects in the array. Take a look at Javanotes 6.0, Section 7.1 -- Creating and Using Arrays, under the "7.1.2 Using Arrays" section. Quoting from the text,
    In Java, a newly created array is always filled with a known, default value: zero for numbers, false for boolean, the character with Unicode number zero for char, and null for objects.
    Xeyxl likes this.

  9. #9
    Xeyxl is offline Member
    Join Date
    May 2014
    Posts
    5
    Rep Power
    0

    Default Re: Need Help With Programming

    Thank you. I appreciate the help from you, and from Norm.

Similar Threads

  1. Replies: 1
    Last Post: 04-20-2013, 09:16 PM
  2. Replies: 1
    Last Post: 04-20-2013, 09:15 PM
  3. android programming vs game programming using java
    By vgoel38 in forum Forum Lobby
    Replies: 2
    Last Post: 09-08-2012, 12:11 AM
  4. GUI Programming Help
    By sirwiggles in forum New To Java
    Replies: 4
    Last Post: 04-28-2009, 04:53 AM

Tags for this Thread

Posting Permissions

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