Results 1 to 4 of 4
  1. #1
    maans88 is offline Member
    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Default help with array of objects of class

    Hey guys, i have two classes,
    I'm gettting this error when I run "Agent" class:
    12
    Exception in thread "main" java.lang.NullPointerException
    at Assignment2.Agent.main(Agent.java:18)

    Not really sure what to do, I've been trying to get it to work for like 3 hours now.
    thanks in advance.


    --------------------------
    Java Code:
    package Assignment2;
    
    import java.util.Scanner;
    import java.io.InputStream;
    import java.io.FileNotFoundException;
    
    public class Agent {
    
    	private int NumberOfHouses;
    	
    	public static void main(String[] args){
    		House test = new House();
    		House[] allHouses = new House[10];
    		
    		
    		test.setNumberOfRooms(12);
    		System.out.println(test.getNumberOfRooms());
    		allHouses[2].setNumberOfRooms(9);
    	}
    }
    --------------------------
    Java Code:
    package Assignment2;
    public class House {
    	
    	private int NumberOfRooms = 0, LivingArea, TotalLotArea;
    	private Boolean Status; 
    	private long Price; 
    	static int test;
    	
    	public void setNumberOfRooms(int num){
    	    NumberOfRooms = num;
    	}
    	public void setLivingArea(int num){
                LivingArea = num;
    	}
    	public void setTotalLotArea(int num){
    	    TotalLotArea = num;
    	}
    	public void setPrice(long num){
    	    Price = num;
            }
    	public int getNumberOfRooms(){
    	    return NumberOfRooms;
    	}	
    }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: help with array of objects of class

    You have allocated an array to hold House objects but you have not assigned any to the array.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    maans88 is offline Member
    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Default Re: help with array of objects of class

    Quote Originally Posted by jim829 View Post
    You have allocated an array to hold House objects but you have not assigned any to the array.

    Regards,
    Jim
    Im not really sure how to do so...

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: help with array of objects of class

    Here's one way.

    Java Code:
    House[] allHouses = new House[10];
    for (int j = 0; j < allHouses.length; j++) {
         allHouses[j] = new House();
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. array of class objects
    By ravi1912 in forum New To Java
    Replies: 1
    Last Post: 08-08-2011, 09:58 AM
  2. How to convert array of Objects into array of Strings
    By elenora in forum Advanced Java
    Replies: 1
    Last Post: 06-10-2011, 03:48 PM
  3. An Array of Objects (Class Initialization)
    By Chris_C in forum New To Java
    Replies: 9
    Last Post: 02-09-2011, 05:49 AM
  4. Replies: 1
    Last Post: 01-03-2010, 09:48 PM

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
  •