首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java gui NullPointerException

java gui NullPointerException
EN

Stack Overflow用户
提问于 2013-02-20 13:53:28
回答 1查看 172关注 0票数 0

当我点击的时候,我一直在*行之间的行上得到一个空指针异常。我已经看了大约一个小时了,似乎找不出原因。任何帮助都将不胜感激。谢谢。

代码语言:javascript
复制
    private Color currentColor;
    private char currentChar;
    private Letter letter;
    private JComboBox comboBox;
    private JPanel controlPanel;
    private Color[] colorArray = {Color.black,Color.blue,Color.cyan,Color.orange,Color.white,Color.yellow};
    private String[] colorStringArray = {"black","blue","cyan","orange","white","yellow"};
    private ArrayList<Letter> letterList;
    private JButton button1;
    private Canvas canvas;
    public WholePanel()
    {
        comboBox = new JComboBox(colorStringArray);
        // here we use black to draw a letter
        currentColor = colorArray[Color.black];

        // set the default character to 'A'
        currentChar = 'A';

        button1 = new JButton("Undo");


        controlPanel = new JPanel();
        controlPanel.add(button1);
        controlPanel.add(comboBox);

        canvas = new Canvas();
        JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, controlPanel, canvas);

        canvas.addMouseListener(new PointListener());
        add(sp);

         // make this panel listen to mouse
        addMouseListener(new PointListener());

        setBackground(Color.white);
        //This method needs to be called for this panel to listen to keys
        //When panel listens to other things, and go back to listen
        //to keys, this method needs to be called again.
        requestFocus();
    }
    private class PointListener implements MouseListener
    {
        int x;
        int y;
        // when a user clicks the panel (applet),
        // KeyListener is focused in it.
        public void mousePressed(MouseEvent event)
        {
            x = event.getX();
            y = event.getY();
            letter = new Letter(x,y,currentChar,colorArray[comboBox.getSelectedIndex()]);
            //******************************
            letterList.add(letter);
            //******************************
            for(Letter letter1 : letterList)
            {
                letter1.Draw(getGraphics());
            }
            x=y=0;
            requestFocus();
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-20 13:56:00

你没有初始化letterList

将以下行添加到WholePanel的构造函数中:

代码语言:javascript
复制
letterList = new ArrayList<Letter>();
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14973177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档