Operations by “for” function on ArrayList and Exception java.lang.ArrayIndexOutOfBoundsException











up vote
0
down vote

favorite












I have a problem with my code. I wrote a simple operations on ArrayLists and have an Exception.
Below is my code:



for (int i=0;i<Core.Config.amountInclu;i++) {
Random r = new Random();
x = r.nextInt(this.tabSizeX);
y = r.nextInt(this.tabSizeY);
Pkt nowy = new Pkt(x,y);
while(newPkt.contains(nowy) || onBoundary(x,y)==false){

x = r.nextInt(this.tabSizeX);
y = r.nextInt(this.tabSizeY);
}
for(int j = 0; j<Core.Config.sizeInclu;j++){
for(int k = 0; k<Core.Config.sizeInclu;j++){
if((x+j)<tabSizeX && (y+k)<tabSizeY){
this.P[x+j][y+k].set_black();
newPkt.add(nowy);
}
}

}

}


After run this code I have an exceptions:



Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -2147483648
at growth_1.Plansza.addInclusion(Plansza.java:661)
at growth_1.Display$18.mouseClicked(Display.java:399)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.Component.processMouseEvent(Component.java:6542)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6304)
at java.awt.Container.processEvent(Container.java:2239)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2297)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4544)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
at java.awt.Container.dispatchEventImpl(Container.java:2283)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
at java.awt.EventQueue$4.run(EventQueue.java:733)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
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)


The algorithm is very simple, we draw a point on JPanel (x,y) and checking if it was already used and if it is on boundary of bigger cell. If yes, we must draw another point.
And then we must set color black to point and neighbouring points.
I thought that I checked all conditions to protect points be out of bounds.



I will be very grateful, if you give me an advice.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have a problem with my code. I wrote a simple operations on ArrayLists and have an Exception.
    Below is my code:



    for (int i=0;i<Core.Config.amountInclu;i++) {
    Random r = new Random();
    x = r.nextInt(this.tabSizeX);
    y = r.nextInt(this.tabSizeY);
    Pkt nowy = new Pkt(x,y);
    while(newPkt.contains(nowy) || onBoundary(x,y)==false){

    x = r.nextInt(this.tabSizeX);
    y = r.nextInt(this.tabSizeY);
    }
    for(int j = 0; j<Core.Config.sizeInclu;j++){
    for(int k = 0; k<Core.Config.sizeInclu;j++){
    if((x+j)<tabSizeX && (y+k)<tabSizeY){
    this.P[x+j][y+k].set_black();
    newPkt.add(nowy);
    }
    }

    }

    }


    After run this code I have an exceptions:



    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -2147483648
    at growth_1.Plansza.addInclusion(Plansza.java:661)
    at growth_1.Display$18.mouseClicked(Display.java:399)
    at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
    at java.awt.Component.processMouseEvent(Component.java:6542)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6304)
    at java.awt.Container.processEvent(Container.java:2239)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2297)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4544)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
    at java.awt.Container.dispatchEventImpl(Container.java:2283)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
    at java.awt.EventQueue$4.run(EventQueue.java:733)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    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)


    The algorithm is very simple, we draw a point on JPanel (x,y) and checking if it was already used and if it is on boundary of bigger cell. If yes, we must draw another point.
    And then we must set color black to point and neighbouring points.
    I thought that I checked all conditions to protect points be out of bounds.



    I will be very grateful, if you give me an advice.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a problem with my code. I wrote a simple operations on ArrayLists and have an Exception.
      Below is my code:



      for (int i=0;i<Core.Config.amountInclu;i++) {
      Random r = new Random();
      x = r.nextInt(this.tabSizeX);
      y = r.nextInt(this.tabSizeY);
      Pkt nowy = new Pkt(x,y);
      while(newPkt.contains(nowy) || onBoundary(x,y)==false){

      x = r.nextInt(this.tabSizeX);
      y = r.nextInt(this.tabSizeY);
      }
      for(int j = 0; j<Core.Config.sizeInclu;j++){
      for(int k = 0; k<Core.Config.sizeInclu;j++){
      if((x+j)<tabSizeX && (y+k)<tabSizeY){
      this.P[x+j][y+k].set_black();
      newPkt.add(nowy);
      }
      }

      }

      }


      After run this code I have an exceptions:



      Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -2147483648
      at growth_1.Plansza.addInclusion(Plansza.java:661)
      at growth_1.Display$18.mouseClicked(Display.java:399)
      at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
      at java.awt.Component.processMouseEvent(Component.java:6542)
      at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
      at java.awt.Component.processEvent(Component.java:6304)
      at java.awt.Container.processEvent(Container.java:2239)
      at java.awt.Component.dispatchEventImpl(Component.java:4889)
      at java.awt.Container.dispatchEventImpl(Container.java:2297)
      at java.awt.Component.dispatchEvent(Component.java:4711)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
      at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4544)
      at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
      at java.awt.Container.dispatchEventImpl(Container.java:2283)
      at java.awt.Window.dispatchEventImpl(Window.java:2746)
      at java.awt.Component.dispatchEvent(Component.java:4711)
      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
      at java.awt.EventQueue.access$500(EventQueue.java:97)
      at java.awt.EventQueue$3.run(EventQueue.java:709)
      at java.awt.EventQueue$3.run(EventQueue.java:703)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
      at java.awt.EventQueue$4.run(EventQueue.java:733)
      at java.awt.EventQueue$4.run(EventQueue.java:731)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
      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)


      The algorithm is very simple, we draw a point on JPanel (x,y) and checking if it was already used and if it is on boundary of bigger cell. If yes, we must draw another point.
      And then we must set color black to point and neighbouring points.
      I thought that I checked all conditions to protect points be out of bounds.



      I will be very grateful, if you give me an advice.










      share|improve this question















      I have a problem with my code. I wrote a simple operations on ArrayLists and have an Exception.
      Below is my code:



      for (int i=0;i<Core.Config.amountInclu;i++) {
      Random r = new Random();
      x = r.nextInt(this.tabSizeX);
      y = r.nextInt(this.tabSizeY);
      Pkt nowy = new Pkt(x,y);
      while(newPkt.contains(nowy) || onBoundary(x,y)==false){

      x = r.nextInt(this.tabSizeX);
      y = r.nextInt(this.tabSizeY);
      }
      for(int j = 0; j<Core.Config.sizeInclu;j++){
      for(int k = 0; k<Core.Config.sizeInclu;j++){
      if((x+j)<tabSizeX && (y+k)<tabSizeY){
      this.P[x+j][y+k].set_black();
      newPkt.add(nowy);
      }
      }

      }

      }


      After run this code I have an exceptions:



      Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -2147483648
      at growth_1.Plansza.addInclusion(Plansza.java:661)
      at growth_1.Display$18.mouseClicked(Display.java:399)
      at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
      at java.awt.Component.processMouseEvent(Component.java:6542)
      at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
      at java.awt.Component.processEvent(Component.java:6304)
      at java.awt.Container.processEvent(Container.java:2239)
      at java.awt.Component.dispatchEventImpl(Component.java:4889)
      at java.awt.Container.dispatchEventImpl(Container.java:2297)
      at java.awt.Component.dispatchEvent(Component.java:4711)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
      at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4544)
      at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
      at java.awt.Container.dispatchEventImpl(Container.java:2283)
      at java.awt.Window.dispatchEventImpl(Window.java:2746)
      at java.awt.Component.dispatchEvent(Component.java:4711)
      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
      at java.awt.EventQueue.access$500(EventQueue.java:97)
      at java.awt.EventQueue$3.run(EventQueue.java:709)
      at java.awt.EventQueue$3.run(EventQueue.java:703)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
      at java.awt.EventQueue$4.run(EventQueue.java:733)
      at java.awt.EventQueue$4.run(EventQueue.java:731)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
      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)


      The algorithm is very simple, we draw a point on JPanel (x,y) and checking if it was already used and if it is on boundary of bigger cell. If yes, we must draw another point.
      And then we must set color black to point and neighbouring points.
      I thought that I checked all conditions to protect points be out of bounds.



      I will be very grateful, if you give me an advice.







      java arrays exception arraylist netbeans






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 20:39

























      asked Nov 12 at 20:33









      Karola

      112




      112
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          I can only suppose, that problem is because of in the statement



          for(int k = 0; k<Core.Config.sizeInclu;j++)...


          You have copy-paste error: j++ instead of k++






          share|improve this answer























          • Yes, you were right. Stupid mistake ;/
            – Karola
            Nov 18 at 17:37











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269676%2foperations-by-for-function-on-arraylist-and-exception-java-lang-arrayindexouto%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          I can only suppose, that problem is because of in the statement



          for(int k = 0; k<Core.Config.sizeInclu;j++)...


          You have copy-paste error: j++ instead of k++






          share|improve this answer























          • Yes, you were right. Stupid mistake ;/
            – Karola
            Nov 18 at 17:37















          up vote
          1
          down vote













          I can only suppose, that problem is because of in the statement



          for(int k = 0; k<Core.Config.sizeInclu;j++)...


          You have copy-paste error: j++ instead of k++






          share|improve this answer























          • Yes, you were right. Stupid mistake ;/
            – Karola
            Nov 18 at 17:37













          up vote
          1
          down vote










          up vote
          1
          down vote









          I can only suppose, that problem is because of in the statement



          for(int k = 0; k<Core.Config.sizeInclu;j++)...


          You have copy-paste error: j++ instead of k++






          share|improve this answer














          I can only suppose, that problem is because of in the statement



          for(int k = 0; k<Core.Config.sizeInclu;j++)...


          You have copy-paste error: j++ instead of k++







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 22:27

























          answered Nov 12 at 21:39









          Wladimir Diskowski

          586




          586












          • Yes, you were right. Stupid mistake ;/
            – Karola
            Nov 18 at 17:37


















          • Yes, you were right. Stupid mistake ;/
            – Karola
            Nov 18 at 17:37
















          Yes, you were right. Stupid mistake ;/
          – Karola
          Nov 18 at 17:37




          Yes, you were right. Stupid mistake ;/
          – Karola
          Nov 18 at 17:37


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269676%2foperations-by-for-function-on-arraylist-and-exception-java-lang-arrayindexouto%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)