Hi guys,
i'm trying to program a simple Tic-Tac-Toe game with extern Buttons.
When i'm compiling the Code there's no mistake, when i press Button1 every thing ok, but on the most other Buttons a mistake is shown up :
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at grafik.Zeichenfenster.farbeAufloesen(Zeichenfenster.java:330)
at grafik.Zeichenfenster.zeichnen(Zeichenfenster.java:276)
at Kreis.zeichnen(Kreis.java:157)
at Kreis.<init>(Kreis.java:91)
at Tic_Tac_Toe_V4.feld(Tic_Tac_Toe_V4.java:284)
at Tic_Tac_Toe_V4.access$1100(Tic_Tac_Toe_V4.java:18)
at Tic_Tac_Toe_V4$1.actionPerformed(Tic_Tac_Toe_V4.java:470)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703)
at java.awt.EventQueue.access$000(EventQueue.java:102)
at java.awt.EventQueue$3.run(EventQueue.java:662)
at java.awt.EventQueue$3.run(EventQueue.java:660)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:676)
at java.awt.EventQueue$4.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:673)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
I read on other Threads too but I havn't found the Problem yet.
here's the code:
btw can anyone see why the color don't change?
/**
* Write a description of class Tic_Tac_Toe_V3 here.
*
* @author AngelOfRock
* @version V4_EndVersion
*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.io.IOException;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Tic_Tac_Toe_V4 extends JFrame implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
private JButton button6;
private JButton button7;
private JButton button8;
private JButton button9;
private JPanel panelButton;
private JLabel oben;
private JLabel anzeige;
private String farbe2;
private String farbe;
private Rechteck Spielfeld;
private Kreis Kaestchen;
private Kreis Kaestchen1 [][];
private String Punkt;
private int x;
private int y;
private int d;
private Rechteck [] linie;
private int farbe1;
private int farbe3;
private String information1;
private String information2;
public Tic_Tac_Toe_V4() throws IOException {
//MOUSELISTENER
super("Fenster");
setLocation(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Das BorderLayout ist mal das erste - später fügen wir noch ein GridLayout im Westen hinzu
getContentPane().setLayout(new BorderLayout(12,12));
//Buttons erzeugen
button1 = new JButton("Feld 0/0");
button2 = new JButton("Feld 0/1");
button3 = new JButton("Feld 0/2");
button4 = new JButton("Feld 1/0");
button5 = new JButton("Feld 1/1");
button6 = new JButton("Feld 1/2");
button7 = new JButton("Feld 2/0");
button8 = new JButton("Feld 2/1");
button9 = new JButton("Feld 2/2");
//Panels erzeugen auf einem GridLayout
panelButton = new JPanel(new GridLayout(9,1));
//Auf Panel Buttons packen
panelButton.add(button1);
panelButton.add(button2);
panelButton.add(button3);
panelButton.add(button4);
panelButton.add(button5);
panelButton.add(button6);
panelButton.add(button7);
panelButton.add(button8);
panelButton.add(button9);
//Listener für Buttons
addButtonListener(button1);
addButtonListener(button2);
addButtonListener(button3);
addButtonListener(button4);
addButtonListener(button5);
addButtonListener(button6);
addButtonListener(button7);
addButtonListener(button8);
addButtonListener(button9);
//Labels erzeugen
oben = new JLabel("Layout Test");
//Label zentrieren
oben.setHorizontalAlignment(JLabel.CENTER);
anzeige = new JLabel("Button klicken");
//Labels auf Frame packen (direkt auf das BorderLayout)
getContentPane().add(BorderLayout.NORTH, oben);
getContentPane().add(anzeige);
//Panels auf Frame packen (das panelButton hat ein GridLayout, dass jetzt in den WestBereich des BorderLayouts kommt)
getContentPane().add(BorderLayout.WEST, panelButton);
pack();
setVisible(true);
//MOUSELISTENER
d=0;
Kaestchen1= new Kreis[5][5];
Spielfeld = new Rechteck("schwarz");
Kaestchen = new Kreis("schwarz", 190, (x+1)*50, (y+1)*50);
linie = new Rechteck [10];
//linie [0]= new Rechteck (String farbe1, int xPos, int yPos, int breite1, int hoehe1);
linie [1]= new Rechteck ("gelb",1,100,300,2);//mitte oben
linie [2]= new Rechteck ("gelb",1,200,300,2);//mitte unten
linie [1]= new Rechteck ("gelb",100,1,2,300);//mitte Links
linie [2]= new Rechteck ("gelb",200,1,2,300);//mitte Rechts
linie [3]= new Rechteck ("gelb",1,1,2,300);// Rand Links
linie [3]= new Rechteck ("gelb",300,1,2,300);//Rand Rechts
linie [3]= new Rechteck ("gelb",1,300,300,2);//Rand Unten
linie [3]= new Rechteck ("gelb",1,1,300,2);
Kaestchen1 [3][3]=new Kreis("schwarz", 90, 150, 150);//
Kaestchen1 [0][1]=new Kreis("schwarz", 90, 150, 150);//
Kaestchen1 [0][2]=new Kreis("schwarz", 90, 150, 150);//
Kaestchen1 [1][0]=new Kreis("schwarz", 90, 150, 150);//Alle Kreise erstellen
Kaestchen1 [1][1]=new Kreis("schwarz", 90, 150, 150);// und in die Mitte des Feldes platzieren
Kaestchen1 [1][2]=new Kreis("schwarz", 90, 150, 150);//
Kaestchen1 [2][0]=new Kreis("schwarz", 90, 150, 150);//
Kaestchen1 [2][1]=new Kreis("schwarz", 90, 150, 150);//
Kaestchen1 [2][2]=new Kreis("schwarz", 90, 150, 150);//
}
public void menue () throws IOException
{
System.out.println(" Bitte Spielerfarbe 1 eingeben: ");
BufferedReader lesespeicher = new BufferedReader(new InputStreamReader(System.in));
information1 = lesespeicher.readLine();
farbe= information1;
System.out.println(" Bitte Spielerfarbe 2 eingeben: ");
BufferedReader lesespeicher2 = new BufferedReader(new InputStreamReader(System.in));
information2 = lesespeicher2.readLine();
farbe2= information2;
if (information1.equals(information2))
{
System.out.println(" Bitte unterschiedliche Farben wählen");
try {
menue();
}
catch (Exception menue)
{
menue();
}
}
if( information1.equals("schwarz")|| information2.equals( "schwarz"))
{
System.out.println (" Alle Farben außer Schwarz bitte");
try{
menue();
}
catch (Exception menue)
{
menue ();
}
}
else {
actionPerformed(null);
}
}
private void spiel_ende ()throws IOException
{
if (
Kaestchen1[3][3].farbe.equals(Punkt)&& Kaestchen1[1][0].farbe.equals(Punkt)&& Kaestchen1[2][0].farbe.equals(Punkt)||// waagerecht
Kaestchen1[0][1].farbe.equals(Punkt)&& Kaestchen1[1][1].farbe.equals(Punkt)&& Kaestchen1[2][1].farbe.equals(Punkt)||// waagerecht
Kaestchen1[0][2].farbe.equals(Punkt)&& Kaestchen1[1][2].farbe.equals(Punkt)&& Kaestchen1[2][2].farbe.equals(Punkt)||// waagerecht
Kaestchen1[3][3].farbe.equals(Punkt)&& Kaestchen1[0][1].farbe.equals(Punkt)&& Kaestchen1[0][2].farbe.equals(Punkt)||// senkrecht
Kaestchen1[1][0].farbe.equals(Punkt)&& Kaestchen1[1][1].farbe.equals(Punkt)&& Kaestchen1[1][2].farbe.equals(Punkt)||// senkrecht
Kaestchen1[2][0].farbe.equals(Punkt)&& Kaestchen1[2][1].farbe.equals(Punkt)&& Kaestchen1[2][2].farbe.equals(Punkt)||// senkrecht
Kaestchen1[3][3].farbe.equals(Punkt)&& Kaestchen1[1][1].farbe.equals(Punkt)&& Kaestchen1[2][2].farbe.equals(Punkt)||// diagonal
Kaestchen1[2][0].farbe.equals(Punkt)&& Kaestchen1[1][1].farbe.equals(Punkt)&& Kaestchen1[0][2].farbe.equals(Punkt) // diagonal
)
{
System.out.println (" Spieler: " + Punkt + " hat gewonnen");
if ( Punkt.equals(information1))
{
farbe1++;
}
else
{
farbe3++;
}
System.out.println("Spieler "+ information1 + " hat " + farbe1+ " Punkte.");
System.out.println("Spieler "+ information2 + " hat " + farbe3+ " Punkte.");
try{
neuesSpiel();
}
catch (Exception neuesSpiel)
{
neuesSpiel ();
}
}
else
{
if (d>=9)
{
System.out.println(" Unentschieden");
}
else {
}
}
}
private void neuesSpiel()throws IOException
{
System.out.println (" Wollen sie weiter spielen? Ja=1 Nein=2");
BufferedReader lesespeicher = new BufferedReader(new InputStreamReader(System.in));
String information4 = lesespeicher.readLine();
double q=Double.parseDouble(information4);
if(information4.equals("1"))
{
Kaestchen1 [3][3]=new Kreis("schwarz", 190, 4, 4);
Kaestchen1 [0][1]=new Kreis("schwarz", 190, 103, 4);
Kaestchen1 [0][2]=new Kreis("schwarz", 190, 203, 4);
Kaestchen1 [1][0]=new Kreis("schwarz", 190, 4, 103);
Kaestchen1 [1][1]=new Kreis("schwarz", 190, 103, 103);
Kaestchen1 [1][2]=new Kreis("schwarz", 190, 203, 103);
Kaestchen1 [2][0]=new Kreis("schwarz", 190, 4, 203);
Kaestchen1 [2][1]=new Kreis("schwarz", 190, 103, 203);
Kaestchen1 [2][2]=new Kreis("schwarz", 190, 203, 203);
d=0;
System.out.println("\f");
}
else
{
System.out.println (" Bis Bald");
}
}
private void feld()throws IOException {// Diese Methode wurde nur für den Aufbau verwendet für das Programm selbst nicht v. Nutzen\\
if ( x==3 && y==3)
{
Kaestchen1 [3][3]=new Kreis(Punkt, 190, 4, 4);
}
else{
if (y==0 && x==1)
{
Kaestchen1 [3][3]=new Kreis(Punkt, 190, 103, 4);
}
else {
if (y==0&& x==2)
{
Kaestchen1 [0][2]=new Kreis(Punkt, 190, 203, 4);
}
else {
if (y==1&&x==0)
{
Kaestchen1 [1][0]=new Kreis(Punkt, 190, 4, 103);
}
else{
if (y==1 && x==1)
{
Kaestchen1 [1][1]=new Kreis(Punkt, 190, 103, 103);
}
else{
if( y==1 && x==2)
{
Kaestchen1 [1][2]=new Kreis(Punkt, 190, 103, 203);
}
else{
if(y==2 && x==0){
Kaestchen1 [2][0]=new Kreis(Punkt, 190, 4, 203);
}
else {
if (y==2 && x==1)
{
Kaestchen1 [2][1]=new Kreis(Punkt, 190, 203, 103);
}
else {
if (y==2 && x==2){
Kaestchen1 [2][2]=new Kreis(Punkt, 190, 203, 203);
}
}
}
}
}
}
}
}
}
}
public static void main(String[] args)
{
}
public void actionPerformed2(ActionEvent e) {
if (e.equals(button1))
{
System.out.println("Button1 geklickt!");
x=3;
y=3;
}
else {
if (e.equals(button2))
{
System.out.println("Button2 geklickt!");
x=0;
y=1;
}
else {
if (e.equals(button3))
{
System.out.println("Button3 geklickt!");
x=0;
y=2;
}
else {
if (e.equals(button4))
{
System.out.println("Button4 geklickt!");
x=1;
y=0;
}
else {
if (e.equals(button5))
{
System.out.println("Button5 geklickt!");
x=1;
y=1;
}
else {
if (e.equals(button6))
{
System.out.println("Button6 geklickt!");
x=1;
y=2;
}
else {
if (e.equals(button7))
{
System.out.println("Button7 geklickt!");
x=2;
y=0;
}
else {
if (e.equals(button8))
{
System.out.println("Button8 geklickt!");
x=2;
y=1;
}
else {
if (e.equals(button9))
{
System.out.println("Button9 geklickt!");
x=2;
y=2;
}
else {
}
}
}
}
}
}
}
}
}
} private void addButtonListener(JButton b) throws IOException
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
Object obj=ae.getSource();
if (obj== button1)
{
System.out.println ("Button1");
x=0;
y=0;
}
else{
if (obj== button2)
{
System.out.println ("Button2");
x=0;
y=1;
}
else {
if (obj== button3)
{
System.out.println ("Button3");
x=0;
y=2;
}
else {
if (obj== button4)
{
System.out.println ("Button4");
x=1;
y=0;
}
else {
if(obj== button5)
{
System.out.println ("Button5");
x=1;
y=1;
}
else {
if (obj== button6)
{
System.out.println ("Button6");
x=1;
y=2;
}
else{
if (obj== button7)
{
System.out.println ("Button7");
x=2;
y=0;
}
else {
if(obj== button8)
{
System.out.println ("Button8");
x=2;
y=1;
}
else {
if (obj== button9)
{
System.out.println ("Button9");
x=2;
y=2;
}
}
}
}
}
}
}
}
}
try {
feld();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
class ButtonLauscher implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Button geklickt!");
}
}