# Thread: Passing values between classes problem.

1. Member Join Date
Dec 2008
Posts
11
Rep Power
0

## 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];
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  Reply With Quote

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

3. Member Join Date
Dec 2008
Posts
11
Rep Power
0

## well i tried it. in outputPath i always have this
int[][] result = new int[pathLength];
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.  Reply With Quote

4. ## 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?  Reply With Quote

5. Member Join Date
Dec 2008
Posts
11
Rep Power
0

## 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.  Reply With Quote

6. Member Join Date
Dec 2008
Posts
11
Rep Power
0

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

if (path.findPath()){
result = new int[pathLength];
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?  Reply With Quote

7. ## 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  Reply With Quote

8. Member Join Date
Dec 2008
Posts
11
Rep Power
0

## 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];
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] = path[i].row;
result[i] = 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];
result = path.outputPath();
}
// for (int i=0;i<result.length;i++){
// System.err.println("result: " + result[i] + " ");
//
// }
return result;
}  Reply With Quote

9. Member Join Date
Dec 2008
Posts
11
Rep Power
0

## 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  Reply With Quote

#### Posting Permissions

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