计时器在会议条件下给出错误

时间:2015-01-28 05:22:14

标签: java swing timertask

好吧,伙计们,我遇到了一个新问题,我做了一个计时器,检查按钮是否每秒都符合某些要求,并且我知道这不是最好的方法,但我不会长时间使用这个程序所以我只是想让它发挥作用。

我遇到的问题是计时器给出了以下错误:

Exception in thread "Timer-0" java.lang.NullPointerException
    at proyecto.Diseños$RemindTask.run(Diseños.java:3561)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)

第3561行是:

if(Boton61.getBackground() == Color.red && Boton51.getBackground() == Color.red && Boton41.getBackground() == Color.red && Boton31.getBackground() == Color.red 
                && colores.colorjugador[0] == 1) System.out.println("El jugador ha ganado !");

完整的代码是:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package proyecto;


import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import static proyecto.Proyecto.turnosjugador1;
import static proyecto.Proyecto.turnosjugador2;

import java.util.Timer;
import java.util.TimerTask;




/**
 *
 * @author Cristian
 *
 * */






public class Diseños extends JComponent implements ActionListener {

   JButton Boton1, Boton2, Boton3, Boton4, Boton5, Boton6, Boton7;

   JButton Boton11,Boton21,Boton31,Boton41,Boton51,Boton61;
   JButton Boton12,Boton22,Boton32,Boton42,Boton52,Boton62;
   JButton Boton13,Boton23,Boton33,Boton43,Boton53,Boton63;
   JButton Boton14,Boton24,Boton34,Boton44,Boton54,Boton64;
   JButton Boton15,Boton25,Boton35,Boton45,Boton55,Boton65;
   JButton Boton16,Boton26,Boton36,Boton46,Boton56,Boton66;
   JButton Boton17,Boton27,Boton37,Boton47,Boton57,Boton67;




  public Diseños()
  {

        Timer timer;
        timer = new Timer();
        timer.schedule(new RemindTask(), 0, //initial delay
        1 * 1000);

   Boton1=new JButton("lol"); 
    Boton1.setBounds(90, 410, 30, 30); 
    Boton1.setBackground(Color.black);
    Component agregar1 = add(Boton1);
    Boton1.addActionListener(this);

    Boton2=new JButton("lol"); 
    Boton2.setBounds(150, 410, 30, 30); 
    Boton2.setBackground(Color.black);
    Component agregar2 = add(Boton2);
    Boton2.addActionListener(this);

    Boton3=new JButton("lol"); 
    Boton3.setBounds(210, 410, 30, 30); 
    Boton3.setBackground(Color.black);
    Component agregar3 = add(Boton3);
    Boton3.addActionListener(this);

    Boton4=new JButton("lol"); 
    Boton4.setBounds(270, 410, 30, 30); 
    Boton4.setBackground(Color.black);
    Component agregar4 = add(Boton4);
    Boton4.addActionListener(this);

    Boton5=new JButton("lol"); 
    Boton5.setBounds(330, 410, 30, 30); 
    Boton5.setBackground(Color.black);
    Component agregar5 = add(Boton5);
    Boton5.addActionListener(this);

    Boton6=new JButton("lol"); 
    Boton6.setBounds(390, 410, 30, 30); 
    Boton6.setBackground(Color.black);
    Component agregar6 = add(Boton6);
    Boton6.addActionListener(this);

    Boton7=new JButton("lol"); 
    Boton7.setBounds(450, 410, 30, 30); 
    Boton7.setBackground(Color.black);
    Component agregar7 = add(Boton7);
    Boton7.addActionListener(this);


    Boton11=new JButton(""); 
    Boton11.setBounds(85, 10, 40, 60); 
    Boton11.setBackground(Color.white);
    Component agregar11 = add(Boton11);
    Boton11.addActionListener(this);

    Boton21=new JButton(""); 
    Boton21.setBounds(85, 70, 40, 60); 
    Boton21.setBackground(Color.white);
    Component agregar21 = add(Boton21);
    Boton21.addActionListener(this);

    Boton31=new JButton(""); 
    Boton31.setBounds(85, 130, 40, 60); 
    Boton31.setBackground(Color.white);
    Component agregar31 = add(Boton31);
    Boton31.addActionListener(this);

    Boton41=new JButton(""); 
    Boton41.setBounds(85, 190, 40, 60); 
    Boton41.setBackground(Color.white);
    Component agregar41 = add(Boton41);
    Boton41.addActionListener(this);

    Boton51=new JButton(""); 
    Boton51.setBounds(85, 250, 40, 60); 
    Boton51.setBackground(Color.white);
    Component agregar51 = add(Boton51);
    Boton51.addActionListener(this);

    Boton61=new JButton(""); 
    Boton61.setBounds(85, 310, 40, 60); 
    Boton61.setBackground(Color.white);
    Component agregar61 = add(Boton61);
    Boton61.addActionListener(this);


    Boton12=new JButton(""); 
    Boton12.setBounds(145, 10, 40, 60); 
    Boton12.setBackground(Color.white);
    Component agregar12 = add(Boton12);
    Boton12.addActionListener(this);

    Boton22=new JButton(""); 
    Boton22.setBounds(145, 70, 40, 60); 
    Boton22.setBackground(Color.white);
    Component agregar22 = add(Boton22);
    Boton22.addActionListener(this);

    Boton32=new JButton(""); 
    Boton32.setBounds(145, 130, 40, 60); 
    Boton32.setBackground(Color.white);
    Component agregar32 = add(Boton32);
    Boton32.addActionListener(this);

    Boton42=new JButton(""); 
    Boton42.setBounds(145, 190, 40, 60); 
    Boton42.setBackground(Color.white);
    Component agregar42 = add(Boton42);
    Boton42.addActionListener(this);

    Boton52=new JButton(""); 
    Boton52.setBounds(145, 250, 40, 60); 
    Boton52.setBackground(Color.white);
    Component agregar52 = add(Boton52);
    Boton52.addActionListener(this);

    Boton62=new JButton(""); 
    Boton62.setBounds(145, 310, 40, 60); 
    Boton62.setBackground(Color.white);
    Component agregar62 = add(Boton62);
    Boton62.addActionListener(this);

    Boton13=new JButton(""); 
    Boton13.setBounds(205, 10, 40, 60); 
    Boton13.setBackground(Color.white);
    Component agregar13 = add(Boton13);
    Boton13.addActionListener(this);

    Boton23=new JButton(""); 
    Boton23.setBounds(205, 70, 40, 60); 
    Boton23.setBackground(Color.white);
    Component agregar23 = add(Boton23);
    Boton23.addActionListener(this);

    Boton33=new JButton(""); 
    Boton33.setBounds(205, 130, 40, 60); 
    Boton33.setBackground(Color.white);
    Component agregar33 = add(Boton33);
    Boton33.addActionListener(this);

    Boton43=new JButton(""); 
    Boton43.setBounds(205, 190, 40, 60); 
    Boton43.setBackground(Color.white);
    Component agregar43 = add(Boton43);
    Boton43.addActionListener(this);

    Boton53=new JButton(""); 
    Boton53.setBounds(205, 250, 40, 60); 
    Boton53.setBackground(Color.white);
    Component agregar53 = add(Boton53);
    Boton53.addActionListener(this);

    Boton63=new JButton(""); 
    Boton63.setBounds(205, 310, 40, 60); 
    Boton63.setBackground(Color.white);
    Component agregar63 = add(Boton63);
    Boton63.addActionListener(this);

    Boton14=new JButton(""); 
    Boton14.setBounds(265, 10, 40, 60); 
    Boton14.setBackground(Color.white);
    Component agregar14 = add(Boton14);
    Boton14.addActionListener(this);

    Boton24=new JButton(""); 
    Boton24.setBounds(265, 70, 40, 60); 
    Boton24.setBackground(Color.white);
    Component agregar24 = add(Boton24);
    Boton24.addActionListener(this);

    Boton34=new JButton(""); 
    Boton34.setBounds(265, 130, 40, 60); 
    Boton34.setBackground(Color.white);
    Component agregar34 = add(Boton34);
    Boton34.addActionListener(this);

    Boton44=new JButton(""); 
    Boton44.setBounds(265, 190, 40, 60); 
    Boton44.setBackground(Color.white);
    Component agregar44 = add(Boton44);
    Boton44.addActionListener(this);

    Boton54=new JButton(""); 
    Boton54.setBounds(265, 250, 40, 60); 
    Boton54.setBackground(Color.white);
    Component agregar54 = add(Boton54);
    Boton54.addActionListener(this);

    Boton64=new JButton(""); 
    Boton64.setBounds(265, 310, 40, 60); 
    Boton64.setBackground(Color.white);
    Component agregar64 = add(Boton64);
    Boton64.addActionListener(this);

    Boton15=new JButton(""); 
    Boton15.setBounds(325, 10, 40, 60); 
    Boton15.setBackground(Color.white);
    Component agregar15 = add(Boton15);
    Boton15.addActionListener(this);

    Boton25=new JButton(""); 
    Boton25.setBounds(325, 70, 40, 60); 
    Boton25.setBackground(Color.white);
    Component agregar25 = add(Boton25);
    Boton25.addActionListener(this);

    Boton35=new JButton(""); 
    Boton35.setBounds(325, 130, 40, 60); 
    Boton35.setBackground(Color.white);
    Component agregar35 = add(Boton35);
    Boton35.addActionListener(this);

    Boton45=new JButton(""); 
    Boton45.setBounds(325, 190, 40, 60); 
    Boton45.setBackground(Color.white);
    Component agregar45 = add(Boton45);
    Boton45.addActionListener(this);

    Boton55=new JButton(""); 
    Boton55.setBounds(325, 250, 40, 60); 
    Boton55.setBackground(Color.white);
    Component agregar55 = add(Boton55);
    Boton55.addActionListener(this);

    Boton65=new JButton(""); 
    Boton65.setBounds(325, 310, 40, 60); 
    Boton65.setBackground(Color.white);
    Component agregar65 = add(Boton65);
    Boton65.addActionListener(this);

    Boton16=new JButton(""); 
    Boton16.setBounds(385, 10, 40, 60); 
    Boton16.setBackground(Color.white);
    Component agregar16 = add(Boton16);
    Boton16.addActionListener(this);

    Boton26=new JButton(""); 
    Boton26.setBounds(385, 70, 40, 60); 
    Boton26.setBackground(Color.white);
    Component agregar26 = add(Boton26);
    Boton26.addActionListener(this);

    Boton36=new JButton(""); 
    Boton36.setBounds(385, 130, 40, 60); 
    Boton36.setBackground(Color.white);
    Component agregar36 = add(Boton36);
    Boton36.addActionListener(this);

    Boton46=new JButton(""); 
    Boton46.setBounds(385, 190, 40, 60); 
    Boton46.setBackground(Color.white);
    Component agregar46 = add(Boton46);
    Boton46.addActionListener(this);

    Boton56=new JButton(""); 
    Boton56.setBounds(385, 250, 40, 60); 
    Boton56.setBackground(Color.white);
    Component agregar56 = add(Boton56);
    Boton56.addActionListener(this);

    Boton66=new JButton(""); 
    Boton66.setBounds(385, 310, 40, 60); 
    Boton66.setBackground(Color.white);
    Component agregar66 = add(Boton66);
    Boton66.addActionListener(this);

    Boton17=new JButton(""); 
    Boton17.setBounds(445, 10, 40, 60); 
    Boton17.setBackground(Color.white);
    Component agregar17 = add(Boton17);
    Boton17.addActionListener(this);

    Boton27=new JButton(""); 
    Boton27.setBounds(445, 70, 40, 60); 
    Boton27.setBackground(Color.white);
    Component agregar27 = add(Boton27);
    Boton27.addActionListener(this);

    Boton37=new JButton(""); 
    Boton37.setBounds(445, 130, 40, 60); 
    Boton37.setBackground(Color.white);
    Component agregar37 = add(Boton37);
    Boton37.addActionListener(this);

    Boton47=new JButton(""); 
    Boton47.setBounds(445, 190, 40, 60); 
    Boton47.setBackground(Color.white);
    Component agregar47 = add(Boton47);
    Boton47.addActionListener(this);

    Boton57=new JButton(""); 
    Boton57.setBounds(445, 250, 40, 60); 
    Boton57.setBackground(Color.white);
    Component agregar57 = add(Boton57);
    Boton57.addActionListener(this);

    Boton67=new JButton(""); 
    Boton67.setBounds(445, 310, 40, 60); 
    Boton67.setBackground(Color.white);
    Component agregar67 = add(Boton67);
    Boton67.addActionListener(this);   



  }

   @Override
  public void paintComponent(Graphics g) {    



    super.paintComponent(g);
    g.drawRect (570, 10, 300, 200);
    g.setColor(Color.BLACK);
    g.fillRect(570, 10, 300, 200);
    g.drawRect (570, 250, 300, 200);
    g.setColor(Color.BLACK);
    g.fillRect(570, 250, 300, 200);
    g.setColor(Color.WHITE);
    Font oldFont=getFont();
    Font fuente=new Font("Primer Jugador", Font.BOLD, 14);
    g.setFont(fuente);
    g.drawString("Primer Jugador", 580, 30);
    Font titulo=new Font("Segundo Jugador", Font.BOLD, 14);
    g.setFont(titulo);
    g.drawString("Segundo Jugador", 580, 270);
    g.drawString("[ " + Proyecto.primernombre + " ]", 580, 50);
    g.drawString("[ " + Proyecto.segundonombre + " ]", 580, 290);
    g.setColor(Color.CYAN);
    g.fillRoundRect(10, 5, 550, 450, 50, 50);
    g.drawString("Turno Numero : " + turnosjugador1, 580, 70);
    g.drawString("Turno Numero : " + turnosjugador2, 580, 310);


  }











    }


class RemindTask extends TimerTask {


    @Override
    public void run() {

        if(Boton61.getBackground() == Color.red && Boton51.getBackground() == Color.red && Boton41.getBackground() == Color.red && Boton31.getBackground() == Color.red 
                && colores.colorjugador[0] == 1) System.out.println("El jugador ha ganado !");

        //timer.cancel(); //Not necessary because we call System.exit    




    }
  }



}

我知道很多行都浪费了不必要的空间,程序可以通过循环缩短,但我需要尽可能简单,以便其他学生理解它。

我只是想知道为什么计时器在计时器上使用IF时会给我这样的错误,非常感谢任何建议。

1 个答案:

答案 0 :(得分:1)

在初始化课程时,计时器可能已执行...如果你看一下JavaDocsdelay参数是

  

延迟 - 在执行任务之前以毫秒为单位的延迟

这意味着它可能几乎立即执行,这意味着您依赖的某些实例字段尚未初始化。

在处理Swing UI时,通常最好使用javax.swing.Timer,因为它在事件调度线程的上下文中调用ActionListener,从而降低了竞争条件的风险。

最好简单地将ActionListener附加到按钮上,并在点击时查看状态,但这只是我的意见