ActionListener NullPointerException

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 683 Views - Last Post: 14 January 2013 - 05:05 AM Rate Topic: -----

#1 Buttertoast  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 60
  • Joined: 10-January 13

ActionListener NullPointerException

Posted 10 January 2013 - 06:45 AM

Hello, i have a problem with my code, it points out a Java.lang.NullPointerException error if i try to run it, could anybody try to help me?


import javax.swing.*;
import java.awt.*;
//Event brauchen wir für das Ereigniss, wenn ein Button geklickt wird
import java.awt.event.*;

public class secondgui extends JFrame
{
     private JButton button;
    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 JButton button10;
    private JButton button11;
    private JButton button12;
    private JButton button13;
    private JButton button14;
    private JButton button15;
    private JButton button16;
    private JButton button17;
    private JButton button18;
    private JButton button19;
    private JButton button20;
    private JButton button21;
    private JButton button22;
    private JButton button23;
    private JButton button24;
    private JButton button25;
    private JButton button26;
    private JButton button27;
    private JButton button28;
    private JButton button29;
    private JButton button30;
    private JButton button31;
    private JButton button32;
    private JButton button33;
    private JButton button34;
    private JButton button35;
    private JButton button36;
    private JButton button37;
    private JButton button38;
    private JButton button39;
    private JButton button40;
    private JButton button41;
    private JButton button42;
    private JButton button43;
    private JButton button44;
    private JButton button45;
    private JButton button46;
    private JButton button47;
    private JButton button48;
    private JButton button49;
    private JButton button50;
    private JButton button51;
    private JButton button52;
    private JButton button53;
    private JButton button54;
    private JButton button55;
    private JButton button56;
    private JButton button57;
    private JButton button58;
    private JButton button59;
    private JButton button60;
    private JButton button61;
    private JButton button62;
    private JButton button63;
    private JButton button64;
    private JButton button65;
    private JButton button66;
    private JButton button67;
    private JButton button68;
    private JButton button69;
    private JButton button70;
    private JButton button71;
    private JButton button72;
    private JButton button73;
    private JButton button74;
    private JButton button75;
    private JButton button76;
    private JButton button77;
    private JButton button78;
    private JButton button79;
    private JButton button80;
    private JButton button81;
    private JPanel panelButton;
    private JLabel oben;
    private JButton anzeige;
    private  String string;
    private  String string2;
    private  String string3;
    private  String string4;
    private  String string5;
    private  String string6;
    private  String string7;
    private  String string8;
    private  String string9;
    private  String stringa;
    private int x1 =1;
   private int y1 =0;
   private int x2 =1;
   private int y2 =0;
   private int x3 =1;
   private int y3 =0;
   private int y4 =0;
   private int x4 =1;
   private int y5 =0;
   private int x5 =1;
   private int x6 =1;
   private int y6 =0;
   private int x7 =1;
   private int y7 =0;
   private int x8 =1;
   private int y8 =0;
   private int x9 = 1;
   private int y9 =0;
   private int o;
   private int q;
 
   

   int[][] sudo = {{1, 0, 0, 6, 0, 0, 0, 0, 0},
                {0, 2, 0, 0, 0, 0, 9, 0, 0},
                {0, 0, 3, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 4, 0, 0, 0, 1, 0},
                {0, 4, 0, 0, 5, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 6, 0, 9, 0},
                {0, 0, 2, 0, 0, 0, 7, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 8, 0},
                {3, 0, 0, 5, 0, 0, 0, 0, 9}};
    public secondgui()
    {
        super("Sudoku");
        setLocation(300,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Das BorderLayout wird erzeugt
        getContentPane().setLayout(new BorderLayout(5,5));  

        //Buttons erzeugen
        button = new JButton("1");
        button1 = new JButton("1");
        button2 = new JButton("1");
        button3 = new JButton("1");
        button4 = new JButton("1");
        button5 = new JButton("1");
        button6 = new JButton("1");
        button7 = new JButton("1");
        button8 = new JButton("1");
        button9 = new JButton("1");
        button10 = new JButton("1");
        button11 = new JButton("1");
        button12 = new JButton("1");
        button13 = new JButton("1");
        button14 = new JButton("1");
        button15 = new JButton("1");
        button16 = new JButton("1");
        button17 = new JButton("1");
        button18 = new JButton("1");
        button19 = new JButton("1");
        button20 = new JButton("1");
        button21 = new JButton("1");
        button22 = new JButton("1");
        button23 = new JButton("1");
        button24 = new JButton("1");
        button25 = new JButton("1");
        button26 = new JButton("1");
        button27 = new JButton("1");
        button28 = new JButton("1");
        button29 = new JButton("1");
        button30= new JButton("1");
        button31 = new JButton("1");
        button32 = new JButton("1");
        button33 = new JButton("1");
        button34 = new JButton("1");
        button35 = new JButton("1");
        button36 = new JButton("1");
        button37 = new JButton("1");
        button38 = new JButton("1");
        button39 = new JButton("1");
        button40 = new JButton("1");
        button41 = new JButton("1");
        button42 = new JButton("1");
        button43 = new JButton("1");
        button44 = new JButton("1");
        button45 = new JButton("1");
        button46 = new JButton("1");
        button47 = new JButton("1");
        button48 = new JButton("1");
        button49 = new JButton("1");
        button50 = new JButton("1");
        button51 = new JButton("1");
        button52 = new JButton("1");
        button53 = new JButton("1");
        button54 = new JButton("1");
        button55 = new JButton("1");
        button56 = new JButton("1");
        button57 = new JButton("1");
        button58 = new JButton("1");
        button59 = new JButton("1");
        button60 = new JButton("1");
        button61 = new JButton("1");
        button62 = new JButton("1");
        button63 = new JButton("1");
        button64 = new JButton("1");
        button65 = new JButton("1");
        button66 = new JButton("1");
        button67 = new JButton("1");
        button68 = new JButton("1");
        button69 = new JButton("1");
        button70 = new JButton("1");
        button71 = new JButton("1");
        button72 = new JButton("1");
        button73 = new JButton("1");
        button74 = new JButton("1");
        button75 = new JButton("1");
        button76 = new JButton("1");
        button77 = new JButton("1");
        button78 = new JButton("1");
        button79 = new JButton("1");
        button80 = new JButton("1");
        button81 = new JButton("1");

        //Panels erzeugen auf einem GridLayout
        panelButton = new JPanel(new GridLayout(9,9));

        //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);
        panelButton.add(button10);
        panelButton.add(button11);
        panelButton.add(button12);
        panelButton.add(button13);
        panelButton.add(button14);
        panelButton.add(button15);
        panelButton.add(button16);
        panelButton.add(button17);
        panelButton.add(button18);
        panelButton.add(button19);
        panelButton.add(button20);
        panelButton.add(button21);
        panelButton.add(button22);
        panelButton.add(button23);
        panelButton.add(button24);
        panelButton.add(button25);
        panelButton.add(button26);
        panelButton.add(button27);
        panelButton.add(button28);
        panelButton.add(button29);
        panelButton.add(button30);
        panelButton.add(button31);
        panelButton.add(button32);
        panelButton.add(button33);
        panelButton.add(button34);
        panelButton.add(button35);
        panelButton.add(button36);
        panelButton.add(button37);
        panelButton.add(button38);
        panelButton.add(button39);
        panelButton.add(button40);
        panelButton.add(button41);
        panelButton.add(button42);
        panelButton.add(button43);
        panelButton.add(button44);
        panelButton.add(button45);
        panelButton.add(button46);
        panelButton.add(button47);
        panelButton.add(button48);
        panelButton.add(button49);
        panelButton.add(button50);
        panelButton.add(button51);
        panelButton.add(button52);
        panelButton.add(button53);
        panelButton.add(button54);
        panelButton.add(button55);
        panelButton.add(button56);
        panelButton.add(button57);
        panelButton.add(button58);
        panelButton.add(button59);
        panelButton.add(button60);
        panelButton.add(button61);
        panelButton.add(button62);
        panelButton.add(button63);
        panelButton.add(button64);
        panelButton.add(button65);
        panelButton.add(button66);
        panelButton.add(button67);
        panelButton.add(button68);
        panelButton.add(button69);
        panelButton.add(button70);
        panelButton.add(button71);
        panelButton.add(button72);
        panelButton.add(button73);
        panelButton.add(button74);
        panelButton.add(button75);
        panelButton.add(button76);
        panelButton.add(button77);
        panelButton.add(button78);
        panelButton.add(button79);
        panelButton.add(button80);
        panelButton.add(button81);
      
        //Listener für Buttons
        addButtonListenerPruefen(anzeige);
        addButtonListener(button1);
        addButtonListener2(button2);
        addButtonListener3(button3);
        addButtonListener4(button4);
        addButtonListener5(button5);
        addButtonListener6(button6);
        addButtonListener7(button7);
        addButtonListener8(button8);
        addButtonListener9(button9);
      
        
        

        //Labels erzeugen
        oben = new JLabel("Sudoku");
        //Label zentrieren
        oben.setHorizontalAlignment(JLabel.CENTER); 
        anzeige = new JButton("Prüfen");
        

        //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);
        getContentPane().add(BorderLayout.EAST, anzeige);
       

        pack();
        setVisible(true);

    }

    public static void main(String[] args)
    {
        FirstGUI g = new FirstGUI();
    }

    private void addButtonListener(JButton B)/>/>
    {
        b.addActionListener(new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            { 
                eingabe(ae.getActionCommand());
                int i = Integer.parseInt(button1.getText());
                if (x1==1)
                {
                 
                    if (i<9)
                    {
                    i= i+1;
                    String string = String.valueOf(i);
                    
                    button1.setText(string);
                    y1= y1+1;
                    }
                   if (y1==8)
                   { x1=0;
                     y1=0;
                    }
                }
                else
                    {
                     
                    if (i>0)
                            {   
                                i= i-1;
                                String string = String.valueOf(i);
                                
                                button1.setText(string);
                                y1= y1+1;
                               }
                    if (y1==8)
                   { x1=1;
                       y1=0;
                    }
                   }
                   
                }
                 
            
        });
    }
    private void addButtonListener2(JButton B)/>/>
    {
        b.addActionListener(new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            { 
                eingabe(ae.getActionCommand());
                int h = Integer.parseInt(button2.getText());
                if (x2==1)
                {
                 
                    if (h<9)
                    {
                    h= h+1;
                    String string2 = String.valueOf(h);
                   
                    button2.setText(string2);
                    y2= y2+1;
                    }
                   if (y2==8)
                   { x2=0;
                     y2=0;
                    }
                }
                else
                    {
                     
                    if (h>0)
                            {   
                                h= h-1;
                                String string2 = String.valueOf(h);
                                
                                button2.setText(string2);
                                y2= y2+1;
                               }
                    if (y2==8)
                   { x2=1;
                       y2=0;
                    }
                   }
                   
                }
                 
            
        });
    }
     private void addButtonListener3(JButton B)/>/>
    {
        b.addActionListener(new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            { 
                eingabe(ae.getActionCommand());
                int j = Integer.parseInt(button3.getText());
                if (x3==1)
                {
                 
                    if (j<9)
                    {
                    j= j+1;
                    String string3 = String.valueOf(j);
                    button3.setText(string3);
                    y3= y3+1;
                    }
                   if (y3==8)
                   { x3=0;
                     y3=0;
                    }
                }
                else
                    {
                     
                    if (j>0)
                            {   
                                j= j-1;
                                String string = String.valueOf(j);
                                button3.setText(string);
                                y3= y3+1;
                               }
                    if (y3==8)
                   { x3=1;
                       y3=0;
                    }
                   }
                   
                }
                 
            
        });
    }
    private void addButtonListener4(JButton B)/>/>
    {
        b.addActionListener(new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            { 
                eingabe(ae.getActionCommand());
                int h = Integer.parseInt(button4.getText());
                if (x4==1)
                {
                 
                    if (h<9)
                    {
                    h= h+1;
                    String string4 = String.valueOf(h);
                    button4.setText(string4);
                    y4= y4+1;
                    }
                   if (y4==8)
                   { x4=0;
                     y4=0;
                    }
                }
                else
                    {
                     
                    if (h>0)
                            {   
                                h= h-1;
                                String string4 = String.valueOf(h);
                                button4.setText(string4);
                                y4= y4+1;
                               }
                    if (y4==8)
                   { x4=1;
                       y4=0;
                    }
                   }
                   
                }
                 
            
        });
    }
    private void addButtonListener5(JButton B)/>/>
    {
        b.addActionListener(new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            { 
                eingabe(ae.getActionCommand());
                int h = Integer.parseInt(button5.getText());
                if (x5==1)
                {
                 
                    if (h<9)
                    {
                    h= h+1;
                    String string5 = String.valueOf(h);
                    button5.setText(string5);
                    y5= y5+1;
                    }
                   if (y5==8)
                   { x5=0;
                     y5=0;
                    }
                }
                else
                    {
                     
                    if (h>0)
                            {   
                                h= h-1;
                                String string5 = String.valueOf(h);
                                button5.setText(string5);
                                y5= y5+1;
                               }
                    if (y5==8)
                   { x5=1;
                       y5=0;
                    }
                   }
                   
                }
                 
            
        });
    }
    private void addButtonListener6(JButton B)/>/>
    {
        b.addActionListener(new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            { 
                eingabe(ae.getActionCommand());
                int h = Integer.parseInt(button6.getText());
                if (x6==1)
                {
                 
                    if (h<9)
                    {
                    h= h+1;
                    String string6 = String.valueOf(h);
                    button6.setText(string6);
                    y6= y6+1;
                    }
                   if (y6==8)
                   { x6=0;
                     y6=0;
                    }
                }
                else
                    {
                     
                    if (h>0)
                            {   
                                h= h-1;
                                String string6 = String.valueOf(h);
                                button6.setText(string6);
                                y6= y6+1;
                               }
                    if (y6==8)
                   { x6=1;
                       y6=0;
                    }
                   }
                   
                }
                 
            
        });
    }
    private void addButtonListener7(JButton B)/>/>
    {
        b.addActionListener(new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            { 
                eingabe(ae.getActionCommand());
                int h = Integer.parseInt(button7.getText());
                if (x7==1)
                {
                 
                    if (h<9)
                    {
                    h= h+1;
                    String string7 = String.valueOf(h);
                    button7.setText(string7);
                    y7= y7+1;
                    }
                   if (y7==8)
                   { x7=0;
                     y7=0;
                    }
                }
                else
                    {
                     
                    if (h>0)
                            {   
                                h= h-1;
                                String string7 = String.valueOf(h);
                                button7.setText(string7);
                                y7= y7+1;
                               }
                    if (y7==8)
                   { x7=1;
                       y7=0;
                    }
                   }
                   
                }
                 
            
        });
    }
    private void addButtonListener8(JButton B)/>/>
    {
        b.addActionListener(new ActionListener()
        { 
            public void actionPerformed(ActionEvent ae) 
            { 
                eingabe(ae.getActionCommand());
                int h = Integer.parseInt(button8.getText());
                if (x8==1)
                {
                 
                    if (h<9)
                    {
                    h= h+1;
                    String string8 = String.valueOf(h);
                    button8.setText(string8);
                    y8= y8+1;
                    }
                   if (y8==8)
                   { x8=0;
                     y8=0;
                    }
                }
                else
                    {
                     
                    if (h>0)
                            {   
                                h= h-1;
                                String string8 = String.valueOf(h);
                                button8.setText(string8);
                                y8= y8+1;
                               }
                    if (y8==8)
                   { x8=1;
                       y8=0;
                    }
                   }
                   
                }
                 
            
        });
    }
    private void addButtonListener9(JButton B)/>/>
    {
        b.addActionListener(new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            { 
                eingabe(ae.getActionCommand());
                int h = Integer.parseInt(button9.getText());
                if (x9==1)
                {
                 
                    if (h<9)
                    {
                    h= h+1;
                    String string9 = String.valueOf(h);
                    button9.setText(string9);
                    y9= y9+1;
                    }
                   if (y9==8)
                   { x9=0;
                     y9=0;
                    }
                }
                else
                    {
                     
                    if (h>0)
                            {   
                                h= h-1;
                                String string9 = String.valueOf(h);
                                
                                button9.setText(string9);
                                y9= y9+1;
                               }
                    if (y9==8)
                   { x9=1;
                       y9=0;
                    }
                   }
                   
                }
                 
            
        });
    }
    private void addButtonListenerPruefen(JButton B)/>/>
    {
        b.addActionListener(new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            { 
                eingabe(ae.getActionCommand());
                
                if ((string == "1") && (string2 == "2") || (string2 == "3") || string2 == "4" || string2 == "5" || string2 == "6" || string2 == "7" || string2 == "8" || string2 == "9")
                   {
                       button1.setBackground(Color.GREEN);
                       anzeige.setText("Richtig");
                    }
                }
                 
            
        });
    }

    private void eingabe(String a)
    {
        anzeige.setText("Prüfen");
    }    
}




Is This A Good Question/Topic? 0
  • +

Replies To: ActionListener NullPointerException

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2794
  • View blog
  • Posts: 11,830
  • Joined: 20-September 08

Re: ActionListener NullPointerException

Posted 10 January 2013 - 07:04 AM

http://technojeeves....free/118-errors
Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1408
  • View blog
  • Posts: 3,123
  • Joined: 05-April 11

Re: ActionListener NullPointerException

Posted 10 January 2013 - 07:15 AM

So this is what obfuscated code looks like

This post has been edited by CasiOo: 10 January 2013 - 07:15 AM

Was This Post Helpful? 0
  • +
  • -

#4 Buttertoast  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 60
  • Joined: 10-January 13

Re: ActionListener NullPointerException

Posted 10 January 2013 - 08:02 AM

its a runtime error
the error report:
java.lang.NullPointerException
	at secondgui.addButtonListenerPruefen(secondgui.java:759)
	at secondgui.<init>(secondgui.java:315)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
	at bluej.runtime.ExecServer$3.run(ExecServer.java:740)


Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2794
  • View blog
  • Posts: 11,830
  • Joined: 20-September 08

Re: ActionListener NullPointerException

Posted 10 January 2013 - 08:59 AM

Quote

     //Listener für Buttons
        addButtonListenerPruefen(anzeige);

'anzeige' is null at that point. You can't add a listener to a null button
Was This Post Helpful? 1
  • +
  • -

#6 Buttertoast  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 60
  • Joined: 10-January 13

Re: ActionListener NullPointerException

Posted 10 January 2013 - 01:47 PM

could you help me?
i dont know how to dedicate the button to do this action
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10700
  • View blog
  • Posts: 39,813
  • Joined: 27-December 08

Re: ActionListener NullPointerException

Posted 10 January 2013 - 01:52 PM

There is so much wrong with this code. First, you really need to learn to use arrays and loops. You could write so much less code using them. Second, don't compare Strings using the == operator, as it compares their memory locations. Use the equals() method instead.
Was This Post Helpful? 1
  • +
  • -

#8 Buttertoast  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 60
  • Joined: 10-January 13

Re: ActionListener NullPointerException

Posted 10 January 2013 - 02:10 PM

thanks, i switched the == for equals
but i dont know how to do the looping thing with the buttons, could you help me?
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10700
  • View blog
  • Posts: 39,813
  • Joined: 27-December 08

Re: ActionListener NullPointerException

Posted 10 January 2013 - 02:14 PM

An example:
JButton[] buttons = new JButton[81];

for(int i = 0; i < buttons.length; i++){
    buttons[i] = new JButton("1");
    panel.add(buttons[i]);
}



And that trims down ~160 lines of code to 4.
Was This Post Helpful? 2
  • +
  • -

#10 Buttertoast  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 60
  • Joined: 10-January 13

Re: ActionListener NullPointerException

Posted 10 January 2013 - 02:21 PM

thank you, that code is great :)

but i still don´t know what to do to get the button working.. x) any ideas?
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10700
  • View blog
  • Posts: 39,813
  • Joined: 27-December 08

Re: ActionListener NullPointerException

Posted 10 January 2013 - 02:23 PM

You have to add the Listener to the JButton after you instantiate it.
Was This Post Helpful? 1
  • +
  • -

#12 Buttertoast  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 60
  • Joined: 10-January 13

Re: ActionListener NullPointerException

Posted 10 January 2013 - 03:09 PM

i edited the code, but now i get the error message already at the first listener x)


import javax.swing.*;
import java.awt.*;
//Event brauchen wir für das Ereigniss, wenn ein Button geklickt wird
import java.awt.event.*;

public class secondgui extends JFrame
{
     private JButton button;
    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 JButton button10;
    private JButton button11;
    private JButton button12;
    private JButton button13;
    private JButton button14;
    private JButton button15;
    private JButton button16;
    private JButton button17;
    private JButton button18;
    private JButton button19;
    private JButton button20;
    private JButton button21;
    private JButton button22;
    private JButton button23;
    private JButton button24;
    private JButton button25;
    private JButton button26;
    private JButton button27;
    private JButton button28;
    private JButton button29;
    private JButton button30;
    private JButton button31;
    private JButton button32;
    private JButton button33;
    private JButton button34;
    private JButton button35;
    private JButton button36;
    private JButton button37;
    private JButton button38;
    private JButton button39;
    private JButton button40;
    private JButton button41;
    private JButton button42;
    private JButton button43;
    private JButton button44;
    private JButton button45;
    private JButton button46;
    private JButton button47;
    private JButton button48;
    private JButton button49;
    private JButton button50;
    private JButton button51;
    private JButton button52;
    private JButton button53;
    private JButton button54;
    private JButton button55;
    private JButton button56;
    private JButton button57;
    private JButton button58;
    private JButton button59;
    private JButton button60;
    private JButton button61;
    private JButton button62;
    private JButton button63;
    private JButton button64;
    private JButton button65;
    private JButton button66;
    private JButton button67;
    private JButton button68;
    private JButton button69;
    private JButton button70;
    private JButton button71;
    private JButton button72;
    private JButton button73;
    private JButton button74;
    private JButton button75;
    private JButton button76;
    private JButton button77;
    private JButton button78;
    private JButton button79;
    private JButton button80;
    private JButton button81;
    private JPanel panelButton;
    private JLabel oben;
    private JButton anzeige;
    private  String string;
    private  String string2;
    private  String string3;
    private  String string4;
    private  String string5;
    private  String string6;
    private  String string7;
    private  String string8;
    private  String string9;
    private  String stringa;
    private int x1 =1;
   private int y1 =0;
   private int x2 =1;
   private int y2 =0;
   private int x3 =1;
   private int y3 =0;
   private int y4 =0;
   private int x4 =1;
   private int y5 =0;
   private int x5 =1;
   private int x6 =1;
   private int y6 =0;
   private int x7 =1;
   private int y7 =0;
   private int x8 =1;
   private int y8 =0;
   private int x9 = 1;
   private int y9 =0;
   private int o;
   private int q;
 
   

   int[][] sudo = {{1, 0, 0, 6, 0, 0, 0, 0, 0},
                {0, 2, 0, 0, 0, 0, 9, 0, 0},
                {0, 0, 3, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 4, 0, 0, 0, 1, 0},
                {0, 4, 0, 0, 5, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 6, 0, 9, 0},
                {0, 0, 2, 0, 0, 0, 7, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 8, 0},
                {3, 0, 0, 5, 0, 0, 0, 0, 9}};
    public secondgui()
    {
        super("Sudoku");
        setLocation(300,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Das BorderLayout wird erzeugt
        getContentPane().setLayout(new BorderLayout(5,5));  

        //Buttons erzeugen
        JButton[] buttons = new JButton[81];

       



        //Panels erzeugen auf einem GridLayout
        panelButton = new JPanel(new GridLayout(9,9));
        
        //Auf Panel Buttons packen
        
        for(int i = 0; i < buttons.length; i++){
            buttons[i] = new JButton("1");
            panelButton.add(buttons[i]);
        }
        
      
        //Listener für Buttons
        
        addButtonListener1(button1);
        addButtonListener2(button2);
        addButtonListener3(button3);
        addButtonListener4(button4);
        addButtonListener5(button5);
        addButtonListener6(button6);
        addButtonListener7(button7);
        addButtonListener8(button8);
        addButtonListener9(button9);
      
        
        

        //Labels erzeugen
        oben = new JLabel("Sudoku");
        //Label zentrieren
        oben.setHorizontalAlignment(JLabel.CENTER); 
        anzeige = new JButton("Prüfen");
        addButtonListenerPruefen(anzeige);

        //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);
        getContentPane().add(BorderLayout.EAST, anzeige);
       

        pack();
        setVisible(true);

        

    }

    public static void main(String[] args)
    {
        secondgui g = new secondgui();
    }

    private void addButtonListener1(JButton B)/>
    {
        b.addActionListener(new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            { 
                eingabe(ae.getActionCommand());
                int i = Integer.parseInt(button1.getText());
                if (x1==1)
                {
                 
                    if (i<9)
                    {
                    i= i+1;
                    String string = String.valueOf(i);
                    
                    button1.setText(string);
                    y1= y1+1;
                    }
                   if (y1==8)
                   { x1=0;
                     y1=0;
                    }
                }
                else
                    {
                     
                    if (i>0)
                            {   
                                i= i-1;
                                String string = String.valueOf(i);
                                
                                button1.setText(string);
                                y1= y1+1;
                               }
                    if (y1==8)
                   { x1=1;
                       y1=0;
                    }
                   }
                   
                }
                 
            
        });
    }
    private void addButtonListener2(JButton B)/>
    {
        b.addActionListener(new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            { 
                eingabe(ae.getActionCommand());
                int h = Integer.parseInt(button2.getText());
                if (x2==1)
                {
                 
                    if (h<9)
                    {
                    h= h+1;
                    String string2 = String.valueOf(h);
                   
                    button2.setText(string2);
                    y2= y2+1;
                    }
                   if (y2==8)
                   { x2=0;
                     y2=0;
                    }
                }
                else
                    {
                     
                    if (h>0)
                            {   
                                h= h-1;
                                String string2 = String.valueOf(h);
                                
                                button2.setText(string2);
                                y2= y2+1;
                               }
                    if (y2==8)
                   { x2=1;
                       y2=0;
                    }
                   }
                   
                }
                 
            
        });
    }
     private void addButtonListener3(JButton B)/>
    {
        b.addActionListener(new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            { 
                eingabe(ae.getActionCommand());
                int j = Integer.parseInt(button3.getText());
                if (x3==1)
                {
                 
                    if (j<9)
                    {
                    j= j+1;
                    String string3 = String.valueOf(j);
                    button3.setText(string3);
                    y3= y3+1;
                    }
                   if (y3==8)
                   { x3=0;
                     y3=0;
                    }
                }
                else
                    {
                     
                    if (j>0)
                            {   
                                j= j-1;
                                String string = String.valueOf(j);
                                button3.setText(string);
                                y3= y3+1;
                               }
                    if (y3==8)
                   { x3=1;
                       y3=0;
                    }
                   }
                   
                }
                 
            
        });
    }
    private void addButtonListener4(JButton B)/>
    {
        b.addActionListener(new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            { 
                eingabe(ae.getActionCommand());
                int h = Integer.parseInt(button4.getText());
                if (x4==1)
                {
                 
                    if (h<9)
                    {
                    h= h+1;
                    String string4 = String.valueOf(h);
                    button4.setText(string4);
                    y4= y4+1;
                    }
                   if (y4==8)
                   { x4=0;
                     y4=0;
                    }
                }
                else
                    {
                     
                    if (h>0)
                            {   
                                h= h-1;
                                String string4 = String.valueOf(h);
                                button4.setText(string4);
                                y4= y4+1;
                               }
                    if (y4==8)
                   { x4=1;
                       y4=0;
                    }
                   }
                   
                }
                 
            
        });
    }
    private void addButtonListener5(JButton B)/>
    {
        b.addActionListener(new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            { 
                eingabe(ae.getActionCommand());
                int h = Integer.parseInt(button5.getText());
                if (x5==1)
                {
                 
                    if (h<9)
                    {
                    h= h+1;
                    String string5 = String.valueOf(h);
                    button5.setText(string5);
                    y5= y5+1;
                    }
                   if (y5==8)
                   { x5=0;
                     y5=0;
                    }
                }
                else
                    {
                     
                    if (h>0)
                            {   
                                h= h-1;
                                String string5 = String.valueOf(h);
                                button5.setText(string5);
                                y5= y5+1;
                               }
                    if (y5==8)
                   { x5=1;
                       y5=0;
                    }
                   }
                   
                }
                 
            
        });
    }
    private void addButtonListener6(JButton B)/>
    {
        b.addActionListener(new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            { 
                eingabe(ae.getActionCommand());
                int h = Integer.parseInt(button6.getText());
                if (x6==1)
                {
                 
                    if (h<9)
                    {
                    h= h+1;
                    String string6 = String.valueOf(h);
                    button6.setText(string6);
                    y6= y6+1;
                    }
                   if (y6==8)
                   { x6=0;
                     y6=0;
                    }
                }
                else
                    {
                     
                    if (h>0)
                            {   
                                h= h-1;
                                String string6 = String.valueOf(h);
                                button6.setText(string6);
                                y6= y6+1;
                               }
                    if (y6==8)
                   { x6=1;
                       y6=0;
                    }
                   }
                   
                }
                 
            
        });
    }
    private void addButtonListener7(JButton B)/>
    {
        b.addActionListener(new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            { 
                eingabe(ae.getActionCommand());
                int h = Integer.parseInt(button7.getText());
                if (x7==1)
                {
                 
                    if (h<9)
                    {
                    h= h+1;
                    String string7 = String.valueOf(h);
                    button7.setText(string7);
                    y7= y7+1;
                    }
                   if (y7==8)
                   { x7=0;
                     y7=0;
                    }
                }
                else
                    {
                     
                    if (h>0)
                            {   
                                h= h-1;
                                String string7 = String.valueOf(h);
                                button7.setText(string7);
                                y7= y7+1;
                               }
                    if (y7==8)
                   { x7=1;
                       y7=0;
                    }
                   }
                   
                }
                 
            
        });
    }
    private void addButtonListener8(JButton B)/>
    {
        b.addActionListener(new ActionListener()
        { 
            public void actionPerformed(ActionEvent ae) 
            { 
                eingabe(ae.getActionCommand());
                int h = Integer.parseInt(button8.getText());
                if (x8==1)
                {
                 
                    if (h<9)
                    {
                    h= h+1;
                    String string8 = String.valueOf(h);
                    button8.setText(string8);
                    y8= y8+1;
                    }
                   if (y8==8)
                   { x8=0;
                     y8=0;
                    }
                }
                else
                    {
                     
                    if (h>0)
                            {   
                                h= h-1;
                                String string8 = String.valueOf(h);
                                button8.setText(string8);
                                y8= y8+1;
                               }
                    if (y8==8)
                   { x8=1;
                       y8=0;
                    }
                   }
                   
                }
                 
            
        });
    }
    private void addButtonListener9(JButton B)/>
    {
        b.addActionListener(new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            { 
                eingabe(ae.getActionCommand());
                int h = Integer.parseInt(button9.getText());
                if (x9==1)
                {
                 
                    if (h<9)
                    {
                    h= h+1;
                    String string9 = String.valueOf(h);
                    button9.setText(string9);
                    y9= y9+1;
                    }
                   if (y9==8)
                   { x9=0;
                     y9=0;
                    }
                }
                else
                    {
                     
                    if (h>0)
                            {   
                                h= h-1;
                                String string9 = String.valueOf(h);
                                
                                button9.setText(string9);
                                y9= y9+1;
                               }
                    if (y9==8)
                   { x9=1;
                       y9=0;
                    }
                   }
                   
                }
                 
            
        });
    }
    private void addButtonListenerPruefen(JButton B)/>
    {
        anzeige.addActionListener(new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            { 
                eingabe(ae.getActionCommand());
                
                if (string.equals("1") && string2.equals("2") || string2.equals("3") || string2.equals("4")|| string2.equals("5") || string2.equals("6") || string2.equals("7") || string2.equals("8") || string2.equals("9"))
                   {
                       button1.setBackground(Color.GREEN);
                       anzeige.setText("Richtig");
                    }
                }
                 
            
        });
    }

    private void eingabe(String a)
    {
        anzeige.setText("Prüfen");
    }    
}




Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10700
  • View blog
  • Posts: 39,813
  • Joined: 27-December 08

Re: ActionListener NullPointerException

Posted 10 January 2013 - 03:14 PM

Please get in the habit of posting the error messages exactly as they appear. Thank you!
Was This Post Helpful? 0
  • +
  • -

#14 Buttertoast  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 60
  • Joined: 10-January 13

Re: ActionListener NullPointerException

Posted 10 January 2013 - 03:39 PM

sorry, its a runtime error
java.lang.NullPointerException
	at secondgui.addButtonListener1(secondgui.java:208)
	at secondgui.<init>(secondgui.java:164)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
	at bluej.runtime.ExecServer$3.run(ExecServer.java:740)


Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10700
  • View blog
  • Posts: 39,813
  • Joined: 27-December 08

Re: ActionListener NullPointerException

Posted 10 January 2013 - 03:41 PM

The whole point of the array was to replace the button1, button2, button3, ..., button81 variables. So use the array rather than passing uninitialized JButtons to a method. That's why you are getting the NullPointerException. By default, Objects are initialized to null. You have to explicitly initialize them so they point to a new object of the appropriate type (ie., a new JButton). Then you can work with the variable.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2