Results 1 to 6 of 6
  1. #1
    chief100 is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default Access an array from a different class

    Java Code:
    public class Main {
     public static void main(String args[]) {
    	    
    	    
    	
    		list.fillWithRandom(intArr);  // list and intArr cannot be resolved
    		list.print(intArr);   // list and intArr cannot be resolved
     }
    }
    
    
    import java.util.*;
    class List {
    	
    	
    	private static final int NUMINTS = 10;
    	
    	public List(int i) {
    		// TODO Auto-generated constructor stub
    	}
    
    	private void List(int numInts) {
    		List list = new List(10);
    		int[] intArr = new int[10];
    	} 
    	
    	//fill array with random numbers
    	public void fillWithRandom(int intArr[]) {
    	 Random r;
    	 r = new Random();
    	 
    	 int i;
    	 
    	 for(i=0; i < NUMINTS ; i++)
    		 intArr[i] = r.nextInt();
    	} 
    	
    	//display numbers
    	public void print(int intArr[]) {
    		int i;
    		for(i=0 ; i < NUMINTS; i++)
    			System.out.println(intArr[i]);
    		
    		
    	}
    }
    I can't figure out how to get the main class to integrate with my list class.

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    I assume that the two classes are in separate files...

    Here is some information on instantiating class objects: Java 102 : Dealing with classes

  3. #3
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by chief100 View Post
    [code]
    I can't figure out how to get the main class to integrate with my list class.
    i'm only learning Java but i believe (if i'm correct) you need to instatiate the second class

    something like

    Type objName = new className();


    so like.. Chicken chickenObject = new Chicken();

    and if i'm correct this is all OOP fundamentals

  4. #4
    chief100 is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default

    I modified it some more and am getting a null pointer error when I compile the code.

    Exception in thread "main" java.lang.NullPointerException
    at List.fillWithRandom(List.java:23)
    at Main.main(Main.java:8)


    Java Code:
    public class Main {
     public static void main(String args[]) {
    	    
    	 List list = new List(0);
    	 
    		int[] intArr = null;  
    		list.fillWithRandom(intArr); // null pointer
    		list.print(intArr);
     }
    }
    Java Code:
    import java.util.*;
    class List {
    	
    	
    	private static final int NUMINTS = 10;
    	
    	public List(int i) {
    		// TODO Auto-generated constructor stub
    	}
    	private void List(int numInts) {
    		List list = new List(10); 
    		int[] intArr = new int[10];
    	} 
    	
    	//fill array with random numbers
    	public void fillWithRandom(int intArr[]) {
    	 Random r;
    	 r = new Random();
    	 
    	 int i;
    	 
    	 for(i=0; i < NUMINTS ; i++)
    		 intArr[i] = r.nextInt();     // null pointer
    	} 
    	
    	//display numbers
    	public void print(int intArr[]) {
    		int i;
    		for(i=0 ; i < NUMINTS; i++)
    			System.out.println(intArr[i]);
    		
    		
    	}
    }

  5. #5
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    does this

    intArr[i] = r.nextInt();

    not need to be something like

    intArr[i] = r.nextInt(20);

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

    Default

    You have a couple of brain farts in your code; think about it: if an object A wants to know something about an object B this first object (A) at least has to 'know' that other object (B). Second, that object B has to make that 'something' available to any interested party. So your classes A and B should look something like this:

    Java Code:
    public class A {
       private B b; // this is going to be passed in
       public A(B b) { // pass it a B
          this.b= b; // remember the B object
       }
       public void doSomething() {
          b.doSomething();
       }
    }
    When an A object is constructed it is passed a B object. When the A object is asked to do something it in turn asks it member object B to do something.

    Here's the definition of B:
    Java Code:
    public class B {
       public B() { // create a B object
          ...
       }
       public void doSomething() {
          ...
       }
    }
    An object B doesn't care about any A, it is a boring little class actually. A static main( ... ) method sets up and plays the game:

    Java Code:
    public static void main(String[] args) {
       B b= new B();
       A a= new A(b);
    
       a.doSomething();
    }
    I hope this clarifies the structure of it all a bit.

    kind regards,

    Jos

Similar Threads

  1. why i cant access a method in a sub class?
    By javanew in forum New To Java
    Replies: 7
    Last Post: 05-03-2010, 06:29 PM
  2. Replies: 2
    Last Post: 04-30-2010, 05:13 PM
  3. Replies: 0
    Last Post: 04-04-2010, 10:06 AM
  4. Access LinkedList from another class
    By jboy in forum New To Java
    Replies: 20
    Last Post: 09-12-2009, 08:16 AM
  5. How to access array stored in database through JSTL
    By saurabh.joshi in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-19-2007, 01:46 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
  •