Welcome to Dream.In.Code
Become a Java Expert!

Join 150,366 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,788 people online right now. Registration is fast and FREE... Join Now!




Lot's O Errors for some reason

 
Reply to this topicStart new topic

Lot's O Errors for some reason, Something wrong with buttons?

Mastergeek666
15 Feb, 2008 - 10:34 PM
Post #1

D.I.C Head
Group Icon

Joined: 10 Aug, 2007
Posts: 120


Dream Kudos: 75
My Contributions
Hey, I'm setting my button array to a string value and using a for loop to add then to panel as well as do various other things! My problem is when i answer yes then click the lets play button it gives me all these errors:
CODE

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at TicTacToe.Top(TicTacToe.java:52)
    at TicTacToe.actionPerformed(TicTacToe.java:94)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6038)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
    at java.awt.Component.processEvent(Component.java:5803)
    at java.awt.Container.processEvent(Container.java:2058)
    at java.awt.Component.dispatchEventImpl(Component.java:4410)
    at java.awt.Container.dispatchEventImpl(Container.java:2116)
    at java.awt.Component.dispatchEvent(Component.java:4240)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
    at java.awt.Container.dispatchEventImpl(Container.java:2102)
    at java.awt.Window.dispatchEventImpl(Window.java:2429)
    at java.awt.Component.dispatchEvent(Component.java:4240)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)


I'm not sure why its doing this. Thanks in advance!

Here's my code:
CODE

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TicTacToe extends JFrame implements ActionListener, ItemListener
{
    private JFrame Intro = new JFrame(), Stats = new JFrame(), Top = new JFrame(), Mid = new JFrame(), Bot = new JFrame();
    private JButton start, btns[];
    private JPanel pane, panel, panel1;
    public TicTacToe()
    {
        int x = JOptionPane.showConfirmDialog(null, "Would you like to play Tic-Tac-Toe?");
        if(x == JOptionPane.YES_OPTION)
        {
            Intro();
        }
        else
        {
            System.exit(0);
        }    
    }
    public JFrame Intro()
    {
        Intro.setTitle("IT'S TIC-TAC-TOE!");
        Intro.setBounds(475, 425, 250, 143);
        Intro.setResizable(false);
        Intro.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pane = new JPanel();
        Container con = Intro.getContentPane();
        JLabel pic = new JLabel(new ImageIcon("funny.gif"));
        start = new JButton("LET'S PLAY!");
        start.addActionListener(this);
        pane.add(pic);
        pane.add(start);
        con.add(pane);
        Intro.setVisible(true);
        return Intro;
    }
    public JFrame Stats()
    {
        Stats.setTitle("Stats");
        Stats.setResizable(false);
        Stats.setBounds(15, 300, 300, 600);
        Stats.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Stats.setVisible(true);
        return Stats;
    }
    public JFrame Top() throws NullPointerException
    {
        panel = new JPanel();
        for(int i = 0; i < 9; i++)
        {
            btns[i] = new JButton(String.valueOf(i));
            btns[i].setText("");
        }
        for(int i = 0; i < btns.length; i++)
        {
            btns[i].addActionListener(this);
             panel.add(btns[i]);
             btns[i].setSize(100, 100);
        }
        Top.setTitle("Top Level");
        Top.setResizable(false);
        Top.setBounds(320, 300, 300, 300);
        Top.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Top.setVisible(true);
        return Top;
    }
    public JFrame Mid()
    {
        panel1 = new JPanel();
        Mid.setTitle("Middle Level");
        Mid.setResizable(false);
        Mid.setBounds(625, 300, 300, 300);
        Mid.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        Mid.setVisible(true);      
        return Mid;
    }
    public JFrame Bot()
    {
        Bot.setTitle("Bottom Level");
        Bot.setResizable(false);
        Bot.setBounds(930, 300, 300, 300);
        Bot.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Bot.setVisible(true);
        return Bot;
    }
    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if(source == start)
        {
            Intro.setVisible(false);
            Stats();
            Top();
            Mid();
            Bot();
        }
        else
        {
            //
        }
    }
    public void itemStateChanged(ItemEvent e)
    {
        
    }
    public static void main(String[] args)
    {
        TicTacToe panel = new TicTacToe();
    }
}


This post has been edited by Mastergeek666: 15 Feb, 2008 - 10:37 PM
User is offlineProfile CardPM
+Quote Post

pertheusual
RE: Lot's O Errors For Some Reason
15 Feb, 2008 - 11:34 PM
Post #2

D.I.C Head
**

Joined: 26 Jan, 2008
Posts: 233



Thanked: 4 times
My Contributions
You're not actually initializing "btns[]" to anything, so it's throwing a NullPointerException.

CODE

public JFrame Top() throws NullPointerException
    {
        panel = new JPanel();

            btns = new JButton[9];       //NEED THIS

        for(int i = 0; i < 9; i++)
        {
            btns[i] = new JButton(String.valueOf(i));
            btns[i].setText("");
        }
        for(int i = 0; i < btns.length; i++)
        {
            btns[i].addActionListener(this);
             panel.add(btns[i]);
             btns[i].setSize(100, 100);
        }
        Top.setTitle("Top Level");
        Top.setResizable(false);
        Top.setBounds(320, 300, 300, 300);
        Top.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Top.setVisible(true);
        return Top;
    }


Also, why bother having 2 for loops in there? They can both be done at once, can't they?

Per
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:58PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month