Menu

Lesson 8

2009-01-31
2013-04-09
  • Joseph Evans

    Joseph Evans - 2009-01-31

    I am working through lesson 8. At about 4:30 you use quick fix to create two methods in Book.java. The setPerson seems to work just fine however when I have quick fix create the getPerson method it adds a statement underneath,

    return null;

    After saving, BookTest shows no errors but when I test it fails and gives me a message:

    java.lang.NullPointerException

    Sorry, but I am stumped. I am using Eclipse 3.4.1.

    Book.java follows:
    package org.totalbeginner.tutorial;

    public class Book {

        public String title;
        public String author;

        public Book(String string) {
            this.title = string;
            this.author = "unknown author";
        }

        public String getAuthor() {
            return author;
        }

        public void setAuthor(String author) {
            this.author = author;
        }

        public String getTitle() {
            return title;
        }

        public void setPerson(Person p2) {
            // TODO Auto-generated method stub
           
        }

        public Person getPerson() {
            // TODO Auto-generated method stub
            return null;
        }

    }

    BookTest.java follows:

    package org.totalbeginner.tutorial;

    import junit.framework.TestCase;

    public class BookTest extends TestCase {

        public void testBook() {
            Book b1 = new Book ("Great Expectations");
            assertEquals("Great Expectations", b1.title);
            assertEquals("unknown author", b1.author);
           
        }
           
        public void testGetPerson() {
            Book b2 = new Book("War and Peace");
            Person p2 = new Person();
            p2.setName("Elvis");
           
            //method to say to whom the book is loaned
           
            b2.setPerson(p2);
            //get the name of the person who has the book
           
            Person testPerson = b2.getPerson();
            String testName = testPerson.getName();
           
            assertEquals("Elvis", testName);
        }
    }

    Thanks for looking and helping. There is nothing else like this that I have found and I am most appreciative.

    Bryant
       

     
    • Joseph Evans

      Joseph Evans - 2009-01-31

      Still not sure why it failed the JUnit test at that point when it passed on yours, however when I finished the coding for the lesson it worked fine and passed the test.

      I may be too focused on the moment to moment instructions. I'll finish the lessons before asking again.

      Thanks

       

Log in to post a comment.