Adding to an arraylist with an event listener [Java]Is Java “pass-by-reference” or “pass-by-value”?Create ArrayList from arrayHow do I read / convert an InputStream into a String in Java?When to use LinkedList over ArrayList in Java?How do I generate random integers within a specific range in Java?Initialization of an ArrayList in one lineConverting 'ArrayList<String> to 'String[]' in JavaConvert ArrayList<String> to String[] arrayHow do I convert a String to an int in Java?Creating a memory leak with Java

Is "The life is beautiful" incorrect or just very non-idiomatic?

French abbreviation for comparing two items ("vs")

Can RMSE and MAE have the same value?

What is the difference between Major and Minor Bug?

Can a Rogue PC teach an NPC to perform Sneak Attack?

How to prevent clipped screen edges on my TV, HDMI-connected?

Notepad++ cannot print

How can I unambiguously ask for a new user's "Display Name"?

How do we calculate energy of food?

Transposing from C to Cm?

Asymmetric table

Two questions about typesetting a Roman missal

How do you interpolate outside the range of data?

Where was Carl Sagan working on a plan to detonate a nuke on the Moon? Where was he applying when he leaked it?

How do I, an introvert, communicate to my friend and only colleague, an extrovert, that I want to spend my scheduled breaks without them?

Tex Quotes(UVa 272)

Is there any way white can win?

Papers on arXiv solving the same problem at the same time

Where can/should I, as a high schooler, publish a paper regarding the derivation of a formula?

Why in most German places is the church the tallest building?

Is MOSFET active device?

Are modern clipless shoes and pedals that much better than toe clips and straps?

Lost property on Portuguese trains

Could George I (of Great Britain) speak English?



Adding to an arraylist with an event listener [Java]


Is Java “pass-by-reference” or “pass-by-value”?Create ArrayList from arrayHow do I read / convert an InputStream into a String in Java?When to use LinkedList over ArrayList in Java?How do I generate random integers within a specific range in Java?Initialization of an ArrayList in one lineConverting 'ArrayList<String> to 'String[]' in JavaConvert ArrayList<String> to String[] arrayHow do I convert a String to an int in Java?Creating a memory leak with Java






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have a gameloop with a triangle that moves around the screen randomly. I want to add a button that when clicked will add another randomly moving triangle. I'm using event listeners and no matter where I place it I'm still getting an exception



Here's my code



import javax.swing.*;
import drawing.Canvas;
import tools.Utils;
import triangle.DynamicTriangle;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class TriangleProgram
private JFrame frame;
private Canvas canvas;
private JPanel lowerPanel;
private JButton addTriangleButton;
private ArrayList<DynamicTriangle> triangles;

public void gameLoop()
int deltaTime = 20;

triangles.add(new RandomTriangleA(canvas, 150, 150));

for (DynamicTriangle dynamicTriangle : triangles)
dynamicTriangle.drawTriangle();

while (true)

for (DynamicTriangle dynamicTriangle : triangles)
dynamicTriangle.undrawTriangle();


for (DynamicTriangle dynamicTriangle : triangles)
dynamicTriangle.update(100);
dynamicTriangle.wrapPosition();


for (DynamicTriangle dynamicTriangle : triangles)
dynamicTriangle.drawTriangle();


Utils.pause(deltaTime);
canvas.repaint();




public TriangleProgram()
frame = new JFrame();
frame.setTitle("Canvas");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

canvas = new Canvas();
frame.add(canvas, BorderLayout.CENTER);

lowerPanel = new JPanel();
lowerPanel.setLayout(new FlowLayout());
frame.add(lowerPanel, BorderLayout.SOUTH);

triangles = new ArrayList<DynamicTriangle>();

addTriangleButton = new JButton("Add Triangle");
lowerPanel.add(addTriangleButton);
frame.revalidate();

addTriangleButton.addActionListener(new ButtonListener());

gameLoop();



public static void main(String[] args)
System.out.println("Running TriangleProgram...");
new TriangleProgram();


class ButtonListener implements ActionListener

@Override
public void actionPerformed(ActionEvent ae)
System.out.println(triangles.size());
triangles.add(new RandomTriangleA(canvas, 150, 150));







I expect every time I press the button that a new triangle is added to the canvas, but instead I get the stack trace:



Running TriangleProgram...
1
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index -1 out-of-bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.remove(ArrayList.java:517)
at java.base/java.util.Collections$SynchronizedList.remove(Collections.java:2424)
at drawing.Canvas.removeMostRecentLine(Canvas.java:162)
at triangle.Triangle.undrawTriangle(Triangle.java:131)
at TriangleProgram.gameLoop(TriangleProgram.java:29)
at TriangleProgram.<init>(TriangleProgram.java:69)
at TriangleProgram.main(TriangleProgram.java:75)









share|improve this question





















  • 2





    When you ask about an exception, always post the complete exception stack trace. It tells what and where the problem is.

    – JB Nizet
    Mar 27 at 18:37











  • Hi, you seem to be referencing triangle near the top of your 'gameLoop' method, but the variable is never declared.

    – James McNee
    Mar 27 at 18:39











  • @JamesMacca It's initialized in the constructor.

    – Bakon Jarser
    Mar 27 at 18:42











  • @JamesMacca check constructor

    – JustAFellowCoder
    Mar 27 at 18:43











  • @BakonJarser 'trianges' is initialised in the constructor. 'triangle' is not.

    – James McNee
    Mar 27 at 18:54

















0















I have a gameloop with a triangle that moves around the screen randomly. I want to add a button that when clicked will add another randomly moving triangle. I'm using event listeners and no matter where I place it I'm still getting an exception



Here's my code



import javax.swing.*;
import drawing.Canvas;
import tools.Utils;
import triangle.DynamicTriangle;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class TriangleProgram
private JFrame frame;
private Canvas canvas;
private JPanel lowerPanel;
private JButton addTriangleButton;
private ArrayList<DynamicTriangle> triangles;

public void gameLoop()
int deltaTime = 20;

triangles.add(new RandomTriangleA(canvas, 150, 150));

for (DynamicTriangle dynamicTriangle : triangles)
dynamicTriangle.drawTriangle();

while (true)

for (DynamicTriangle dynamicTriangle : triangles)
dynamicTriangle.undrawTriangle();


for (DynamicTriangle dynamicTriangle : triangles)
dynamicTriangle.update(100);
dynamicTriangle.wrapPosition();


for (DynamicTriangle dynamicTriangle : triangles)
dynamicTriangle.drawTriangle();


Utils.pause(deltaTime);
canvas.repaint();




public TriangleProgram()
frame = new JFrame();
frame.setTitle("Canvas");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

canvas = new Canvas();
frame.add(canvas, BorderLayout.CENTER);

lowerPanel = new JPanel();
lowerPanel.setLayout(new FlowLayout());
frame.add(lowerPanel, BorderLayout.SOUTH);

triangles = new ArrayList<DynamicTriangle>();

addTriangleButton = new JButton("Add Triangle");
lowerPanel.add(addTriangleButton);
frame.revalidate();

addTriangleButton.addActionListener(new ButtonListener());

gameLoop();



public static void main(String[] args)
System.out.println("Running TriangleProgram...");
new TriangleProgram();


class ButtonListener implements ActionListener

@Override
public void actionPerformed(ActionEvent ae)
System.out.println(triangles.size());
triangles.add(new RandomTriangleA(canvas, 150, 150));







I expect every time I press the button that a new triangle is added to the canvas, but instead I get the stack trace:



Running TriangleProgram...
1
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index -1 out-of-bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.remove(ArrayList.java:517)
at java.base/java.util.Collections$SynchronizedList.remove(Collections.java:2424)
at drawing.Canvas.removeMostRecentLine(Canvas.java:162)
at triangle.Triangle.undrawTriangle(Triangle.java:131)
at TriangleProgram.gameLoop(TriangleProgram.java:29)
at TriangleProgram.<init>(TriangleProgram.java:69)
at TriangleProgram.main(TriangleProgram.java:75)









share|improve this question





















  • 2





    When you ask about an exception, always post the complete exception stack trace. It tells what and where the problem is.

    – JB Nizet
    Mar 27 at 18:37











  • Hi, you seem to be referencing triangle near the top of your 'gameLoop' method, but the variable is never declared.

    – James McNee
    Mar 27 at 18:39











  • @JamesMacca It's initialized in the constructor.

    – Bakon Jarser
    Mar 27 at 18:42











  • @JamesMacca check constructor

    – JustAFellowCoder
    Mar 27 at 18:43











  • @BakonJarser 'trianges' is initialised in the constructor. 'triangle' is not.

    – James McNee
    Mar 27 at 18:54













0












0








0








I have a gameloop with a triangle that moves around the screen randomly. I want to add a button that when clicked will add another randomly moving triangle. I'm using event listeners and no matter where I place it I'm still getting an exception



Here's my code



import javax.swing.*;
import drawing.Canvas;
import tools.Utils;
import triangle.DynamicTriangle;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class TriangleProgram
private JFrame frame;
private Canvas canvas;
private JPanel lowerPanel;
private JButton addTriangleButton;
private ArrayList<DynamicTriangle> triangles;

public void gameLoop()
int deltaTime = 20;

triangles.add(new RandomTriangleA(canvas, 150, 150));

for (DynamicTriangle dynamicTriangle : triangles)
dynamicTriangle.drawTriangle();

while (true)

for (DynamicTriangle dynamicTriangle : triangles)
dynamicTriangle.undrawTriangle();


for (DynamicTriangle dynamicTriangle : triangles)
dynamicTriangle.update(100);
dynamicTriangle.wrapPosition();


for (DynamicTriangle dynamicTriangle : triangles)
dynamicTriangle.drawTriangle();


Utils.pause(deltaTime);
canvas.repaint();




public TriangleProgram()
frame = new JFrame();
frame.setTitle("Canvas");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

canvas = new Canvas();
frame.add(canvas, BorderLayout.CENTER);

lowerPanel = new JPanel();
lowerPanel.setLayout(new FlowLayout());
frame.add(lowerPanel, BorderLayout.SOUTH);

triangles = new ArrayList<DynamicTriangle>();

addTriangleButton = new JButton("Add Triangle");
lowerPanel.add(addTriangleButton);
frame.revalidate();

addTriangleButton.addActionListener(new ButtonListener());

gameLoop();



public static void main(String[] args)
System.out.println("Running TriangleProgram...");
new TriangleProgram();


class ButtonListener implements ActionListener

@Override
public void actionPerformed(ActionEvent ae)
System.out.println(triangles.size());
triangles.add(new RandomTriangleA(canvas, 150, 150));







I expect every time I press the button that a new triangle is added to the canvas, but instead I get the stack trace:



Running TriangleProgram...
1
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index -1 out-of-bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.remove(ArrayList.java:517)
at java.base/java.util.Collections$SynchronizedList.remove(Collections.java:2424)
at drawing.Canvas.removeMostRecentLine(Canvas.java:162)
at triangle.Triangle.undrawTriangle(Triangle.java:131)
at TriangleProgram.gameLoop(TriangleProgram.java:29)
at TriangleProgram.<init>(TriangleProgram.java:69)
at TriangleProgram.main(TriangleProgram.java:75)









share|improve this question
















I have a gameloop with a triangle that moves around the screen randomly. I want to add a button that when clicked will add another randomly moving triangle. I'm using event listeners and no matter where I place it I'm still getting an exception



Here's my code



import javax.swing.*;
import drawing.Canvas;
import tools.Utils;
import triangle.DynamicTriangle;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class TriangleProgram
private JFrame frame;
private Canvas canvas;
private JPanel lowerPanel;
private JButton addTriangleButton;
private ArrayList<DynamicTriangle> triangles;

public void gameLoop()
int deltaTime = 20;

triangles.add(new RandomTriangleA(canvas, 150, 150));

for (DynamicTriangle dynamicTriangle : triangles)
dynamicTriangle.drawTriangle();

while (true)

for (DynamicTriangle dynamicTriangle : triangles)
dynamicTriangle.undrawTriangle();


for (DynamicTriangle dynamicTriangle : triangles)
dynamicTriangle.update(100);
dynamicTriangle.wrapPosition();


for (DynamicTriangle dynamicTriangle : triangles)
dynamicTriangle.drawTriangle();


Utils.pause(deltaTime);
canvas.repaint();




public TriangleProgram()
frame = new JFrame();
frame.setTitle("Canvas");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

canvas = new Canvas();
frame.add(canvas, BorderLayout.CENTER);

lowerPanel = new JPanel();
lowerPanel.setLayout(new FlowLayout());
frame.add(lowerPanel, BorderLayout.SOUTH);

triangles = new ArrayList<DynamicTriangle>();

addTriangleButton = new JButton("Add Triangle");
lowerPanel.add(addTriangleButton);
frame.revalidate();

addTriangleButton.addActionListener(new ButtonListener());

gameLoop();



public static void main(String[] args)
System.out.println("Running TriangleProgram...");
new TriangleProgram();


class ButtonListener implements ActionListener

@Override
public void actionPerformed(ActionEvent ae)
System.out.println(triangles.size());
triangles.add(new RandomTriangleA(canvas, 150, 150));







I expect every time I press the button that a new triangle is added to the canvas, but instead I get the stack trace:



Running TriangleProgram...
1
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index -1 out-of-bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.remove(ArrayList.java:517)
at java.base/java.util.Collections$SynchronizedList.remove(Collections.java:2424)
at drawing.Canvas.removeMostRecentLine(Canvas.java:162)
at triangle.Triangle.undrawTriangle(Triangle.java:131)
at TriangleProgram.gameLoop(TriangleProgram.java:29)
at TriangleProgram.<init>(TriangleProgram.java:69)
at TriangleProgram.main(TriangleProgram.java:75)






java arraylist event-listener






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 14:18







ExitEnter

















asked Mar 27 at 18:32









ExitEnterExitEnter

11 bronze badge




11 bronze badge










  • 2





    When you ask about an exception, always post the complete exception stack trace. It tells what and where the problem is.

    – JB Nizet
    Mar 27 at 18:37











  • Hi, you seem to be referencing triangle near the top of your 'gameLoop' method, but the variable is never declared.

    – James McNee
    Mar 27 at 18:39











  • @JamesMacca It's initialized in the constructor.

    – Bakon Jarser
    Mar 27 at 18:42











  • @JamesMacca check constructor

    – JustAFellowCoder
    Mar 27 at 18:43











  • @BakonJarser 'trianges' is initialised in the constructor. 'triangle' is not.

    – James McNee
    Mar 27 at 18:54












  • 2





    When you ask about an exception, always post the complete exception stack trace. It tells what and where the problem is.

    – JB Nizet
    Mar 27 at 18:37











  • Hi, you seem to be referencing triangle near the top of your 'gameLoop' method, but the variable is never declared.

    – James McNee
    Mar 27 at 18:39











  • @JamesMacca It's initialized in the constructor.

    – Bakon Jarser
    Mar 27 at 18:42











  • @JamesMacca check constructor

    – JustAFellowCoder
    Mar 27 at 18:43











  • @BakonJarser 'trianges' is initialised in the constructor. 'triangle' is not.

    – James McNee
    Mar 27 at 18:54







2




2





When you ask about an exception, always post the complete exception stack trace. It tells what and where the problem is.

– JB Nizet
Mar 27 at 18:37





When you ask about an exception, always post the complete exception stack trace. It tells what and where the problem is.

– JB Nizet
Mar 27 at 18:37













Hi, you seem to be referencing triangle near the top of your 'gameLoop' method, but the variable is never declared.

– James McNee
Mar 27 at 18:39





Hi, you seem to be referencing triangle near the top of your 'gameLoop' method, but the variable is never declared.

– James McNee
Mar 27 at 18:39













@JamesMacca It's initialized in the constructor.

– Bakon Jarser
Mar 27 at 18:42





@JamesMacca It's initialized in the constructor.

– Bakon Jarser
Mar 27 at 18:42













@JamesMacca check constructor

– JustAFellowCoder
Mar 27 at 18:43





@JamesMacca check constructor

– JustAFellowCoder
Mar 27 at 18:43













@BakonJarser 'trianges' is initialised in the constructor. 'triangle' is not.

– James McNee
Mar 27 at 18:54





@BakonJarser 'trianges' is initialised in the constructor. 'triangle' is not.

– James McNee
Mar 27 at 18:54












1 Answer
1






active

oldest

votes


















0















 triangle.add(new Triangle(canvas, 150, 150));


Well here you never even declare triangle so not sure how this code even runs to give you an exception. I'm assuming you meant triangles
Also you can probably(?) condense your loops.



Edit: Also when you declare the list triangles, why is it a list of DynamicTriangle but when you initialize and add to list it's Triangle.



Not sure if DynamicTriangle is subclass of Triangle or just typo but if that's the case it shouldn't compile.



If it's a typo then make your ArrayList<DynamicTriangle> triangles use Triangle instead.



If DyanmicTriangle is a subclass of Triangle then you want to declare and initialize triangles as ArrayList<Triangle>() and when you add you do .add(new DynamicTriangle(...






share|improve this answer



























  • Good catch, and those loops look fine...

    – JustAFellowCoder
    Mar 27 at 18:45











  • @JustAFellowCoder I don't mean they have an issue with them but he can probably just use one for each loop in the while loop instead of three. depends on what hes trying to achieve and what those methods he's calling actually does.

    – chris
    Mar 27 at 18:47











  • this was my mistake, I was renaming my code be more sensibly named before I posted and made a few errors. I've updated the code in the main post along with the full stack trace.

    – ExitEnter
    Mar 28 at 14:15











  • @ExitEnter the problem isnt in the class you posted. can you post undrawTriangle and removeMostRecentLine

    – chris
    Mar 28 at 17:49











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%2f55384285%2fadding-to-an-arraylist-with-an-event-listener-java%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









0















 triangle.add(new Triangle(canvas, 150, 150));


Well here you never even declare triangle so not sure how this code even runs to give you an exception. I'm assuming you meant triangles
Also you can probably(?) condense your loops.



Edit: Also when you declare the list triangles, why is it a list of DynamicTriangle but when you initialize and add to list it's Triangle.



Not sure if DynamicTriangle is subclass of Triangle or just typo but if that's the case it shouldn't compile.



If it's a typo then make your ArrayList<DynamicTriangle> triangles use Triangle instead.



If DyanmicTriangle is a subclass of Triangle then you want to declare and initialize triangles as ArrayList<Triangle>() and when you add you do .add(new DynamicTriangle(...






share|improve this answer



























  • Good catch, and those loops look fine...

    – JustAFellowCoder
    Mar 27 at 18:45











  • @JustAFellowCoder I don't mean they have an issue with them but he can probably just use one for each loop in the while loop instead of three. depends on what hes trying to achieve and what those methods he's calling actually does.

    – chris
    Mar 27 at 18:47











  • this was my mistake, I was renaming my code be more sensibly named before I posted and made a few errors. I've updated the code in the main post along with the full stack trace.

    – ExitEnter
    Mar 28 at 14:15











  • @ExitEnter the problem isnt in the class you posted. can you post undrawTriangle and removeMostRecentLine

    – chris
    Mar 28 at 17:49
















0















 triangle.add(new Triangle(canvas, 150, 150));


Well here you never even declare triangle so not sure how this code even runs to give you an exception. I'm assuming you meant triangles
Also you can probably(?) condense your loops.



Edit: Also when you declare the list triangles, why is it a list of DynamicTriangle but when you initialize and add to list it's Triangle.



Not sure if DynamicTriangle is subclass of Triangle or just typo but if that's the case it shouldn't compile.



If it's a typo then make your ArrayList<DynamicTriangle> triangles use Triangle instead.



If DyanmicTriangle is a subclass of Triangle then you want to declare and initialize triangles as ArrayList<Triangle>() and when you add you do .add(new DynamicTriangle(...






share|improve this answer



























  • Good catch, and those loops look fine...

    – JustAFellowCoder
    Mar 27 at 18:45











  • @JustAFellowCoder I don't mean they have an issue with them but he can probably just use one for each loop in the while loop instead of three. depends on what hes trying to achieve and what those methods he's calling actually does.

    – chris
    Mar 27 at 18:47











  • this was my mistake, I was renaming my code be more sensibly named before I posted and made a few errors. I've updated the code in the main post along with the full stack trace.

    – ExitEnter
    Mar 28 at 14:15











  • @ExitEnter the problem isnt in the class you posted. can you post undrawTriangle and removeMostRecentLine

    – chris
    Mar 28 at 17:49














0














0










0









 triangle.add(new Triangle(canvas, 150, 150));


Well here you never even declare triangle so not sure how this code even runs to give you an exception. I'm assuming you meant triangles
Also you can probably(?) condense your loops.



Edit: Also when you declare the list triangles, why is it a list of DynamicTriangle but when you initialize and add to list it's Triangle.



Not sure if DynamicTriangle is subclass of Triangle or just typo but if that's the case it shouldn't compile.



If it's a typo then make your ArrayList<DynamicTriangle> triangles use Triangle instead.



If DyanmicTriangle is a subclass of Triangle then you want to declare and initialize triangles as ArrayList<Triangle>() and when you add you do .add(new DynamicTriangle(...






share|improve this answer















 triangle.add(new Triangle(canvas, 150, 150));


Well here you never even declare triangle so not sure how this code even runs to give you an exception. I'm assuming you meant triangles
Also you can probably(?) condense your loops.



Edit: Also when you declare the list triangles, why is it a list of DynamicTriangle but when you initialize and add to list it's Triangle.



Not sure if DynamicTriangle is subclass of Triangle or just typo but if that's the case it shouldn't compile.



If it's a typo then make your ArrayList<DynamicTriangle> triangles use Triangle instead.



If DyanmicTriangle is a subclass of Triangle then you want to declare and initialize triangles as ArrayList<Triangle>() and when you add you do .add(new DynamicTriangle(...







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 27 at 18:54

























answered Mar 27 at 18:43









chrischris

514 bronze badges




514 bronze badges















  • Good catch, and those loops look fine...

    – JustAFellowCoder
    Mar 27 at 18:45











  • @JustAFellowCoder I don't mean they have an issue with them but he can probably just use one for each loop in the while loop instead of three. depends on what hes trying to achieve and what those methods he's calling actually does.

    – chris
    Mar 27 at 18:47











  • this was my mistake, I was renaming my code be more sensibly named before I posted and made a few errors. I've updated the code in the main post along with the full stack trace.

    – ExitEnter
    Mar 28 at 14:15











  • @ExitEnter the problem isnt in the class you posted. can you post undrawTriangle and removeMostRecentLine

    – chris
    Mar 28 at 17:49


















  • Good catch, and those loops look fine...

    – JustAFellowCoder
    Mar 27 at 18:45











  • @JustAFellowCoder I don't mean they have an issue with them but he can probably just use one for each loop in the while loop instead of three. depends on what hes trying to achieve and what those methods he's calling actually does.

    – chris
    Mar 27 at 18:47











  • this was my mistake, I was renaming my code be more sensibly named before I posted and made a few errors. I've updated the code in the main post along with the full stack trace.

    – ExitEnter
    Mar 28 at 14:15











  • @ExitEnter the problem isnt in the class you posted. can you post undrawTriangle and removeMostRecentLine

    – chris
    Mar 28 at 17:49

















Good catch, and those loops look fine...

– JustAFellowCoder
Mar 27 at 18:45





Good catch, and those loops look fine...

– JustAFellowCoder
Mar 27 at 18:45













@JustAFellowCoder I don't mean they have an issue with them but he can probably just use one for each loop in the while loop instead of three. depends on what hes trying to achieve and what those methods he's calling actually does.

– chris
Mar 27 at 18:47





@JustAFellowCoder I don't mean they have an issue with them but he can probably just use one for each loop in the while loop instead of three. depends on what hes trying to achieve and what those methods he's calling actually does.

– chris
Mar 27 at 18:47













this was my mistake, I was renaming my code be more sensibly named before I posted and made a few errors. I've updated the code in the main post along with the full stack trace.

– ExitEnter
Mar 28 at 14:15





this was my mistake, I was renaming my code be more sensibly named before I posted and made a few errors. I've updated the code in the main post along with the full stack trace.

– ExitEnter
Mar 28 at 14:15













@ExitEnter the problem isnt in the class you posted. can you post undrawTriangle and removeMostRecentLine

– chris
Mar 28 at 17:49






@ExitEnter the problem isnt in the class you posted. can you post undrawTriangle and removeMostRecentLine

– chris
Mar 28 at 17:49









Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55384285%2fadding-to-an-arraylist-with-an-event-listener-java%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

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript