Results 1 to 5 of 5
  1. #1
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    87
    Rep Power
    0

    Default Trying to create a class which has an array of another class, can't init properly

    This one should be easy for you! I've been away from Java for 6 months and it's not clear if I ever knew the answer to this.

    Java Code:
    public class ClassTest {
    
    	public static void main(String[] args) {
    		String model = new String();
    		ClassTest2 variable = new ClassTest2();
    		System.out.println(variable.bankFileName);
    // next line generates null pointer exception
    		System.out.println(variable.bank[0].patchFileName);
    	}
    }
    
    class ClassTest1 {
    	String patchModel;
    	String patchFileName;
    
    	ClassTest1(String name) {
    		patchModel = new String();
    		patchFileName = name;
    	}
    }
    
    class ClassTest2 {
    	Boolean changed = false;
    	String bankFileName = "Untitled";
    	ClassTest1[] bank;
    
    	ClassTest2() {
    		bank = new ClassTest1[8];
    	}
    }
    Here I have a class called "ClassTest2". One of its elements is an array of class "ClassTest1". I get a null pointer exception at the point indicated. ClassTest2's constructor declares an array of 8 of ClassTest1. However I have no clue how to actually call that constructor with the "name" parameter included.

    Undoubtedly this is really basic but I can't quite figure it out, so any help is most welcome.

    Thanks,

    DL

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Trying to create a class which has an array of another class, can't init properly

    What does variable.bank[0] contain? Probably a null value since you haven't initialized the array (only allocated storage).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    87
    Rep Power
    0

    Default Re: Trying to create a class which has an array of another class, can't init properly

    Quote Originally Posted by jim829 View Post
    What does variable.bank[0] contain? Probably a null value since you haven't initialized the array (only allocated storage).

    Regards,
    Jim
    You're right. I've tried a few other things but am totally shooting in the dark. E.g.

    Java Code:
    public class ClassTest {
    	public static void main(String[] args) {
    		ClassTest2 variable = new ClassTest2();
    		System.out.println(variable.bank[0].patchFileName);
    	}
    }
    
    class ClassTest1 {
    	String patchFileName = new String();
    
    	ClassTest1(String name) {
    		patchFileName = name;
    	}
    }
    
    class ClassTest2 {
    	ClassTest1[] bank;
    
    	ClassTest2() {
    		for(int i = 0; i < 8; i++) {
    			bank[i] = new ClassTest1("patch " + i);
    		}
    	}
    }

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Trying to create a class which has an array of another class, can't init properly

    You ClassTest2 constructor solved the problem. But you introduced a new one by removing the bank allocation. Also, patchFileName just needs to be
    declared a string. Don't create an instance of String as it isn't necessary.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    87
    Rep Power
    0

    Default Re: Trying to create a class which has an array of another class, can't init properly

    Jim, thanks a lot! Sometimes I expect too much from Java, like it can read my mind.

    Here's the final code:

    Java Code:
    	
    public class ClassTest {
    	public static void main(String[] args) {
    		ClassTest2 variable = new ClassTest2();
    		System.out.println(variable.bank[0].patchFileName);
    	}
    }
    
    class ClassTest1 {
    	String patchFileName = "";
    
    	ClassTest1(String name) {
    		patchFileName = name;
    	}
    }
    
    class ClassTest2 {
    	ClassTest1[] bank = new ClassTest1[8];
    
    	ClassTest2() {
    		for(int i = 0; i < 8; i++) {
    			bank[i] = new ClassTest1("patch " + i);
    		}
    	}
    }
    I actually used Strings as stand-ins for the real classes in my program so I wouldn't have to include all that detail.

Similar Threads

  1. Replies: 2
    Last Post: 02-10-2015, 06:08 PM
  2. Replies: 2
    Last Post: 02-10-2015, 06:06 PM
  3. How to create an array of MY class
    By Accendo in forum New To Java
    Replies: 2
    Last Post: 01-20-2011, 05:18 PM
  4. how to create static array in class
    By itaipee in forum New To Java
    Replies: 1
    Last Post: 04-19-2009, 03:33 PM
  5. design question comments wanted - essential class init()
    By Nicholas Jordan in forum Advanced Java
    Replies: 0
    Last Post: 07-22-2008, 09:41 PM

Posting Permissions

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