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!"); 
    } 
}

Look at the line of your code where the NPE was thrown. Print the values of the variables at that line to see which is null. Backtrack with more prints until you find out why it's null.

ps: if (e.equals(button1))
e is an ActionEvent, button1 is JButton. They can never be equal. Maybe you were thinking of e.getSource()?

Thanx for the fast answer

e.getSource

when I "e.getSource" one error after the next is soming up

one error after the next is soming up

Sorry, but if you don't give details of the error(s) then it's hard to comment on them.

"method getSource in class java.ulti.EventObject cannot be appliend to given types;
required:no argument; found java.swing.Jbutton; reason actual and formal argument lists differ in length"
how to repair that... I'm very new to java

getSource();

Y, i already thought about that, but then it says "cann't find that method" so what do I have to write in this method, when i have made it?

@JamesCherrill
Thank you very much for your help :)
I hope i can solve the last clue of my code^^
Do you know a good Guid for Mouselistender, cause i found on google only patchy ones.

OK... I give up :(
I looked 2 hours 2 find my mistake of Null, didn't found it...
can anyone look pls over the posted code where my problem is?

Sorry, but if you don't give details of the error(s) then it's hard to comment on them. Post the exact complete text of the null pointer error message, and the exact line(s) of code it refers to.

The problem must be here, cause in other version of the game ( Only with IOException/BufferdReader) Everything works.The "Button1" is working without any troubles but if i press any other one the Errormassage (which I posted at the beginning of this thread) is shown up. But isn't it the "same" comparison at the other ones?

 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;
                            }
                             }
                         }
                     }
                  }
             }
         }
     }
   }

and don't say:

"the code must be here"

guessing based on what code you added. every change to the use of the code can also cause trouble. the stack trace tells you exactly on which line of which class the exception is thrown.

Hey, sry that I haven't wrote back sooner. Had been 1 Week in Berlin without internet.
I think I found the mistake @ the Breakpoint in Debuggmode:
Breakpoint hit the file for which the source cannot be found. Filename: Component.java
So how can I bypass that?

What's (presumably) happening is that you have a null reference in your program somewhere. You pass that as a parameter to a Swing/AWT method, that calls other methods until the null reference is actually used and the NPE is thrown. In this case that finally happens in AWT's Component class. The dubugger tries to show you the line of code in question, but it doesn't have access to the Java API source code.
You can safely ignore Component because that's not where the problem is caused. Look at the complete stack when the NPE is thrown and look for the last entry that is in your code. Thast's where your bug is really manifesting itself.

Edited 4 Years Ago by JamesCherrill

Finally I made it :)
Thank you all for your help and tips.
Btw. when the game is over BlueJ freezes( cann't close any window), might my program be the reason? o.O

most likely, yes

does the entire application block, or does it just not stop running the JVM?
and I assume it's just BlueJ that freezes?

The "Drawing window, terminal window" are "frozen"- I mean i cannot close them, when the game is over and the programm finished. I always have to restart BlueJ, if i want to make a new round of my Tic_Tac_Toe -.-#

BlueJ isn't exactly the most most respected professional tool for Java development. WHat happens if you just run your code at the command prompt?

Yes I know that but I just started programming and I'm also useing Eclips. And the last problem was caused my eclips... It added a codeline as assistance. After deleteing it the hole Programm work.
Just one last question:
How do I programm a waiting period as an event?
The codes I found in the web doesn't work unlikly

Can you explain what you are trying to do?
Do you want to wait after a certain event happens?
or do you want to wait and then create an event?

Edited 4 Years Ago by NormR1

Y The Program is a Tic_Tac_Toe Game and now after the game there's instandly a new game a you don't see which play won...
So I want to add an Event like if sb wins wait e.g. 5 Sec.
Or would it be easier just add a button with "New Game"?

I agree. The wait would be less code, but the button is far better user interface - it puts the user in charge.

Ok thanks a lot for ur help.
Now it's solved :D
Do i have to mark somehow the thread?

This question has already been answered. Start a new discussion instead.