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.
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.
Java:
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) {}
});
}
}
}
}
}
Java:
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);
}
Java:
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];
}
}
Java:
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;
}
}
Code:
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)