连接到另一个类时,AWT-EventQueue-0 java.lang.NullPointerException

时间:2014-11-19 14:01:34

标签: java nullpointerexception paint

基本上我得到了错误:

run:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at PaintV2.UIPanel$1.actionPerformed(PaintGUI.java:line marked)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6527)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6292)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4883)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4705)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4705)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:719)
    at java.awt.EventQueue$4.run(EventQueue.java:717)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)'

我正在尝试将我的GUI链接到颜色类,因为我正在制作一个绘图应用程序。我的问题是:我的代码出了什么问题?如果有人知道如何修复它,你能解释一下如何解决它吗

我的主要方法:

package PaintV2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;


public class PaintGUI{

    public static void main(String[] args ){

        JFrame frame = new JFrame("test panel 2");

        MainPanel panel = new MainPanel();

        UIPanel uip = new UIPanel(panel);

        frame.setLayout(new BorderLayout());
        frame.add(uip, BorderLayout.NORTH);

        frame.add(panel, BorderLayout.CENTER);
        frame.setSize(900, 900);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}

我的MainPanel课程:

class MainPanel extends JPanel {   
    int px, py; // radius
    public Color colvals;
    public Colour col;

    public MainPanel(){        
        this.addMouseMotionListener(new MouseMotionAdapter() {
            // store drag coordinates and repaint
                public void mouseDragged( MouseEvent event )
                {
                        px = event.getX();
                        py = event.getY();
                        repaint();
                }
                }); // end call to addMouseMotionListener
    }

    public void paint( Graphics g )
    {       
        g.setColor(Color.RED); 
        g.fillOval( px, py, 15, 15 );
    }
}    

我的UIPanel课程:

class UIPanel extends JPanel {
    public MainPanel gpanel;
    public Integer data;
    public Color colval;
    public Colour col;

    public UIPanel(MainPanel panel) {
        gpanel = panel;

        Box btnBox = Box.createHorizontalBox();
        btnBox.add(Box.createHorizontalGlue());
        JButton setBtn = new JButton("Set");

        //code for the colour chooser
        //decided to use a dropdow list with hashmap
        final DefaultComboBoxModel colour = new DefaultComboBoxModel();
        colour.addElement("Red");
        colour.addElement("Blue");
        colour.addElement("Yellow");
        colour.addElement("Green");
        final JComboBox colours = new JComboBox(colour);
        JScrollPane colourScroll = new JScrollPane(colours);


        JButton freeBtn = new JButton("FreeHand");
        JButton rectBtn = new JButton("Rectangle");
        JButton circBtn = new JButton("Circle");
        JButton lineBtn = new JButton("line");

        btnBox.add(setBtn);
        btnBox.add(colourScroll);
        btnBox.add(freeBtn);
        btnBox.add(rectBtn);
        btnBox.add(circBtn);
        btnBox.add(lineBtn);
        btnBox.setSize(300, 100);
        btnBox.add(Box.createHorizontalGlue());

        add(btnBox, BorderLayout.NORTH);

        setBtn.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {

                data= colours.getSelectedIndex();

                col.setCol(data);//This is where the error is being flagged up              
            }
        });
    }
}

和颜色类

package PaintV2;

import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import javax.swing.*;

public class Colour{

    public Color colVal;

     HashMap<Integer, Color> map = new HashMap<Integer, Color>();

    public Colour() {

        map.put(0, Color.RED);
        map.put(1, Color.BLUE);
        map.put(2, Color.YELLOW);
        map.put(3, Color.GREEN);

    }

    public Color setCol(Integer data) {
        return map.get(data);
    }
}

提前抱歉大量代码和代码混乱。此外,如果你需要我编辑任何位以使其更容易理解,发布或其他东西,我会在我能够编辑它时。

1 个答案:

答案 0 :(得分:2)

似乎是您刚刚声明的col而未初始化。试试这个......

public void actionPerformed(ActionEvent e) {

    data= colours.getSelectedIndex();
    if(col == null)
        col = new Colour();
    col.setCol(data);//This is where the error is being flagged up              

}