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

    Default Passing values between classes problem.

    Hi all i have a funny problem here.

    I have to clases let's call them 1 and 2 for ease. Class 2 is a path finding algorithm and when instantiated given some parameters it will then calculate a path and return it as an array [][]. Problem is first call it returns the path correctcly. Second call it returns the same path.Or doesn't return anything. I just have the same path saved in class 1.

    in class 1 i have
    private static int[][] result; wher ei store the path received
    and i call the path finding funciton in this function:

    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;

    if (path.findPath()){
    result = new int[pathLength][2];
    result = path.outputPath();
    }

    return result;
    }

    Where path.outputPath() returns the array.

    Can anyone give me a hint? I know is perhaps a stupid beginners problem but.. still.. help the begineer :)

    Thanks alll

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Try setting result to null in find path or outputPath. That way each time it calculates the path it starts fresh

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

    Default

    well i tried it. in outputPath i always have this
    int[][] result = new int[pathLength][2];
    and i return this one.
    If i put in findPath the result = null at the beginning and then i have the code which requests the result from outputPath the second time i ask the result i get a NullPointerException.

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    I don't see why you should have to declare result = new int then result = path.outputPath()

    Have you tried declaring result inside your outputPath() method and sending in pathLength?

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

    Default

    if i initialize the result in the findPath function with null, the second time i call the function i get NullPointerException. If i initialize all elements with 0 then the second time i call this function it takes these values. 00000 etc.i can't see the mistake.Is so frustrating. These one week simple mistakes.

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

    Default

    well i thought that since each path has a different length (pathLength) i have this:

    if (path.findPath()){
    result = new int[pathLength][2];
    result = path.outputPath();
    }

    path.findPath happends in class 2. This one calculates the path and returns true if it finds one.Then if i find one i initialize this result with its length and 2 coz there are x and y positions. And then i put the result in it with path.outputPath.

    Am i doing it wrong?

  7. #7
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    I know what you mean. I spent 2 days tearing apart my code to try to figure out why something wasn't working only to realize i had a > symbol instead of a < in my loop.

    Could you post your outputPath code? Maybe that will help figure things out

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

    Default

    This is the outputPath from class 2. The class is calculating correctcly the path:

    public int[][] outputPath()
    {System.out.println("Path finder says: pathLength is:" + pathLength);
    int[][] result = new int[pathLength][2];
    System.out.println("The wire path is");
    for (int i = 0; i < pathLength; i++){
    System.out.println("path is:" + path[i].row + " " + path[i].col);
    //map[path[i].row][path[i].col] = 8;}
    result[i][0] = path[i].row;
    result[i][1] = path[i].col;
    }
    return result;
    }

    And here is the function from class 1 that is suppose to save the new result upon each call.

    public int[][] findPath(int x, int y, int startPosX, int startPosY){
    int[][] result = null;
    int pathLength = 0;
    PathFinder path = new PathFinder(mapLength,mapWidth,map,startPosX,startP osY,x,y);
    pathLength = path.pathLength;

    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;
    }

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

    Default

    anyway weather i fix it or not now i want to thank u for the time and consideration. i didn't expect someone to answer so fast. Thankx pal

Similar Threads

  1. passing values from main page to pop up window
    By vicky in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 12-29-2008, 03:06 PM
  2. Need help passing data between classes
    By bri1547 in forum New To Java
    Replies: 3
    Last Post: 07-21-2008, 04:19 AM
  3. passing dynamically generated values
    By abhiN in forum Advanced Java
    Replies: 1
    Last Post: 01-20-2008, 03:21 AM
  4. Passing variable information between classes
    By zen_to_go in forum New To Java
    Replies: 1
    Last Post: 10-30-2007, 08:09 PM
  5. Passing the Values
    By Lagarto in forum JDBC
    Replies: 1
    Last Post: 07-16-2007, 03:03 AM

Posting Permissions

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