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.


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

Results 1 to 2 of 2

Thread: What is wrong with this code, please desperate

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Thanked 0 Times in 0 Posts

    Default What is wrong with this code, please desperate

    public class Site
       protected class PageNode
          protected PageNode down;
          protected PageNode across;
          protected PageNode up;
          protected Page page;
       private PageNode currentPage;
       private PageNode homePage;
       public Site()
             this.homePage=new PageNode();
             PageNode shops=addPage("Shops",this.homePage);
             PageNode products=addPage("Products",this.homePage);
          // addPage("Paisley",shops);
          // addPage("Hamilton",shops);
       //    PageNode kitchen=addPage("Kitchen",products);
       //    addPage("Bedroom",products);
       //    addPage("Kettles",kitchen);
       //    addPage("Cookers",kitchen);
       //    addPage("Toasters",kitchen);
                public PageNode addPage(String name,PageNode homePage)
                   PageNode newNode=new PageNode();
                      newNode.page=new Page(name);
                      return currentPage;
       public void displayCurrentPage()                
       PageNode currentPage=this.homePage.down;
       System.out.println("current page name: ");
       System.out.println("has links to ");
         System.out.println("list is empty");
          while (currentPage!=null)
       public void selectLink()
          // add code for step2
    public void moveUp()
          // add code for step3
      public void displaySiteMap()
          // add code for step4

    and the error is

    ----jGRASP exec: java SiteTest

    1: display current page
    2: select link
    3: move up
    4: display site map
    0: quit
    select option: 1
    current page name:
    Exception in thread "main" java.lang.NullPointerException
    at Site.displayCurrentPage(Site.java:60)
    at SiteTest.main(SiteTest.java:17)

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete

    Thanks in advance guys

  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Faversham, Kent, UK
    My Mood
    Thanked 58 Times in 54 Posts

    Default Re: What is wrong with this code, please desperate

    The 'page' in 'homePage' is null on line 60. The PageNode class can be created without any of its members being initialised, so they are null by default. If you want to access them, you should check they're not null before calling any methods on them.

    The 'Best Practice' solution to this kind of problem is to make sure the members can't be null by providing default initialisers for them, and/or a constructor that forces initialisation of them.

    If you don't know in advance what values to give the members, you could try using the Null Object pattern, where you initialise with a special instance of the object that is safe to use but does nothing.

Similar Threads

  1. Whats wrong with my code?
    By mlan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 27th, 2010, 01:42 PM
  2. [SOLVED] what is wrong for the java code
    By chuikingman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 11th, 2010, 02:00 AM
  3. What can go wrong if you replace && with & in the following code:
    By scott01 in forum Java Theory & Questions
    Replies: 4
    Last Post: February 12th, 2010, 07:47 AM
  4. I can't find out what is wrong with my code. Please Help!
    By hallor618 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 10th, 2010, 02:44 PM
  5. Generation of Palindrome number in Java
    By tina.goyal in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 26th, 2009, 08:49 AM