我的ActionListener出了什么问题? (未知来源)

时间:2014-08-08 06:41:59

标签: java user-interface actionlistener

public class sesUygulamasi implements ActionListener {
    JButton azalt = new JButton("-");
    JButton arttır = new JButton("+");
    int defa=5;
    boolean[] liste=new boolean[10];
    JPanel birinci = new JPanel();
    JPanel ikinci = new JPanel();
    JPanel ucuncu = new JPanel();
    JPanel dorduncu = new JPanel();
    JPanel besinci = new JPanel();
    JPanel altinci = new JPanel();
    JPanel yedinci = new JPanel();
    JPanel sekizinci = new JPanel();
    JPanel dokuzuncu = new JPanel();
    JPanel onuncu = new JPanel();

    public JPanel createContentPane() {
        JPanel GUI = new JPanel();
        GUI.setLayout(null);
        JPanel yazilar = new JPanel();
        yazilar.setLayout(null);
        yazilar.setLocation(10, 0);
        yazilar.setSize(250, 30);
        GUI.add(yazilar);

        JLabel down = new JLabel("Volume-Down");
        down.setForeground(Color.black);
        down.setLocation(0, 0);
        down.setSize(100, 30);
        down.setHorizontalAlignment(0);
        yazilar.add(down);

        JLabel up = new JLabel("Volume-Up");
        up.setForeground(Color.black);
        up.setLocation(90, 0);
        up.setSize(100, 30);
        up.setHorizontalAlignment(0);
        yazilar.add(up);

        JPanel cubuklar = new JPanel();
        cubuklar.setLayout(null);
        cubuklar.setLocation(10, 20);
        cubuklar.setSize(225, 150);
        GUI.add(cubuklar);


        birinci.setBackground(Color.BLACK);
        birinci.setLocation(10, 140);
        birinci.setSize(10, 10);
        cubuklar.add(birinci);


        ikinci.setBackground(Color.BLACK);
        ikinci.setLocation(25, 130);
        ikinci.setSize(10, 20);
        cubuklar.add(ikinci);


        ucuncu.setBackground(Color.BLACK);
        ucuncu.setLocation(40, 120);
        ucuncu.setSize(10, 30);
        cubuklar.add(ucuncu);


        dorduncu.setBackground(Color.BLACK);
        dorduncu.setLocation(55, 110);
        dorduncu.setSize(10, 40);
        cubuklar.add(dorduncu);


        besinci.setBackground(Color.BLACK);
        besinci.setLocation(70, 100);
        besinci.setSize(10, 50);
        cubuklar.add(besinci);


        altinci.setBackground(Color.BLACK);
        altinci.setLocation(85, 90);
        altinci.setSize(10, 60);
        altinci.setVisible(false);
        cubuklar.add(altinci);


        yedinci.setBackground(Color.BLACK);
        yedinci.setLocation(100, 80);
        yedinci.setSize(10, 70);
        yedinci.setVisible(false);
        cubuklar.add(yedinci);


        sekizinci.setBackground(Color.BLACK);
        sekizinci.setLocation(115, 70);
        sekizinci.setSize(10, 80);
        sekizinci.setVisible(false);
        cubuklar.add(sekizinci);


        dokuzuncu.setBackground(Color.BLACK);
        dokuzuncu.setLocation(130, 60);
        dokuzuncu.setSize(10, 90);
        dokuzuncu.setVisible(false);
        cubuklar.add(dokuzuncu);


        onuncu.setBackground(Color.BLACK);
        onuncu.setLocation(145, 50);
        onuncu.setSize(10, 100);
        onuncu.setVisible(false);
        cubuklar.add(onuncu);

        JPanel butonlar = new JPanel();
        butonlar.setLayout(null);
        butonlar.setLocation(10, 200);
        butonlar.setSize(225, 50);
        GUI.add(butonlar);


        azalt.setLocation(25, 0);
        azalt.setSize(50, 30);
        azalt.addActionListener(this);
        butonlar.add(azalt);


        arttır.setLocation(100, 0);
        arttır.setSize(50, 30);
        arttır.addActionListener(this);
        butonlar.add(arttır);

        return GUI;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Ses Sistemi");
        JFrame.setDefaultLookAndFeelDecorated(true);
        sesUygulamasi demo = new sesUygulamasi();
        frame.setContentPane(demo.createContentPane());
        frame.setSize(225, 300);
        frame.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        if(arg0.getSource()==azalt){
            defa--;
            liste=sayac(defa);
            birinci.setVisible(liste [0]);
            ikinci.setVisible(liste [1]);
            ucuncu.setVisible(liste [2]);
            dorduncu.setVisible(liste [3]);
            besinci.setVisible(liste [4]);
            altinci.setVisible(liste [5]);
            yedinci.setVisible(liste [6]);
            sekizinci.setVisible(liste [7]);
            dokuzuncu.setVisible(liste [8]);
            onuncu.setVisible(liste [9]);

        }
        if(arg0.getSource()==arttır){
            defa++;
            liste=sayac(defa);
            birinci.setVisible(liste [0]);
            ikinci.setVisible(liste [1]);
            ucuncu.setVisible(liste [2]);
            dorduncu.setVisible(liste [3]);
            besinci.setVisible(liste [4]);
            altinci.setVisible(liste [5]);
            yedinci.setVisible(liste [6]);
            sekizinci.setVisible(liste [7]);
            dokuzuncu.setVisible(liste [8]);
            onuncu.setVisible(liste [9]);
        }
    }

    private boolean[] sayac(int i) {
        for (int x=0;x<liste.length;x++){
            if(i-1>=x){
                liste[x]=true;
            }
            else{
                liste[x]=false;
            }
        }
        return null;
    }
}

这是我得到的堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at sesUygulamasi.actionPerformed(sesUygulamasi.java:170)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

我该如何解决这个问题?该程序就像一个混音器。如果您点击"-"按钮,声音会降低,如果您点击"+"按钮,声音会更高。

1 个答案:

答案 0 :(得分:2)

您正在从null方法

返回sayac
private boolean[] sayac(int i) {
    for (int x = 0; x < liste.length; x++) {
        if (i - 1 >= x) {
            liste[x] = true;
        } else {
            liste[x] = false;
        }
    }
    return null;
}

这意味着当您尝试在actionPerformed方法中访问它时,会触发NullPointerException ...

if (arg0.getSource() == azalt) {
    defa--;
    liste = sayac(defa); // I'm null
    birinci.setVisible(liste[0]);
    //...
}
if (arg0.getSource() == arttir) {
    defa++;
    liste = sayac(defa); // I'm null
    birinci.setVisible(liste[0]);
    //..

考虑到这是尝试IDE和Java的调试器功能的一个很好的机会。在指示行之前添加一个断点,在调试模式下运行程序并检查变量并向上走回堆栈跟踪以查看它来自哪里......