How can I call a method of an object that is stored in an ArrayList?

Hello,
I am working on a simple game.
I have my own collection class that can contain some Brick objects.
That class looks like this:
class MyCollection<Brick>
      ArrayList<Brick> list = new ArrayList<Brick>();
      public void add(Brick obj)
            list.add(obj);
     public int get(int i)
            return list.get(i);
     public int length()
           return list.size();
}My Brick class is abstract and contains this method:
public abstract int getXpos();That method is overridden by the subclass Brick_type1 (which, of course, extends Brick) and returns an int.
In my main class I try to call the getXpos() method but I cannot call if for an unknown reason:
MyCollection mc = new MyCollection<Brick>();
Brick_type1 bt = new Brick_type1(10);
mc.add(bt);
for(int i = 0; i<mc.length(); i++)
            System.out.println(bc.get(i).getXpos());   // getXpos() does not exist, says the compilatorbc.get(i) should return a Brick that contains the getXpos() method, so why can´t I call it?

Roxxor wrote:
Sorry, it should be:
public Brick get(int i)
return list.get(i);
}...so it actually returns a Brick, but I still cannot call the getXpos() method for some reason.Is this also just a typo?
MyCollection mc = new MyCollection<Brick>();

Similar Messages

  • How can I call a Method in a Transformation Routine

    Hello Experts,
    How can I call a Method in a Transformation Routine ??
    THNXX

    Hi,
    http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/6090a621-c170-2910-c1ab-d9203321ee19?quicklink=index&overridelayout=true
    It will be help full .
    Regards,

  • How can we call the method of used controller?

    Hi All,
       i created two WDA Applications.( like YWDA1,YWDA2 ) . i am using the component WDA2 in WDA 1.and displaying the one view of WDA2 as popup window in WDA1 on action of one of the input element in the view of WDA1 by using the method l_window_manager->create_window_for_cmp_usage
    I have a button on the view of WDA2 which has appear in the popup window...how can i call the method which has binded to that button....and where should i code that...and i need to assign selected value in the popup window to input elemetn of view  WDA1
    Please help me to resolve this....
    Regards,
    Ravi

    You can not directly call view's event handler from other component.
    create a method in component controller of the second component and in the button click call the component controller method. ( also make the method as interface so that you can call it from other components )
    Now, you can call the interfacecontroller's method
    DATA: l_ref_INTERFACECONTROLLER TYPE REF TO ZIWCI__VSTX_REBATE_REQ_WD .
      l_ref_INTERFACECONTROLLER =   wd_This->wd_CpIfc_<comp usage name>( ).
      l_ref_INTERFACECONTROLLER->Save_Rr(
        STATUS = '01'                       " Zvstxrrstatus
    save_rr is the method of second component controller

  • How can I call a method for all instances of a certain class?

    Hi,
    I have created a snake java applet, which you can play here [http://users.telenet.be/javagames/|http://users.telenet.be/javagames/].
    But now I want to make this into a multi-player game (with two or more snakes etc..).
    In my main class you can find methods like these:
    snake.move();
    snake.drawSnake();Now if I just alter my main class and make a second instance of the class snake and change my methods to this,
    snake.move();
    snake2.move();
    snake.drawSnake();
    snake2.drawSnake();my game almost works perfectly, I'll have to make some minor changes though.
    But my question is, is there a better way of doing this. Instead of duplicating all the code, how can I make the methods immediately work for both snakes, instead of calling it twice?
    All suggestions are welcome!
    Thanks in advance.
    Lennart

    If there was something common to all snake instances then you could declare that variable static.
    Note: I don't want to confuse you here. A static variable can have a value apart from any instances at all. Instances can view static variables but a static method or inner class, for example, cannot view instance variables.
    More generally, I would think you would want to treat each snake as an automaton with its own behavior. There must be some class such as Game or Board or whatever that has, as suggested, either an array or collection of snakes. Iterate over each snake, ask it to perform some computation for the game and then draw itself.
    - Saish

  • Junit : How can I get the method name (say testMyAddress) that failed

    My code is below
              TestSuite masterSuite = new TestSuite(testClass);
              TestResult result = new TestResult();
              masterSuite.run(result);
                   Enumeration errors = result.errors();
                   while (errors.hasMoreElements())
                        TestFailure error = (TestFailure)errors.nextElement();
                        Test test = error.failedTest();
    /*will give me the class name but how can I get the method that threw the exception.
    I can get fName (that contains the method name) field to get the method,but being private I cannot hold of the field.
    Wondering if there is any easy way to get the method name that threw exception,without writing unneccessary code
                        Class c1 = test.getClass();
    thx
    m

    getting all methods is no good!
    My test class looks like this
    MyTestClass{
    testGoodData(){
    asserttrue(.....);
    testBadData(){
    asserttrue(.....);
    testNullData(){
    asserttrue(.....);
    someHelperMethod(){
    thx
    m

  • How Can I Call a method in iView  ,which is in another iView

    I Have 2 iViews i want to call a method in Iview which is i another iView
    Thanks and Regards
    Prasad.Y

    Hi Prasad,
    first, welcom at SDN.
    Next, let us clarify some wording:
    A <i>PAR</i> is a <i>Portal ARchive</i>. It is a zip in a certain format. It contains a <i>portal application</i>.
    A <i>portal application</i> consists of <i>components</i> and/or <i>services</i>.
    An <i>iView</i> is somehow an "instance" of a <i>component</i>. It is created within the portal. There are <i>no iViews in a PAR</i> (like: "There are no objects in a JAR (but classes).").
    With this in mind, you know that the question does not work, for iViews don't have "methods".
    If you ask how you can call a method of a class belonging to a different component (but within the same portal app), it is no problem at all, each class has access to each other class within the same portal app (in fact, one has to differentiate between public and private part, but we don't want to go so far). To call use a class belonging to (the public part of) a different app, you will have to set the SharingReference within portalapp.xml.
    If that's what you're looking for - great.
    If not, ask again, but far more detailed.
    Hope it helps
    Detlev
    PS: Please consider to reward points for helpful answers. Thanks in advance.

  • How Can I Call the method in WAR project's class?

    Message was edited by: JUNHA SHIN

    I have one correct Web Moudule Project.
    The EAR project(EmployeeCardJdoEar) added the WAR archive
    (EmployeeCardJdoWar) is run correctly.
    I want to add the war archive to my WD project.
    because, the war project contain business logic to be reused.
    so. i had enrolled in my WD project's properties ->
    Web Dynpro References -> Sharing References ->
    sap.com/EmployeeCardJdoEar.
    but, this method in WAR(EmployeeCardJdoWar) occurs Exception.
    Context ctx = new InitialContext();
    pmf = (PersistenceManagerFactory)
               ctx.lookup("java:comp/env/jdo/defaultPMF");
    I think lookup call is not run correctly.
    But, this lookup is run correctly in my EAR project.

  • How can i call a method from the a subdirectory using packages

    This is my directory structure:
    java
    --p1
    --One.java
    --p2
    --Two.java
    package java.p1;
    import p2.Two;
    public class One
         public static void main(String args[])
              Two obj = new Two();
              obj.p();
    package java.p2;
    public class Two
         public void p()
              System.out.println("p method");
    ERRORS !!
    C:\Documents and Settings\ashutosh\Desktop\java\p1\One.java:3: package p2 does not exist
    import p2.Two;
              ^
    C:\Documents and Settings\ashutosh\Desktop\java\p1\One.java:9: cannot find symbol
    symbol  : class Two
    location: class java.p1.One
              Two obj = new Two();
                    ^
    C:\Documents and Settings\ashutosh\Desktop\java\p1\One.java:9: cannot find symbol
    symbol  : class Two
    location: class java.p1.One
              Two obj = new Two();
                                  ^Help me out. Thanks.

    jinchuriki123 wrote:
    This is my directory structure:
    java
    --p1
    --One.java
    --p2
    --Two.java
    package java.p1;
    import p2.Two;
    public class One
         public static void main(String args[])
              Two obj = new Two();
              obj.p();
    package java.p2;
    public class Two
         public void p()
              System.out.println("p method");
    ERRORS !!
    C:\Documents and Settings\ashutosh\Desktop\java\p1\One.java:3: package p2 does not exist
    import p2.Two;
    ^
    C:\Documents and Settings\ashutosh\Desktop\java\p1\One.java:9: cannot find symbol
    symbol  : class Two
    location: class java.p1.One
              Two obj = new Two();
    ^
    C:\Documents and Settings\ashutosh\Desktop\java\p1\One.java:9: cannot find symbol
    symbol  : class Two
    location: class java.p1.One
              Two obj = new Two();
    ^Help me out. Thanks.You don't have a class p2.Two, You do have a class java.p2.Two. Package paths aren't relative.
    Also, it's recommended not to use package names containing "java" or "javax" (or is it illegal to do so?). Change your package & folder names.
    db

  • How can you get  the public variables from object that extends JLabel?

    I'm using the mouseClickedListener method e.getComponent(); to get the component that is clicked on the sceen. The component i clicked is type "object" and extends a jlabel, and i really need to acces a variable from it. Heres the code i'm using-
    MouseListener listenerDown=new java.awt.event.MouseListener() {
            public void mousePressed(MouseEvent e){
                paintAll();
                mX=e.getX();
                mY=e.getY();
            public void mouseClicked(MouseEvent e) {
                Component c = e.getComponent();
                drawResizeBox(c);
                selected=c;
            public void mouseReleased(MouseEvent e) {
            public void mouseEntered(MouseEvent e) {
            public void mouseExited(MouseEvent e) {
    package javapoint;
    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.RenderingHints;
    import java.awt.geom.Line2D;
    import java.awt.image.BufferedImage;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    public class object extends JLabel{
        public object(Rectangle rect,int id){
            idNum=id;
            Rect=rect;
            BufferedImage image = new BufferedImage((int)rect.getWidth()+1, (int)rect.getHeight()+1, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g = (Graphics2D) image.getGraphics();       
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.drawRect((int)rect.getX(), (int)rect.getY(), (int)rect.getWidth(), (int)rect.getHeight());
            Icon icon = new ImageIcon((Image)image);
            setIcon(icon);
            setBounds((int)rect.getX()-1, (int)rect.getY()-1, (int)rect.getWidth()+1, (int)rect.getHeight()+1);
            mainFrame.slideArr[mainFrame.sIndx].add(this);
            setVisible(true);
            r=true;       
        object(Oval oval,int id){
            idNum=id;       
            setBounds(oval.getX(), oval.getY(), oval.getWidth(), oval.getHeight());
            getGraphics().drawOval(oval.getX(), oval.getY(), oval.getWidth(), oval.getHeight());
            o=true;
            setVisible(true);
        object(Line2D line,int id){
            idNum=id;       
            setBounds((int)line.getX1(), (int)line.getY1(), (int)line.getX2(), (int)line.getY2()); //Not gunna work
            getGraphics().drawLine((int)line.getX1(), (int)line.getY1(), (int)line.getX2(), (int)line.getY2());
            l=true;
            setVisible(true);
        object(Icon icon,int id){
            idNum=id;
            setIcon(icon);
            setBounds(50,50,icon.getIconWidth(),icon.getIconHeight());
            i=true;
            setVisible(true);
        void drawObject(object obj){
            if(r){
                Rectangle rect=obj.Rect;
                setBounds((int)rect.getX()-1, (int)rect.getY()-1, (int)rect.getWidth()+1, (int)rect.getHeight()+1);          
                Rect=rect;
                BufferedImage image = new BufferedImage((int)rect.getWidth()+1, (int)rect.getHeight()+1, BufferedImage.TYPE_INT_ARGB);
                Graphics2D g = (Graphics2D) image.getGraphics();           
                g.setColor(Color.red);
                g.drawRect(0, 0, (int)rect.getWidth(), (int)rect.getHeight());           
                Icon icon = new ImageIcon((Image)image);
                setIcon(icon);
            }else if(l){
            }else if(o){
            }else if(i){
        public boolean r=false;
        public Rectangle Rect;
        public boolean o=false;
        public Oval Oval;
        public boolean l=false;
        public Line2D Line;
        public boolean i=false;
        public Icon Icon;
        public JLabel label;
        public int idNum;
    }Edited by: ghostbust555 on Feb 12, 2010 2:14 PM

    ghostbust555 wrote:
    Well see the problem is i have an array of 200 objects.What does that have to do with anything? And if it does, why isn't it in the code you posted?
    I dont understand what you mean in your "Edit..." part. could you show some code/ explain farther? sorry if its a dumb question I'm still a bit of a novice at java.Yeah.
    object yuck = (object) e.getComponent(); // That's a cast.
    boolean yucksR = yuck.r; // Get the "r" variable from the object.

  • How can I delete the backups of my imac, that are stored on my macbook air.

    I wanted to transfer a folder (of 5 gigas) from my iMac to my macbook air (both running Lion OS) using a thunderbolt cable, so I started my macbook air on target disk mode (by starting it up while holding the key T) and on my iMac I went to the time machine utility, switched it on and selected the disk from the macbook air, I selected the folder, copied it to my macbook air's dekstop, the whole process took me less than 2 minutes. I disconnected the cable, turned the macbook air on and the folder was in the desktop as I wanted, however, it was created automatically a backup of my iMac on my macbook air. So now I am kind of lacking space and I want to delete the backup, but unfortunately, I can't find the backup on my macbook.

    .....and the name of the folder?
    press Command-F in the Finder to bring up a Finder search, and then click the plus button to the right of the search window to add a new filter. Then select "other" in the first drop-down menu to assign  new filters, followed by adjusting them
    Once completed, you can then search for a keyword, such as a file name.

  • How can we read the contents of a textfile that is stored in the KM

    Hi All
    We are doing a Webdynpro application and we need to access the data of a textfile which is stored in the KM.
    The actual problem is :I have a textfile in Knowledege Management(KM) , /documents folder .Now i need to access the contents of that textfile.
    Please kindly help me in this regards as its urgent requirement for us.
    Thanks in Advance
    Thanks and Regards
    Nishita Salver

    Hi,
    Try the following
    RID rid = RID.getRID("/documents/testext.txt");
    try {
    com.sapportals.wcm.repository.IResource resource = ResourceFactory.getInstance().getResource(rid, resourceContext);
    if (resource != null) {
    String text = "";
    BufferedReader in = new BufferedReader(new InputStreamReader(resource.getContent().getInputStream()));
    while ((text = in.readLine()) != null) {
    wdContext.currentTextElement().setText(text);
    } catch (RuntimeException e2) {
    // TODO Auto-generated catch block
    /thread/90181 [original link is broken]
    Regards
    Ayyapparaj

  • JUNIT : how to call static methods through mock objects.

    Hi,
    I am writing unit test cases for an action class. The method which i want to test calls one static method of a helper class. Though I create mock of that helper class, but I am not able to call the static methods through the mock object as the methods are static. So the control of my test case goes to that static method and again that static method calls two or more different static methods. So by this I am testing the entire flow instead of testing the unit of code of the action class. So it can't be called as unit test ?
    Can any one suggest me that how can I call static methods through mock objects.

    The OP's problem is that the object under test calls a static method of a helper class, for which he wants to provide a mock class
    Hence, he must break the code under test to call the mock class instead of the regular helper class (because static methods are not polymorphic)
    that wouldn't have happened if this helper class had been coded to interfaces rather than static methods
    instead of :
    public class Helper() {
        public static void getSomeHelp();
    public class MockHelper() {
        public static void getSomeHelp();
    }do :
    public class ClassUnderTest {
        private Helper helper;
        public void methodUnderTest() {  // unchanged
            helper.getSomeHelp();
    public interface Helper {
        public void getSomeHelp();
    public class HelperImpl implements Helper {
        public void getSomeHelp() {
            // actual implementation
    public class MockHelper implements Helper {
        public void getSomeHelp() {
            // mock implementation
    }

  • Calling a method in BPM Object from jsf page

    Hi All,
    How do I call a method in BPM object from JSF page? Is it possible to invoke it in a manner similar to invoking a method from managed bean in JSF application?
    Please help.
    Thanks and Regards,
    Veronica

    You can use f:invoke (or f:invokea to with parameters)
    For ajax calls, you can use f:invokeUrl to get the URL to a particular method within your BPM object, although make sure the Server-Side Method property is set to Yes.
    http://download.oracle.com/docs/cd/E13154_01/bpm/docs65/taglib/index.html

  • How can we call "SP.UI.Notify.addNotification" methods in sharepoint designer page(.aspx) load without any onclick control?

    Hello Friends,
    How can we call "SP.UI.Notify.addNotification" methods in sharepoint designer page(.aspx) load without any onclick control?
    Scenario: When i was open my page i need to show below script,But here i used button control.
    <script type="text/javascript" src="/_layouts/14/MicrosoftAjax.js"></script><script src="/_layouts/14/sp.runtime.js" type="text/javascript"></script>
    <script src="/_layouts/14/sp.js" type="text/javascript"></script><script type="text/javascript">
    var strNotificationID;
    function showNofication()
     strNotificationID = SP.UI.Notify.addNotification("<font color='#AA0000'>In Progress..</font> <img src='/_Layouts/Images/kpiprogressbar.gif' align='absmiddle'> ", false);
    </script>
    <div class="ms-toolpanefooter"><input class="UserButton" onclick="Javascript:showNofication();" type="button" value="Show Nofitication"/> </div>
    Thanks
    Reddy

    Hi  Reddy,
    You can use the window.onload method for achieving your demand as below:
    <script type="text/javascript">
    window.onload=function(){
    </script>
    Hope this helps!
    Best Regards,
    Eric
    Eric Tao
    TechNet Community Support

  • How can I call the create method in BO from Application Service

    Hello!
    When I create a Business Object, CAF generates some methods automatically.
    How can I call the create method in the BO from Application Service logic?
    When i call the method then the entityManager and the sessionContext is NULL.
    How can I initialize this?
    Can anybody help me?
    Thanks, Thomas

    If you are using CE 7.11...
    1) In the Application Services, add the BO as dependant object in dependencies tab.
    2) In the implemention, add the following codes to call create method of the BO:
    this.get<BO>.createMethod();
    julius

Maybe you are looking for