Results 1 to 4 of 4
  1. #1
    alin_ms is offline Member
    Join Date
    Dec 2008
    Posts
    11
    Rep Power
    0

    Default problem calling function from class to class

    Hi guys and girls,

    I have this issue. I am having a class which is calling a method findPath upon user click on menu items. Problem is that the function is instantiantiating a class and calls another method in this class. First time is running correctly but second time it doesnt call this function. I can't figure out why. Here is the code

    public int[][] findPath(int x, int y, int startPosX, int startPosY){
    int pathLength = 0;

    PathFinder path = new PathFinder(mapLength,mapWidth,map,startPosX,startP osY,x,y);
    pathLength = path.pathLength;
    System.err.println("calling function findPath");
    if (path.findPath()){
    result = new int[pathLength][2];
    result = path.outputPath();
    }
    // for (int i=0;i<result.length;i++){
    // System.err.println("result: " + result[i][0] + " ");
    //
    // }
    return result;
    }

    I am instantiating thie PathFinder class in this method and the path.outputPath() should return me different arrays upon call. But it only calculates once and that's it. Second time is kinda skipping the whole thing.
    Can anyone kindly advise me what is going on.

    Thank you all.

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default intializing the same variable twice

    I'm not sure what program is supposed to do... it looks like it's recusive, but I'll stay out of that because I don't have enough info. One thing for sure is that you're intializng the same variable twice and then using it in a loop.
    Java Code:
    [B][COLOR="Red"]result[/COLOR][/B] = new int[pathLength][2];
    [B][COLOR="red"]result[/COLOR][/B] = path.outputPath();
    }
    // for (int i=0;i<[B][COLOR="red"]result[/COLOR][/B].length;i++)
    Change the name of the "result" variable to smoething else... for example:
    Java Code:
    [B][COLOR="Red"]arrayResult[/COLOR][/B] = new int[pathLength][2];
    [B][COLOR="red"]pathResult[/COLOR][/B] = path.outputPath();
    And then use the appropiate varaible name in the "for" loop.
    Also, I don't know what you are trying to do here:
    Java Code:
    if (path.findPath())
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    alin_ms is offline Member
    Join Date
    Dec 2008
    Posts
    11
    Rep Power
    0

    Default

    the if statement is coz the find path function in the other class returns true or false. i mean is either finding a path or not.i will try with ur suggestion see what happends.

    Thanks. I hope it does someting good :)

  4. #4
    alin_ms is offline Member
    Join Date
    Dec 2008
    Posts
    11
    Rep Power
    0

    Default

    well even if i commented the first result = new int[][] and then use just path.outputpath() it still holds the same result from the first call. same array. i will try again the same thing.. go through the code line by line but i am already sick of it. If u have any other suggestions please do not hezitate not even a second. And thank you for the time u put in answering my call.

Similar Threads

  1. Replies: 1
    Last Post: 11-17-2008, 09:32 AM
  2. Register a callback function in other class
    By barts2108 in forum New To Java
    Replies: 2
    Last Post: 11-10-2008, 04:24 PM
  3. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  4. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 AM
  5. Problem calling another class
    By adlb1300 in forum New To Java
    Replies: 3
    Last Post: 10-25-2007, 02:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •