Results 1 to 2 of 2
  1. #1
    cat11 is offline Member
    Join Date
    May 2013
    Posts
    1
    Rep Power
    0

    Default How to call an array filled with elements from another class?

    How do I get elements of a rectangle array r[] which I fill in DrawView class, from another class called MainView?

    I have tried out several solutions but either I only get an array filled with the last element of the DrawView class array or nullpointer exception.

    E.g. the code below, when I try to catch the array in MainView (inside for cycle, r[i]=drawView.r[i]), gives an array wholly filled with the last element of DrawView array r[]. But I would like to catch in MainView the same array as in DrawView.

    Any ideas what should I change?

    Java Code:
    public class DrawView extends View {  
      public Rect [] r = new Rect[81];
      Rect r2= new Rect(left, top, right, bottom);
      int m=0;
      
      @Override
      public void onDraw(Canvas canvas) {
    
        //...........some calculations of variables leftPosFirst etc.......//
        int i=0;
        while(i<9){
       	while(i<9){        
    		int j=0;
    		try{
                    	if(m<81){
    	                	r2.left=leftPosFirst+1;
    	                	r2.top=(heightRect+heightRect*j)+1;
    	                	r2.right=rightPosFirst-1;
    	                	r2.bottom=(heightRect+heightRect*(j+1))-1;
    		                r[m]=r2;
    		                m++;
            	        }
    		}
            	catch (Exception ex){}
            	j++;
       	}
        	i++;
         }
      } 
    }
    Java Code:
    public class MainActivity extends Activity implements OnTouchListener{
       DrawView drawView;
       int m=0;
       Rect[] r = new Rect[81];
    
       @Override
       public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            drawView = new DrawView(this);
           
            setContentView(drawView);
    
           drawView.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View arg0, MotionEvent arg1) {
                	
    
                    switch (arg1.getAction()) {
    	                case MotionEvent.ACTION_DOWN: {
    	    	            int i;
    
    	    	        	for (i=0; i<81; i++) {
    				    try{
    	    				r[i] = drawView.r[i];
    	    	        		System.out.println(r[i]);
    				    }
    				    catch(Exception e){}
    	    	        	}
    	                    return true;
    	                }
                    }
                    return false;
                }
           });
       }
    
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,923
    Rep Power
    4

    Default Re: How to call an array filled with elements from another class?

    You should display your exceptions with a printStackTrace(). Otherwise it is difficult to assist. And why do you have two identical nested while loops above. It appears you are trying to transpose a 9 x 9 cube into a linear 81 element array. I don't think it will work the way you are approaching it. Is this what you want?

    Java Code:
    int m = 0;
    for (int j = 0; j < 9; j++) {
      for (int i = 0; i < 9; i++) {
         // do something with i, j and m
         // other processing
         m++;
      }
    }




    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. call an array list in another class?
    By behedwin in forum New To Java
    Replies: 1
    Last Post: 11-13-2012, 04:17 PM
  2. Replies: 2
    Last Post: 05-03-2012, 07:12 PM
  3. read or call an array from other class an method
    By ki_ha1984 in forum New To Java
    Replies: 5
    Last Post: 01-23-2012, 10:42 PM
  4. Replies: 16
    Last Post: 07-08-2010, 09:13 PM
  5. 3d array filled with garbage values
    By jon80 in forum New To Java
    Replies: 5
    Last Post: 01-01-2009, 10:12 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
  •