Returning 2D array of Button from one class to another!

-1 Hussam Cheema · August 23, 2014
I am facing a little problem in returning 2D array of Buttons from one class to another. It's giving (java.lang.NullPointerException) when I run this program :(



I made 3 classes..... 2 main classes are given below:


package chess;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Controller {

   private JFrame frame;
   private JPanel[][] panel;
   private JButton[][] c_buttons;
   private Players player;
   public int i,j;

   public Controller(){

       frame = new JFrame("Chess By HUS$AM");
       panel = new JPanel[10][10];
       c_buttons = new JButton[4][9];

       player = new Players();

       // HERE I AM RETURNING BUTTONS FROM Players CLASS USING METHOD...
       c_buttons = player.computer_pawn_players();

       for(i=1; i<=8; i++){
           for(j=1; j<=8; j++){

               panel[j]= new JPanel();

           }
       }

       for(j=1; j<=8; j++)
           panel[2][j].add(c_buttons[2][j]);

       frame.setLayout(new GridLayout(8,8));

       for(i=1; i<=8; i++){    
           for(j=1; j<=8; j++){
               frame.add(panel[j]);

               if ((i + j) % 2 == 0) panel[j].setBackground(Color.black);
               else panel[j].setBackground(Color.white);
           }  
       }//end outer for loop

       frame.setSize(600,500);
       frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
       frame.setVisible(true);


   }//end constructor

}//end controller class


Second Class is : 



package chess;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;

public class Players {

   private JButton[][] buttons;
   private Icon[][] icons;
   public int i,j;

   public void Players(){

       buttons = new JButton[4][9];
       icons = new ImageIcon[4][9];

       for(i=1; i<=8; i++)
           for(j=1; j<=8; j++)
               buttons[j] = new JButton();

       for(j=1; j<=8; j++)
           icons[1][j] = new ImageIcon(getClass().getResource("blackPawn"));

       for(j=1; j<=8; j++)
           buttons[2][j].setIcon(icons[1][j]);

   }

   public JButton[][] computer_pawn_players(){

       return buttons;

   }
}


Post a Reply

Replies

Oldest  Newest  Rating
+2 Pere Garau Burguera · August 27, 2014
Make sure when you do this:



c_buttons = player.computer_pawn_players();



that this method actually returns something. You have to look in the players class to see if the problem may be on this line. You can post your error trace here so we can see where is the problem exactly and can give you better solutions. 
0 Hussam Cheema · August 27, 2014
Thanks guys :) Its working fine now, I solved the problem :)
0 Yoncho Yonchev · August 25, 2014
Usually reading the output from the console helps to identify the problem or even following the stacktrace to understand in which line is your mistake too. It is good idea to post it too.
  • 1

Java / Android Development

107,195 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator