0

I have a panel on whitch i create city object with picture and display it on map. When I click on the picture it display a new frame with name inside. But when i click on this image it shows that error.

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.rangeCheck(ArrayList.java:638)
    at java.util.ArrayList.get(ArrayList.java:414)
    at superworld.SuperPanel$1.mouseClicked(SuperPanel.java:60)
    at java.awt.Component.processMouseEvent(Component.java:6530)
    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:4542)
    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)

I wrote something like that:

package superworld;

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

public class SuperPanel extends JPanel implements ActionListener{

        JFrame frame;
        JFrame frame2;
        JLabel nazwa;
        Timer mainTimer;
    public static final int HEIGHT = 550;
    public static final int WIDTH = 1050;
        int i;
        int w=-100;
        int h=-50;
        ArrayList<SuperMiasto> miasta = new ArrayList<SuperMiasto>();      

   private JButton heroButton;
   private JButton cywilButton;


    public SuperPanel(JFrame frame) {
        mainTimer = new Timer(10,this);
               heroButton = new HeroButton(this);
               cywilButton = new CywilButton(this);
              frame2 = new JFrame();
               nazwa = new JLabel();




        setLayout(null);
        setPreferredSize(new Dimension(WIDTH, HEIGHT));
        setBackground(Color.GREEN);               

               for(i=0;i<1;i++)
                {
         //  i=0;
                    miasta.add( new SuperMiasto() );  

                   miasta.get(i).setNazwa("Radom");
                   miasta.get(i).nazwalabel = new JLabel();
                    nazwa.setText("Nazwa:");
                    nazwa.setBounds(0,0,50,20); 
                    miasta.get(i).nazwalabel.setText(miasta.get(i).getNazwa());
                    miasta.get(i).nazwalabel.setBounds(51,0,50,20);
                    miasta.get(i).x=w;
                    miasta.get(i).y=h;
                    miasta.get(i).imagelabel = new JLabel(miasta.get(i).image);
                    miasta.get(i).imagelabel.setBounds(miasta.get(i).x,miasta.get(i).y,miasta.get(i).image.getIconWidth(),miasta.get(i).image.getIconHeight());
                    add(miasta.get(i).imagelabel);
                    miasta.get(i).imagelabel.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {   
                        miasta.get(i).cityframe = new JFrame();
                       miasta.get(i).cityframe.setLayout(null);
                        miasta.get(i).cityframe.setSize(300,300); 
                       miasta.get(i).cityframe.add(nazwa);
                        miasta.get(i).cityframe.add(miasta.get(i).nazwalabel);                       
                        miasta.get(i).cityframe.setVisible(true);                                       
                    }
                    });

                    w=w+200;
                    if (w > WIDTH-200)
                    {
                        h=h+200;
                        w=-100;
                    }

                }

    }
        @Override
      public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
           add(heroButton); 
           add(cywilButton); 

    }   
      public void actionPerformed(ActionEvent e) {
            repaint();            
    }
}

And a city class:

package superworld;

import javax.swing.*;

public class SuperMiasto {
    private String nazwa;
    private int liczba_mieszkańców;
    private int liczba_mocy;
    ImageIcon image = new ImageIcon("C:/Users/Zuzanna Sawala/Moje dokumenty/NetBeansProjects/SuperWorld/miasto.png");
   int x,y;
    JLabel imagelabel;
    JFrame cityframe;
    JLabel nazwalabel;
   // int x,y;
   /* SuperMiasto(int x,int y){

    }*/
    /*Gettery i settery*/
    String getNazwa(){
        return nazwa;
    }
    void setNazwa(String nazwa){
        this.nazwa=nazwa;
    }
    int getLiczba_mieszkańcow(){
        return liczba_mieszkańców;
    }
    void setLiczba_mieszkańcow(int liczba_mieszkańców){
        this.liczba_mieszkańców=liczba_mieszkańców;
    }
    int getLiczba_Mocy(){
        return liczba_mocy;
    }
}

I'm newbie in java and i have no idea what that errors means. It's wierd becouse when i change for loop to i=0;it works perfectly correct. I have no idea what couse that error. Please help.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Vekka
  • 141
  • 1
  • 1
  • 12
  • What line is causing your errors? And you should never add components from within a paint or paintComponent method, ever. Those methods are for drawing and drawing only. – Hovercraft Full Of Eels Oct 12 '14 at 19:29
  • @JonSkeet: it's not a NullPointerException. Rather, it's an AIOOBE. – Hovercraft Full Of Eels Oct 12 '14 at 19:33
  • The error shows up when i click the image. I think the loop caus it somehow becouse without loop with only one object it works fine. And what should i use instead of paintComponent method? – Vekka Oct 12 '14 at 19:36
  • I don't know what you're trying to do with your code. But again, paintComponent should never be used for what you're using it for. – Hovercraft Full Of Eels Oct 12 '14 at 19:37
  • Are you a javascript programmer? Knowing that would eventually make things easier to explain... I'm very sceptic about the error happening at line 60 rather than 63 (it's now a good idea what you're doing there and should cause a warning anyway). Can you verify again, please (after you set the wrong title already...)? – Kalle Richter Oct 12 '14 at 19:56
  • No i'm student and i started learning Java like 2 weeks ago. I'm just making project. :/ – Vekka Oct 12 '14 at 20:00
  • Ah, so the title is wrong. Sorry, missed that... – Jon Skeet Oct 12 '14 at 20:09

0 Answers0