Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

please help me!!!  RSS feed

 
joew weakers
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there folks. Wondering maybe someone might be able to assist me with this quick query. I have two classes - GeomInVectors and SDOGeometries -which essentially take geometry values from my database and place these geometries into simple vector objects. Having declared variables statically my program executed the way I wanted it to. However I was told that static variables were only to be used as a last resort and having changed these static variables to non-static my program ceased to work correctly. Below are a couple of commented code snippets which are used in executing my program. Perhaps somebody may spot something I cannot see!
from the GeomInVectors class:
public class GeomInVectors
{
protected Object ve = null;
public Vector ptv = new Vector(); //this was changed from static to non-static
....
public void pointVector(Object ve) //this is called in SDOGeometries class
{
ptv.add(ve); //adds object to ptv vector wherever called
}
public void printPoints(Vector point_vector) throws Exception
{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter ("C:\\db.txt", true)));
try
{
System.out.println(ptv.size()); // this returns 0 where it should be
populated with point objects from the database
for(int i = 0; i < ptv.size(); i++)
{
Point point = (Point) ptv.elementAt(i);
double a = point.getX();
double b = point.getY();
...
}
catch(Exception e){}
}
from the SDOGeometries class:
protected void printGType(Geometry geom)
{
GeomInVectors giv1 = new GeomInVectors();
try
{
if (geom == null)
System.out.println("geometry has a null value");
if (geom instanceof Point)
{
Point point = (Point) geom;
System.out.println(point);
giv1.pointVector(point); //this calls the pointVector method and adds the point object to it...this method executes right as far as I know
}
this code is called at the end of SDOGeometries:
GeomInVectors giv = new GeomInVectors();
giv.printPoints(giv.ptv);
giv.printLinestrings(giv.lsv);
My question is simply why is the vector ptv empty in the printPoints method whereas it should be filled with points returned from my database. The points are being retrieved as they are printed correctly to the screen in the printGType method. I cannot see what is wrong here. Thanks Joe
 
Tom Wolve
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
considering the last the three lines of your code the output is correct.
After creating the new instance

(including the creation of a new Vector ptv for this instance) you call the printPoints-method before adding any points. So the size of the Vector is in fact 0.
Hope that helped
Tom
 
joew weakers
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tom for replying. however just one resulting question. How do I pass the vector (ptv) which holds the point objects into the printPoints method in order to achieve my goal. Cheers Joe
 
Tom Wolve
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before calling

add some points to the vector by calling
 
Fintan Conway
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by joew weakers:

public class GeomInVectors {
protected Object ve = null;
public Vector ptv = new Vector(); //this was changed from static to non-static
....
public void pointVector(Object ve) //this is called in SDOGeometries class
{
ptv.add(ve); //adds object to ptv vector wherever called
}
public void printPoints(Vector point_vector) throws Exception
{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter ("C:\\db.txt", true)));
try
{
System.out.println(ptv.size()); // this returns 0 where it should be
populated with point objects from the database
...
...
protected void printGType(Geometry geom)
{
GeomInVectors giv1 = new GeomInVectors();
try
{
....
giv1.pointVector(point); //this calls the pointVector method and adds the point object to it...this method executes right as far as I know
}
this code is called at the end of SDOGeometries:
GeomInVectors giv = new GeomInVectors();
giv.printPoints(giv.ptv);
giv.printLinestrings(giv.lsv);
My question is simply why is the vector ptv empty in the printPoints method whereas it should be filled with points returned from my database.

Hi Joe,
The reason for your problems is that you are creating TWO GeomInVectors objects. In java, when you create a static variable, that one variable is available to all the objects that you create of that class. In your case 'giv1' and 'giv'. Therefore giv.ptv points to the same vector as giv1.ptv.
When you changed the variable ptv from static in your GeomInVectors class this means that each GeomInVectors object you create will have their own version of ptv. What you are doing in the code you have written is :
Create giv1
ptv in giv1 is created as a new Vector.
Add objects to the variable ptv in giv1 {giv1.pointVector(point)}
giv1.ptv now has all the points added to it
At the end of your SDOGeometries class
Create giv
ptv in giv is created as a new Vector.
giv.ptv does not have any points added to it and is *empty*.

What I would consider is moving the line :
GeomInVectors giv1 = new GeomInVectors();
out of the method, and make giv1 a class level variable. And change the 3 lines of code at the end of SDOGeometries to :
giv1.printPoints(giv1.ptv);
giv1.printLinestrings(giv1.lsv);
Hope this Helps,
Fintan
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!