Results 1 to 3 of 3
  1. #1
    Saletra is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default Array of objects

    Hello,

    I'm trying to make array of 5 object but not in traditional way. When want to see data from int rodzaj from class Kasa i'm getting error coused by System.out.println(kasa[0].rodzaj);

    Java Code:
    public class Bank {
    
    public static void main(String[] args) {
    
    	Kasa kasa[] = new Kasa[5];
    	Klient klient = new Klient();
    	BankManager men = new BankManager(klient, kasa);
    	men.sprawdz();
    	}
    }
    
    class BankManager extends Thread{
    
    Klient klient;
    Kasa kasa[];
    
    BankManager(Klient k, Kasa ka[]){
    	this.klient = k;
    	this.kasa = ka;
    }
    public void sprawdz(){
    	[COLOR="Red"]System.out.println(kasa[0].rodzaj);[/COLOR] //error, or System.out.println(kasa[0]); give null
    	System.out.println(klient.priorytet); //works fine 
    }
    public void run(){
    sprawdz();
    }}
    
    class Klient {
    int priorytet=2;
    
    }
    class Kasa {
    public int rodzaj=0;
    }
    Last edited by Saletra; 07-31-2010 at 10:21 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,021
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Saletra View Post
    Hello,

    I'm trying to make array of 5 object but not in traditional way. When want to see data from int rodzaj from class Kasa i'm getting error coused by System.out.println(kasa[0].rodzaj);
    When you create an array of five Kasa references those references don't point to an actual Kasa object yet, i.o.w. you have to create the individual Kasa objects and make those array references point to them; e.g.

    Java Code:
    Kasa[] kasa= new Kasa[5]; // 5 references, each one equals null
    kasa[0]= new Kasa(); // element #0 now points to a Kasa object
    kind regards,

    Jos

  3. #3
    Saletra is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

Similar Threads

  1. Array of Objects
    By sfe23 in forum New To Java
    Replies: 19
    Last Post: 02-04-2009, 05:57 PM
  2. Array of objects
    By rosh72851 in forum New To Java
    Replies: 5
    Last Post: 10-31-2008, 04:03 AM
  3. Array of Objects
    By bluefloyd8 in forum New To Java
    Replies: 5
    Last Post: 01-22-2008, 06:27 PM
  4. Array with objects
    By toby in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 09:50 AM
  5. array of objects
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-02-2007, 05:24 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
  •