Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Multi d. help request

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Multi d. help request

    I am trying to make a Connect Four game with java swing, but I am getting an error with my attempt at making an multi D panel maker when I try to run it.
    Any and all help would be awesome
    import javax.swing.*;
    import java.awt.Color;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
    public class ConnectFour implements  ActionListener{
        JPanel buttonPanel;
        JButton OneButton, TwoButton, ThreeButton, FourButton, FiveButton, SixButton, SevenButton;
        int a=0, b=0;
     
        public JPanel createContentPane (){
     
            JPanel totalGUI = new JPanel();
            totalGUI.setLayout(null);
     
            buttonPanel = new JPanel();
            buttonPanel.setLayout(null);
            buttonPanel.setLocation(10, 10);
            buttonPanel.setSize(780, 40);
            totalGUI.add(buttonPanel);
     
            JPanel panelForPanels = new JPanel();
            panelForPanels.setLayout(null);
            panelForPanels.setLocation(10, 40);
            panelForPanels.setSize(780, 670);
            totalGUI.add(panelForPanels);
     
            //something right under this is wrong :(
            for( int x = 0; x < 6; x = x++){
               for( int y = 0; y < 5; y = y++){
                  JPanel blackPanel[][] = new JPanel[6][5];
                  blackPanel[x][y].setBackground(Color.black);
                  blackPanel[x][y].setLocation(b,a);
                  blackPanel[x][y].setSize(100,100);
                  panelForPanels.add(blackPanel[x][y]);
                  a=a+110;
               }
               a=0;
               b=b+110;
            }
     
            OneButton = new JButton("DropHere");
            OneButton.setLocation(0, 0);
            OneButton.setSize(100, 30);
            OneButton.addActionListener(this);
            buttonPanel.add(OneButton);
     
            TwoButton = new JButton("DropHere");
            TwoButton.setLocation(110, 0);
            TwoButton.setSize(100, 30);
            TwoButton.addActionListener(this);
            buttonPanel.add(TwoButton);
     
            ThreeButton = new JButton("DropHere");
            ThreeButton.setLocation(220, 0);
            ThreeButton.setSize(100, 30);
            ThreeButton.addActionListener(this);
            buttonPanel.add(ThreeButton);
     
            ThreeButton = new JButton("DropHere");
            ThreeButton.setLocation(330, 0);
            ThreeButton.setSize(100, 30);
            ThreeButton.addActionListener(this);
            buttonPanel.add(ThreeButton);
     
            FourButton = new JButton("DropHere");
            FourButton.setLocation(440, 0);
            FourButton.setSize(100, 30);
            FourButton.addActionListener(this);
            buttonPanel.add(FourButton);
     
            FiveButton = new JButton("DropHere");
            FiveButton.setLocation(550, 0);
            FiveButton.setSize(100, 30);
            FiveButton.addActionListener(this);
            buttonPanel.add(FiveButton);
     
            SixButton = new JButton("DropHere");
            SixButton.setLocation(660, 0);
            SixButton.setSize(100, 30);
            SixButton.addActionListener(this);
            buttonPanel.add(SixButton);
     
            SevenButton = new JButton("DropHere");
            SevenButton.setLocation(770, 0);
            SevenButton.setSize(100, 30);
            SevenButton.addActionListener(this);
            buttonPanel.add(SevenButton);
     
     
            totalGUI.setOpaque(true);
            return totalGUI;
        }
     
        public void actionPerformed(ActionEvent e) {
        }
     
        private static void createAndShowGUI() {
     
            JFrame.setDefaultLookAndFeelDecorated(true);
            JFrame frame = new JFrame("[=] Connect Four! [=]");
     
     
            ConnectFour demo = new ConnectFour();
            frame.setContentPane(demo.createContentPane());
     
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(1060, 800);
            frame.setVisible(true);
        }
     
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Multi d. help request

    I am getting an error
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multi d. help request

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
    at ConnectFour.createContentPane(ConnectFour.java:31)
    at ConnectFour.createAndShowGUI(ConnectFour.java:106)
    at ConnectFour.access$000(ConnectFour.java:6)
    at ConnectFour$1.run(ConnectFour.java:116)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 703)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)
    Exception in thread "AWT-EventQueue-0" java.lang.NegativeArraySizeException
    at ConnectFour.createContentPane(ConnectFour.java:30)
    at ConnectFour.createAndShowGUI(ConnectFour.java:106)
    at ConnectFour.access$000(ConnectFour.java:6)
    at ConnectFour$1.run(ConnectFour.java:116)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 703)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ConnectFour.createContentPane(ConnectFour.java:31)
    at ConnectFour.createAndShowGUI(ConnectFour.java:104)
    at ConnectFour.access$000(ConnectFour.java:6)
    at ConnectFour$1.run(ConnectFour.java:114)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 703)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ConnectFour.createContentPane(ConnectFour.java:31)
    at ConnectFour.createAndShowGUI(ConnectFour.java:104)
    at ConnectFour.access$000(ConnectFour.java:6)
    at ConnectFour$1.run(ConnectFour.java:114)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 703)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ConnectFour.createContentPane(ConnectFour.java:31)
    at ConnectFour.createAndShowGUI(ConnectFour.java:104)
    at ConnectFour.access$000(ConnectFour.java:6)
    at ConnectFour$1.run(ConnectFour.java:114)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 703)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ConnectFour.createContentPane(ConnectFour.java:31)
    at ConnectFour.createAndShowGUI(ConnectFour.java:104)
    at ConnectFour.access$000(ConnectFour.java:6)
    at ConnectFour$1.run(ConnectFour.java:114)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 703)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ConnectFour.createContentPane(ConnectFour.java:31)
    at ConnectFour.createAndShowGUI(ConnectFour.java:104)
    at ConnectFour.access$000(ConnectFour.java:6)
    at ConnectFour$1.run(ConnectFour.java:114)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 703)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ConnectFour.createContentPane(ConnectFour.java:31)
    at ConnectFour.createAndShowGUI(ConnectFour.java:104)
    at ConnectFour.access$000(ConnectFour.java:6)
    at ConnectFour$1.run(ConnectFour.java:114)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 703)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ConnectFour.createContentPane(ConnectFour.java:31)
    at ConnectFour.createAndShowGUI(ConnectFour.java:104)
    at ConnectFour.access$000(ConnectFour.java:6)
    at ConnectFour$1.run(ConnectFour.java:114)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 703)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ConnectFour.createContentPane(ConnectFour.java:31)
    at ConnectFour.createAndShowGUI(ConnectFour.java:104)
    at ConnectFour.access$000(ConnectFour.java:6)
    at ConnectFour$1.run(ConnectFour.java:114)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 703)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)

    --- Update ---

    I got no idea what any of that error stuff means

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Multi d. help request

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
    at ConnectFour.createContentPane(ConnectFour.java:31)
    at ConnectFour.createAndShowGUI(ConnectFour.java:106)
    The first line gives the name/class of the exception: ArrayIndexOutOfBoundsException: 0
    The next line gives the location of the error: line 31 in ConnectFour
    Following that the lines are the call stack trace that shows where one method called another:
    the createAndShowGUI method on line 106 called the createContentPanel() method.

    At line 31 the code tried to index into an empty array. The index of 0 says that there were not any elements in the array.

    EDIT: ____________________________
    I guess your IDE ignores exceptions and continues trying to execute a very broken program. Here's another exception near the end of the above trace:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ConnectFour.createContentPane(ConnectFour.java:31)
    at ConnectFour.createAndShowGUI(ConnectFour.java:104)
    There was a null value on line 31. This is the cause of the rest of the problems.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Multi d. help request

    The stack trace reads from the bottom - I suspect there are parts not shown due to the buffer size of your console output - and the meaningful stuff is at the top:

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
    at ConnectFour.createContentPane(ConnectFour.java:31)

    The top line means that an ArrayIndexOutOfBoundsException was thrown,

    and the second line says that it occurred in the ConnectFour class, createContentPane() method at line 31.

    Your line numbers probably differ from mine - a limitation of this forum's software - but these 4 lines are probably the source of your problem:
            for( int x = 0; x < 6; x = x++){
                for( int y = 0; y < 5; y = y++){
                    JPanel blackPanel[][] = new JPanel[6][5];
                    blackPanel[x][y].setBackground(Color.black);
    Those lines include 2 for() loops, the second nested in the first, that DECLARE a 2-dimensional array of JPanel objects called blackPanel[][], but the objects in those arrays are never initialized, as in:

    blackPanel[x][y] = new JPanel();

    so that when the statement

    blackPanel[x][y].setBackground(Color.black);

    is encountered, you should be seeing a NullPointerException, or NPE, instead of the ArrayIndexOutOfBounds exception that you're reporting.

    Curious.

    So, DECLARING the variable blackPanel[][] should be done outside the for() loops, but INITIALIZING each object in blackPanel[][] should be done inside the for() loops.

  6. #6
    Junior Member
    Join Date
    May 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multi d. help request

    For some reason my version of java didn't recognized x++ as x+1 and put me in an infin. loop. That along with having my declaration in the loop and the lack of initialization. after fixing that stuff it seems to be working perfectly so far thanks guys <3

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Multi d. help request

    What is the need for the blackPanel array? Could a non array variable do what is needed?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    May 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multi d. help request

    I'm still new at this so I just did what I kinda know.
    The blackPanel array is for the default empty squares in my connect four game.
    I will change the color when a player chooses to drop a "ball"

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Multi d. help request

    The blackPanel array is local to the loops and does not exist when they end.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    May 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multi d. help request

    D: how is it local if the declaration is outside of it?

    JPanel blackPanel[][] = new JPanel[8][7];
            for( int x = 0; x < 7; x = x+1){
               for( int y = 0; y < 6; y = y+1){
                  blackPanel[x][y] = new JPanel();
                  blackPanel[x][y].setBackground(Color.black);
                  blackPanel[x][y].setLocation(b,a);
                  blackPanel[x][y].setSize(100,100);
                  panelForPanels.add(blackPanel[x][y]);
                  a=a+110;
               }
               a=0;
               b=b+110;
            }

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Multi d. help request

    The code has changed from what was posted before. Hard to talk about something that has changed.

    What happens to the array when the createContentPane() method exits?

    NOTE: The for loops should use the array's .length field to control the indexing into the arrays. The code should not use harcoded numbers like 7 and 6.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. unbuffered I/O request
    By kuri in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 25th, 2014, 08:22 AM
  2. Request Component
    By teymoorei in forum AWT / Java Swing
    Replies: 3
    Last Post: June 18th, 2013, 10:33 AM
  3. Request Information
    By anandluke in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 15th, 2013, 07:06 AM
  4. request
    By aegr in forum What's Wrong With My Code?
    Replies: 38
    Last Post: March 24th, 2013, 10:07 AM
  5. Http Request
    By nillson in forum Loops & Control Statements
    Replies: 3
    Last Post: March 20th, 2013, 06:42 PM