# Swing JPanel-Matrix NullPointer



## Brighton2 (26. Jun 2015)

Hey,
ich muss Schiffeversenken mit Swing umsetzten. Dies Versuche ich mit Hilfe von vielen JPanels. Nun wird mir immer beim hinzufügen der MouseListener eine NullPointer-Exception an den Kopf geschmissen und ich verstehe nicht warum. Ich habe das Gefühl ich sehe den Wald vor lauter Bäumen nicht. Wäre super wenn ihr da mal einen Blick drüber werfen würdet.
Der Fehler (siehe unten.) wird immer im ersten Code Block in Zeile 16 ausgegeben, aber auch erst im zweiten Schleifen-Durchgang. b.getFieldPanelByCoordinate(0,0) ist nicht null;

Danke für eure Mühe.


```
public class GameController2{
public void initGameScreen(){
        //for(Player p : players){
            gameScreen = new GameScreen(this, players[1]);
            frame.setContentPane(gameScreen);
            gameScreenAddListener();


        //}
    }
public void gameScreenAddListener(){        
        for(Boards b : gameScreen.getBoards()){
            for(int i = 0; i < b.getFieldPanel().length; i++){
                for(int j = 0; j < b.getFieldPanel()[i].length; j++){
                    System.out.println(i +" "+ j);
                    b.getFieldPanelByCoordinate(i, j).addMouseListener(new MouseListener(){
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            // TODO Auto-generated method stub
                            System.out.println("deine Mama");
                        }
                        @Override
                        public void mouseEntered(MouseEvent e) {}
                        @Override
                        public void mouseExited(MouseEvent e) {}
                        @Override
                        public void mousePressed(MouseEvent e) {}
                        @Override
                        public void mouseReleased(MouseEvent e) {}
                    });
                }
            }
        }    
    }


}
```


```
public class GameScreen extends JPanel {
public void initBoards(){
        ownBoardPanel = new Boards(gc);
        char[][] ownBoard = owner.getBoard().viewOwnBoard();
        for(int n = 0; n < ownBoard.length; n++){
            for(int m = 0; m < ownBoard.length; m++){
                ownBoardPanel.addField(new FieldPanel(gc,n,m, ownBoard[n][m]));
            }
        }        
        enemyBoardPanel = new JTabbedPane();
        enemyBoardPanel.setVisible(true);
        enemyBoardPanel.setTabPlacement(JTabbedPane.BOTTOM);
        boards = new Boards[gc.getNumberOfPlayers()-1];
        int counter = 0;
        for(int i = 0; i < gc.getNumberOfPlayers(); i++){
            if(i != owner.getId()){
                char[][] enemyBoard = gc.getPlayers()[i].getBoard().viewBoardForEnemy();
                boards[counter] = new Boards(gc);
                for(int n = 0; n < enemyBoard.length; n++){
                    for(int m = 0; m < enemyBoard[i].length; m++){
//                        System.out.println(n +" "+ m);
                        boards[counter].addField(new FieldPanel(gc,n,m, enemyBoard[n][m]));
                    }
                }
                enemyBoardPanel.addTab("Player "+(i+1), boards[counter]);
                counter++;
            }
        }
        
        this.add(ownBoardPanel);
        this.add(enemyBoardPanel);
        
    }
```


```
public class Boards extends JPanel{


    private static final long serialVersionUID = -4496784329962625843L;
    private FieldPanel[][] fieldPanels;
    
    public Boards(GameController2 gc){
        this.setVisible(true);
        this.setLayout(new GridLayout(gc.getBoardSize(), gc.getBoardSize()));
        fieldPanels = new FieldPanel[gc.getBoardSize()][gc.getBoardSize()];
    }
    public void addField(FieldPanel fp){
        fieldPanels[fp.getYPos()][fp.getXPos()] = fp;
        this.add(fp);
    }
    
    public FieldPanel[][] getFieldPanel(){
        return fieldPanels;
    }
    
    public FieldPanel getFieldPanelByCoordinate(int i, int j){
        return fieldPanels[i][j];
    }
}
```


```
public class FieldPanel extends JPanel {


    private static final long serialVersionUID = -3958978947223937595L;
    private int xPos;
    private int yPos;
    private String imagePath;


    public FieldPanel(GameController2 gc, int y, int x, char type){
        super();
        this.setVisible(true);
        this.setLayout(new BorderLayout());
        chooseType(type);
//        System.out.println("erstellt "+y+" "+x);
    }


    public void chooseType(char type){
        switch(type){
        case 'H': this.add(new JLabel("H")); break;
        case 'M': this.add(new JLabel("M")); break;
        case 'S': this.add(new JLabel("S")); break;
        case 'O': this.add(new JLabel("O")); break;
        }
    }


    public int getXPos(){
        return xPos;
    }
    public int getYPos(){
        return yPos;
    }
}
```


```
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at de.ProjektBattleship.Core.GameController2.gameScreenAddListener(GameController2.java:175)
    at de.ProjektBattleship.Core.GameController2.initGameScreen(GameController2.java:123)
    at de.ProjektBattleship.Core.GameController2$4.actionPerformed(GameController2.java:103)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
```


----------



## strußi (27. Jun 2015)

in der klasse Boards, hast du das Array nicht initialisiert, nur deklariert.


----------



## Brighton2 (27. Jun 2015)

Initialisiert wurde das Array im Konstruktor 

```
[COLOR=#333333]fieldPanels [/COLOR][COLOR=#339933]=[/COLOR][COLOR=#000000][B]new[/B][/COLOR][COLOR=#333333] FieldPanel[/COLOR][COLOR=#009900][[/COLOR][COLOR=#333333]gc.[/COLOR][COLOR=#006633]getBoardSize[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900]][/COLOR][COLOR=#009900][[/COLOR][COLOR=#333333]gc.[/COLOR][COLOR=#006633]getBoardSize[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900]][/COLOR][COLOR=#339933];[/COLOR]
```


----------



## Brighton2 (27. Jun 2015)

Fehler gefunden. Habe die x & y Parameter der Klasse FieldPanel keinen Variablen zugewiesen. 


closed


----------



## strußi (27. Jun 2015)

sry, für meine falsche diagnose


----------

