Results 1 to 3 of 3
  1. #1
    leapinlizard is offline Member
    Join Date
    Apr 2009
    Posts
    19
    Rep Power
    0

    Default [SOLVED] Null exception

    I'm getting error in here
    it compiles but when i run ..

    it says line 48: which is following line: null exception error
    System.out.printf(bookArray[j].toString());


    Java Code:
    public class BookTest
    {
    	public static void main(String args[])
    
    	{
                   Book bookArray[] = new Book[6]; 
    		
                    
    	 	String dataArray [][] = {{"Java","Davis","123","Wiley","London","25.50","Book"},
    					 {"Where's my car", "Aston Kutcher","777","ORiely","Dallas","5.95","Fiction","123","456"},
    					 {"Earthquakes","ChuckBerry","435","LABooks","LA","75.00","NonFiction"},
    					 {"C++","Jones","456","Random","NY","10.75","Book"},
    					 {"Doom","The Rock","918","Sans Publishing","LA","12.50","Fiction","1233","2323"}, 
    					 {"Universal Studios","Walt","987","Dell","Houston","29.90","NonFiction"}};
     
    
                   for (int i=0; i<bookArray.length; i++)
                    {
                    	
     			if (dataArray[i][6].equals("Fiction"))
    			
    			  bookArray[i]=new Fiction (dataArray[i][0],dataArray[i][1], Integer.parseInt(dataArray[i][2]), new Publisher(dataArray[i][3],dataArray[i][4]), Double.parseDouble(dataArray[i][5]),i, new Background(dataArray[i][7], dataArray[i][8]));
                            
    
                             
    			if (dataArray[i][6].equals("NonFiction"))
                                bookArray[i]= new NonFiction(dataArray[i][0],dataArray[i][1], Integer.parseInt(dataArray[i][2]), new Publisher(dataArray[i][3],dataArray[i][4]), Double.parseDouble(dataArray[i][5]),"History");
    			
                                                           
    	           		
    		}// end of FOR Loop
    
    
                
             	
                    for (int j=0; j<6;j++)
                        {
                       
                 	    System.out.println("hello");
    		    System.out.println();
    
    		    
    			System.out.printf(bookArray[j].toString());
    			
                        } //end of for with j  
    
    	               
           } //end of main
    } //end class BookTest

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    Java Code:
    	if (dataArray[i][6].equals("Fiction"))
    		bookArray[i]=new Fiction (dataArray[i][0],dataArray[i][1], Integer.parseInt(dataArray[i][2]), new Publisher(dataArray[i][3],dataArray[i][4]), Double.parseDouble(dataArray[i][5]),i, new Background(dataArray[i][7], dataArray[i][8]));
                    
    	if (dataArray[i][6].equals("NonFiction"))
                    bookArray[i]= new NonFiction(dataArray[
    you only new object for Fiction, nonFiction
    then... dataArray[i][6] ="book" will not assign to any object, so is null
    call method from null will throw npe

  3. #3
    leapinlizard is offline Member
    Join Date
    Apr 2009
    Posts
    19
    Rep Power
    0

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 07:48 PM
  2. Null pointer exception
    By Stephenmak in forum New To Java
    Replies: 5
    Last Post: 04-01-2009, 03:17 PM
  3. null pointer exception
    By jyothi.priyanka in forum New To Java
    Replies: 12
    Last Post: 03-11-2009, 06:04 PM
  4. Null Pointer Exception
    By andre1011 in forum Advanced Java
    Replies: 4
    Last Post: 02-07-2009, 04:30 AM
  5. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 06:56 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
  •