ActionListener NullPointerException

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 Buttertoast  Icon User is offline

  • D.I.C Head

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

Re: ActionListener NullPointerException

Posted 11 January 2013 - 03:20 AM

could you give me an example?
Was This Post Helpful? 0
  • +
  • -

#17 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1954
  • View blog
  • Posts: 4,053
  • Joined: 11-December 07

Re: ActionListener NullPointerException

Posted 11 January 2013 - 04:22 AM

One thing at a time macos. Let's get to green before we refactor.

addButtonListenerPruefen(anzeige);

// snip lines of code

anzeige = new JButton("Prüfen");



As goose pointed out, you are trying to add an action listener to the button before you create the button. Move the anzeige = new JButton("Prüfen"); line to where you create the other buttons and this particular error should be resolved.

Once you fix all the errors in your code, go back and look ad macos's posts about arrays. Your code has a lot of repetition and probably be done in somewhere between 30-100 lines.
Was This Post Helpful? 0
  • +
  • -

#18 Buttertoast  Icon User is offline

  • D.I.C Head

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

Re: ActionListener NullPointerException

Posted 12 January 2013 - 06:18 AM

shouldnt it be possible to do the buttonlistenerthing with arrays,too?
i tried it but it didnt work:

 for(int i = 1; i < 9; i++){
        addButtonListener[i](button[i]);
      }


Was This Post Helpful? 0
  • +
  • -

#19 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1954
  • View blog
  • Posts: 4,053
  • Joined: 11-December 07

Re: ActionListener NullPointerException

Posted 12 January 2013 - 07:24 AM

Almost right!

addButtonListener is a method you have written. There is only one copy of that method. It should be more like this:

for(int i = 0; i < button.length; i++){
    JButton x = button[i];
    addButtonListener(x);
}


I've changed 2 things. I'm passing a button yo your method and I've made your loop srtart at zero and go to the end of your array. There is a nicer short form of this loop:

for(JButton x : button) {
    addButtonListener(x);
}


This post has been edited by cfoley: 12 January 2013 - 07:25 AM

Was This Post Helpful? 0
  • +
  • -

#20 Buttertoast  Icon User is offline

  • D.I.C Head

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

Re: ActionListener NullPointerException

Posted 12 January 2013 - 07:33 AM

i tried it with your code but i get the console error message:
cannot find symbol - method addButtonListener(javax.swing.JButton)

i have to rename my methods,right?
but to what?
Was This Post Helpful? 0
  • +
  • -

#21 Buttertoast  Icon User is offline

  • D.I.C Head

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

Re: ActionListener NullPointerException

Posted 12 January 2013 - 07:52 AM

okay, i got it to work, but now i have another problem,
i always get an error "cannot find symbol - variable button"
and i want to make this method to work for every button, but how can i do it that this method works for every button

private void addButtonListener(JButton B)/>
    {
        b.addActionListener(new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            { 
                eingabe(ae.getActionCommand());
                int i = Integer.parseInt(button[1]);
                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;
                    }
                   }
                   
                }
                 
            
        });
    }



this is the whole code:

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 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;
   public JTextField tf = new JTextField("Größe des Sudokus(9 für 9x9 oder 6 für 6x6)", 3);
 
   

   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[] button = new JButton[81];

       



        //Panels erzeugen auf einem GridLayout
        panelButton = new JPanel(new GridLayout(9,9));
        
        //Auf Panel Buttons packen
        
        for(int i = 0; i < button.length; i++){
            button[i] = new JButton("1");
            panelButton.add(button[i]);
        }
        
      
        //Listener für Buttons
        
       for(int i = 1; i < button.length; i++){
           JButton x = button[i];
           addButtonListener(x);
         }



   
        
        

        //Labels & Textfeld erzeugen
        
        
        tf.setSize(200, 24);
        oben = new JLabel("Sudoku");
        //Label & Textfeld zentrieren
        tf.setHorizontalAlignment(JTextField.LEFT);
        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);
        getContentPane().add(BorderLayout.NORTH,tf);
        

        //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 addButtonListener(JButton B)/>
    {
        b.addActionListener(new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            { 
                eingabe(ae.getActionCommand());
                int i = Integer.parseInt(button[1]);
                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");
                    }
                }
                 
            
        });
    }

    public void GroßeDesSudokus()
    {
       
       String z =tf.getText();
       
       if (z.equals("6"))
       {
           JButton[] button = new JButton[36];
           for(int i = 0; i < button.length; i++){
            button[i] = new JButton("1");
            panelButton.add(button[i]);
                                                 }
        }
                else
                    {tf.setText("Die Angabe ist nicht möglich.");
                        }
       if (z.equals("9"))
       {
           JButton[] button = new JButton[81];
           for(int i = 0; i < button.length; i++){
            button[i] = new JButton("1");
            panelButton.add(button[i]);
        }
        }
                 else
                    {tf.setText("Die Angabe ist nicht möglich.");
                        }
    }
     public void actionPerformed(ActionEvent event)   
     {    
          String cmd = event.getActionCommand();
          if (cmd.equals("Prüfen"))
          {                              
                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);
                       button2.setBackground(Color.GREEN);
                       anzeige.setText("Richtig");
                    }
                }
          }
          
     

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




Was This Post Helpful? 0
  • +
  • -

#22 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1954
  • View blog
  • Posts: 4,053
  • Joined: 11-December 07

Re: ActionListener NullPointerException

Posted 12 January 2013 - 09:07 AM

Specifically which line gives that error?
Was This Post Helpful? 0
  • +
  • -

#23 Buttertoast  Icon User is offline

  • D.I.C Head

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

Re: ActionListener NullPointerException

Posted 12 January 2013 - 10:25 AM

View Postcfoley, on 12 January 2013 - 09:07 AM, said:

Specifically which line gives that error?

The array button [1] and the variable button1, 2, 3...
Was This Post Helpful? 0
  • +
  • -

#24 Buttertoast  Icon User is offline

  • D.I.C Head

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

Re: ActionListener NullPointerException

Posted 12 January 2013 - 11:40 AM

its in line 135
Was This Post Helpful? 0
  • +
  • -

#25 Buttertoast  Icon User is offline

  • D.I.C Head

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

Re: ActionListener NullPointerException

Posted 12 January 2013 - 05:13 PM

got it work..

now my problem is that i want the method addButtonListener to work for each button on its own, at this moment every button works with the same method,
the sense of my method is to increase the number that is written on the button and it should work for every button, but if i press button[2] button[1] to gets +1 and not button[2]
does anybody know a solution?

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

public class secondgui extends JFrame implements ActionListener
{
    private JPanel panelButton;
    private JLabel oben;
    private JButton anzeige;
   
    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;
   protected JTextField tf = new JTextField("Größe des Sudokus(9 für 9x9 oder 6 für 6x6)", 3);
   public JButton[] button = new JButton[81];
   public String[] string = new String[81];
   

   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
        

       



        //Panels erzeugen auf einem GridLayout
        panelButton = new JPanel(new GridLayout(9,9));
        
        //Auf Panel Buttons packen
        
        for(int i = 0; i < button.length; i++){
            button[i] = new JButton("1");
            panelButton.add(button[i]);
        }
        
      
        //Listener für Buttons
       
        
       for(int i = 1; i < button.length; i++){
           JButton x = button[i];
           addButtonListener(x);
         }
         //Listener für Textfeld
        
          tf.addActionListener(this);

   
        
        

        //Labels & Textfeld erzeugen
        
       
        tf.setSize(200, 24);
        oben = new JLabel("Sudoku");
        //Label & Textfeld zentrieren
        tf.setHorizontalAlignment(JTextField.LEFT);
        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);
        getContentPane().add(BorderLayout.NORTH,tf);
        

        //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 addButtonListener(JButton B)/>
    {
        b.addActionListener(new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            { 
                eingabe(ae.getActionCommand());
                int i = Integer.parseInt(button[0].getText());
                if (x1==1)
                {
                 
                    if (i<9)
                    {
                    i= i+1;
                     string[0] = String.valueOf(i);
                    
                    button[0].setText(string[0]);
                    y1= y1+1;
                    }
                   if (y1==8)
                   { x1=0;
                     y1=0;
                    }
                }
                else
                    {
                     
                    if (i>0)
                            {   
                                i= i-1;
                                string[0] = String.valueOf(i);
                                
                                button[0].setText(string[0]);
                                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(button[1].getText());
                if (x2==1)
                {
                 
                    if (h<9)
                    {
                    h= h+1;
                    string[1] = String.valueOf(h);
                   
                    button[1].setText(string[1]);
                    y2= y2+1;
                    }
                   if (y2==8)
                   { x2=0;
                     y2=0;
                    }
                }
                else
                    {
                     
                    if (h>0)
                            {   
                                h= h-1;
                                string[1] = String.valueOf(h);
                                
                                button[1].setText(string[1]);
                                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(button[2].getText());
                if (x3==1)
                {
                 
                    if (j<9)
                    {
                    j= j+1;
                    string[2] = String.valueOf(j);
                    button[2].setText(string[2]);
                    y3= y3+1;
                    }
                   if (y3==8)
                   { x3=0;
                     y3=0;
                    }
                }
                else
                    {
                     
                    if (j>0)
                            {   
                                j= j-1;
                                string[2] = String.valueOf(j);
                                button[2].setText(string[2]);
                                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(button[3].getText());
                if (x4==1)
                {
                 
                    if (h<9)
                    {
                    h= h+1;
                    string[3] = String.valueOf(h);
                    button[3].setText(string[3]);
                    y4= y4+1;
                    }
                   if (y4==8)
                   { x4=0;
                     y4=0;
                    }
                }
                else
                    {
                     
                    if (h>0)
                            {   
                                h= h-1;
                                string[3] = String.valueOf(h);
                                button[3].setText(string[3]);
                                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(button[4].getText());
                if (x5==1)
                {
                 
                    if (h<9)
                    {
                    h= h+1;
                    string[4] = String.valueOf(h);
                    button[4].setText(string[4]);
                    y5= y5+1;
                    }
                   if (y5==8)
                   { x5=0;
                     y5=0;
                    }
                }
                else
                    {
                     
                    if (h>0)
                            {   
                                h= h-1;
                                string[4] = String.valueOf(h);
                                button[4].setText(string[4]);
                                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(button[5].getText());
                if (x6==1)
                {
                 
                    if (h<9)
                    {
                    h= h+1;
                    string[5] = String.valueOf(h);
                    button[5].setText(string[5]);
                    y6= y6+1;
                    }
                   if (y6==8)
                   { x6=0;
                     y6=0;
                    }
                }
                else
                    {
                     
                    if (h>0)
                            {   
                                h= h-1;
                                string[5] = String.valueOf(h);
                                button[5].setText(string[5]);
                                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(button[6].getText());
                if (x7==1)
                {
                 
                    if (h<9)
                    {
                    h= h+1;
                    string[6] = String.valueOf(h);
                    button[6].setText(string[6]);
                    y7= y7+1;
                    }
                   if (y7==8)
                   { x7=0;
                     y7=0;
                    }
                }
                else
                    {
                     
                    if (h>0)
                            {   
                                h= h-1;
                                string[6] = String.valueOf(h);
                                button[6].setText(string[6]);
                                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(button[7].getText());
                if (x8==1)
                {
                 
                    if (h<9)
                    {
                    h= h+1;
                    string[7] = String.valueOf(h);
                    button[7].setText(string[7]);
                    y8= y8+1;
                    }
                   if (y8==8)
                   { x8=0;
                     y8=0;
                    }
                }
                else
                    {
                     
                    if (h>0)
                            {   
                                h= h-1;
                                string[7] = String.valueOf(h);
                                button[7].setText(string[7]);
                                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(button[8].getText());
                if (x9==1)
                {
                 
                    if (h<9)
                    {
                    h= h+1;
                    string[8]= String.valueOf(h);
                    button[8].setText(string[8]);
                    y9= y9+1;
                    }
                   if (y9==8)
                   { x9=0;
                     y9=0;
                    }
                }
                else
                    {
                     
                    if (h>0)
                            {   
                                h= h-1;
                                string[8] = String.valueOf(h);
                                
                                button[8].setText(string[8]);
                                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[0].equals("1") && string[1].equals("2") || string[1].equals("3") || string[1].equals("4")|| string[1].equals("5") || string[1].equals("6") || string[1].equals("7") || string[1].equals("8") || string[1].equals("9"))
                   {
                       button[1].setBackground(Color.GREEN);
                       anzeige.setText("Richtig");
                    }
                }
                 
            
        });
    }

    public void actionPerformed2(ActionEvent evt)
    {
       
       String z =tf.getText();
       
       if (z.equals("6"))
       {
           JButton[] button = new JButton[36];
           for(int i = 0; i < button.length; i++){
            button[i] = new JButton("1");
            panelButton.add(button[i]);
                                                 }
        }
                else
                    {tf.setText("Die Angabe ist nicht möglich.");
                        }
       if (z.equals("9"))
       {
           JButton[] button = new JButton[81];
           for(int i = 0; i < button.length; i++){
            button[i] = new JButton("1");
            panelButton.add(button[i]);
        }
        }
                 else
                    {tf.setText("Die Angabe ist nicht möglich.");
                        }
    }
     public void actionPerformed(ActionEvent event)   
     {    
          String cmd = event.getActionCommand();
          if (cmd.equals("Prüfen"))
          {                              
                if (string[0].equals("1") && string[1].equals("2") || string[1].equals("3") || string[1].equals("4")|| string[1].equals("5") || string[1].equals("6") || string[1].equals("7") || string[1].equals("8") || string[1].equals("9"))
                   {
                       button[1].setBackground(Color.GREEN);
                       button[2].setBackground(Color.GREEN);
                       anzeige.setText("Richtig");
                    }
                }
          }
          
     

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


Was This Post Helpful? 0
  • +
  • -

#26 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1954
  • View blog
  • Posts: 4,053
  • Joined: 11-December 07

Re: ActionListener NullPointerException

Posted 14 January 2013 - 05:05 AM

I'm not sure I follow the code in your action listener but I'll describe one way of adding 1 to the number it displays. All the code below goes into your actionPerformad() method.

First of all you have to decide which button was pressed. The You can get the item that was clicked from the ActionEvent ae:

Object source = ae.getSource();


Assuming you have only added this action listener to buttons, you can savely cast it to a button like this:

JButton pressed = (JButton) source;


Now get the text and convert it to an int:

String text = pressed.getText();
int value = Integer.parseInt(text);


Calculate the new value, convert to a String and replace the text on the button:

				int newValue = value + 1;
				String newText = Integer.toString(newValue);
				pressed.setText(newText);


You might have to modify this to get exactly what you want but it should point you in the right direction.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2