Results 1 to 6 of 6
  1. #1
    egrizzly is offline Member
    Join Date
    Jun 2016
    Posts
    10
    Rep Power
    0

    Default Code compiles but does not run!!!

    Hello all,

    does anybody know why the code below compiles but does not run? When I run the code it shows the error Exception in thread "main" java.lang.NullPointerException
    at BooksTestDrive.main(BooksTestDrive.java:15)


    Java Code:
    class Books
    {	
    	String title;
    	String author;	
    }
    
    class BooksTestDrive
    {
    	public static void main(String [] args)
    	{
    		Books [] myBooks = new Books[3];
    		int x = 0;
    		myBooks[0].title = "The Grapes of Java";
    		myBooks[1].title = "The Java Gatsby";
    		myBooks[2].title = "The Java Cookbook";
    		myBooks[1].author = "bob";
    		myBooks[2].author = "Ian";
    		while (x < 3)
    		{
    			System.out.print(myBooks[x].title);
    			System.out.print(" by ");
    			System.out.println(myBooks[x].author);
    			x = x + 1;
    		}
    		
    	}
    	
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Code compiles but does not run!!!

    Exception in thread "main" java.lang.NullPointerException
    at BooksTestDrive.main(BooksTestDrive.java:15)
    Find the null value on line 15 and fix it.

    Also posted at: Code Compiles But Does Not Run! - Java | Dream.In.Code
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Code compiles but does not run!!!

    You have allocated an array for three books, but you haven't filled it with three books yet ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Code compiles but does not run!!!

    It also shows that your output does not match your code. If you run the code you posted, you'd get a NullPointerException on line 13 and not 15. Please keep pasted output and code in sync.

    And say you crossposted upfront. It could have saved Jos from typing an answer which was already given on the other site.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    68
    Rep Power
    0

    Default Re: Code compiles but does not run!!!

    Try this:

    Java Code:
    class Books extends BooksTestDrive
    {   
        String title;
        String author;  
    }
     
    class BooksTestDrive
    {
        public static void main(String [] args)
        {
            Books[] myBooks = new Books[3];
    		  for(int i = 0; i < myBooks.length; i++)
    		  {
               myBooks[i] = new Books();
    	     }
            int x = 0;
            myBooks[0].title = "The Grapes of Java";
            myBooks[1].title = "The Java Gatsby";
            myBooks[2].title = "The Java Cookbook";
            myBooks[1].author = "bob";
            myBooks[2].author = "Ian";
    
            while (x < 3)
            {
                System.out.print(myBooks[x].title);
                System.out.print(" by ");
                System.out.println(myBooks[x].author);
                x = x + 1;
            }    
        }
    }
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Code compiles but does not run!!!

    @Sizzlewump: please don't spoonfeed; the OP can turn it in as if it were his/her own work and get a good grade ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. My code compiles but does not do anything
    By logonin in forum New To Java
    Replies: 5
    Last Post: 02-08-2016, 04:06 AM
  2. Code compiles and runs, but window does not pop up.
    By DrBagelBites in forum New To Java
    Replies: 6
    Last Post: 09-27-2014, 04:44 AM
  3. Code compiles but get null pointer exeption
    By jamesreno in forum Threads and Synchronization
    Replies: 17
    Last Post: 04-08-2014, 04:28 AM
  4. Code compiles but fails to run (works in NetBeans)
    By swilliams236 in forum New To Java
    Replies: 2
    Last Post: 11-07-2011, 09:05 PM
  5. Code compiles in/runs in IDE but not on UNIX system
    By Unclejunebug in forum New To Java
    Replies: 7
    Last Post: 10-08-2009, 08:35 AM

Posting Permissions

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