Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: old object take variabile from the new issue

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default old object take variabile from the new issue

    hi, i have a problem with my code.
    when, on strausParser class, i create the new object Beam i give him an array of Node object called "nodiestremi".

    the problem is that when a Beam is created, the Beam created before take the Node of new Beam.

    why it appends?

    public class Main {
     
    	public static void main(String[] args) throws NumberFormatException, IOException{
     
    		SetFile.setupWindows();
     
    		Structure structure = new Structure();
    	}
    }
    public class SetFile {
     
     
    	}
    }




    i get an output like this:
    Import node complete
    0 0 0 1
    0 1 1 2
    0 1 1 0
    0 2 2 4
    Import beam complete
    and not like this:

    Import node complete
    0 0 0 1
    0 0 1 2
    0 0 1 0
    0 0 2 4
    Import beam complete
    Last edited by tuocuggino; May 1st, 2014 at 07:26 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: old object take variabile from the new issue

    Is the problem you are having because of all the static variables being used in the classes?
    If each instance of a class should have its own values, then the variables that hold those values should not be static.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: old object take variabile from the new issue

    Quote Originally Posted by Norm View Post
    Is the problem you are having because of all the static variables being used in the classes?
    If each instance of a class should have its own values, then the variables that hold those values should not be static.
    thanks for the reply.
    I've changed my program like before, removing all static modifier. But the problem remains the same
    Last edited by tuocuggino; May 1st, 2014 at 07:27 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: old object take variabile from the new issue

    The posted code does not compile without errors and can not be executed for testing.

    Please fix the compiler errors so the code can be executed for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: old object take variabile from the new issue

    Please post the code here, not as attachments. Make sure the code for testing is not too large.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Oct 2013
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: old object take variabile from the new issue

    Quote Originally Posted by Norm View Post
    Please post the code here, not as attachments. Make sure the code for testing is not too large.
    sorry here is the code.

    the interesting Class are: Beam, Structure, StrausParser and Node
    Last edited by tuocuggino; May 1st, 2014 at 07:27 AM.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: old object take variabile from the new issue

    How are you debugging the code to see what it is doing and why its output is not what you want?

    Where in the code is the output generated?
    Where does that coded get the data that is printed?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Oct 2013
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: old object take variabile from the new issue

    Quote Originally Posted by Norm View Post
    How are you debugging the code to see what it is doing and why its output is not what you want?

    Where in the code is the output generated?
    Where does that coded get the data that is printed?
    I use eclipse, with his debugger.

    the output is generated on Class StrausParser, method: importBeams (line 95)

    the code that generate the output take the data from the method where it is, and from the arraylists called element and node from structure class

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: old object take variabile from the new issue

    I use eclipse, with his debugger.
    I can't help you to use the IDE's debugger.
    I use println statements for my debugging. I would print out the values of the variables that the code uses to create the report that is wrong. That print out would show where the code is going wrong.
    Some choices would be:
    the data passed to the method that creates the report is wrong
    the method makes mistakes when it creates the output from the data
    or both of the above.

    Is the data that is passed to the method that creates the report correct?


    I do NOT see where the output you posted comes from in the code. The importBeams() method has ONE println() with text and numbers, not just numbers:
    			System.out.println("index: " + indiceelemento + " ndElem: " + nodoelemento + " extr1 " + nodiestremi[0].getIndex() + " " + nodiestremi[1].getIndex());
    Where is the output from that println?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Oct 2013
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: old object take variabile from the new issue

    Quote Originally Posted by Norm View Post
    I can't help you to use the IDE's debugger.
    I use println statements for my debugging. I would print out the values of the variables that the code uses to create the report that is wrong. That print out would show where the code is going wrong.
    Some choices would be:
    the data passed to the method that creates the report is wrong
    the method makes mistakes when it creates the output from the data
    or both of the above.

    Is the data that is passed to the method that creates the report correct?


    I do NOT see where the output you posted comes from in the code. The importBeams() method has ONE println() with text and numbers, not just numbers:
    			System.out.println("index: " + indiceelemento + " ndElem: " + nodoelemento + " extr1 " + nodiestremi[0].getIndex() + " " + nodiestremi[1].getIndex());
    Where is the output from that println?
    the output is this:
    index: 0 ndElem: 0 extr1 0 1
    index: 0 ndElem: 1 extr1 1 2
    index: 0 ndElem: 1 extr1 1 0
    index: 0 ndElem: 2 extr1 2 4
    i have added some text in the meanwhile for avoid to do confusion , but the numbers (that matters) are the same

    the output that i search is that:
    index: 0 ndElem: 0 extr1 0 1
    index: 0 ndElem: 0 extr1 1 2
    index: 0 ndElem: 0 extr1 1 0
    index: 0 ndElem: 0 extr1 2 4
    that is the index of the node passed to the first Beam remain the same when i add other beam


    the data used by the method are correct.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: old object take variabile from the new issue

    Are the values of the ndElem what is wrong?
    Is everything else correct?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Oct 2013
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: old object take variabile from the new issue

    Quote Originally Posted by Norm View Post
    Are the values of the ndElem what is wrong?
    Is everything else correct?
    if i check the value of ndElem that is nodoelemento that is structure.element.get(a).getNodes()[0].getIndex(); with the debugger it is correct.

    the problem is that when a new beam is created, the node of all oldest beam are overwritten with the node of the new beam

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: old object take variabile from the new issue

    when a new beam is created, the node of all oldest beam are overwritten with the node of the new beam
    Can you print out the values that you are talking about to show what is happening?

    the node of all oldest beam are overwritten with the node of the new beam
    Are you talking about this array: Node[] nodiestremi?
    The code only defines one instance of that array and overwrites it each time around the loop. Move the definition inside the loop so a new array is created each time.
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    tuocuggino (April 30th, 2014)

  15. #14
    Junior Member
    Join Date
    Oct 2013
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: old object take variabile from the new issue

    Quote Originally Posted by Norm View Post
    Can you print out the values that you are talking about to show what is happening?


    Are you talking about this array: Node[] nodiestremi?
    The code only defines one instance of that array and overwrites it each time around the loop. Move the definition inside the loop so a new array is created each time.
    i rewrite the method importBeams with some system print:

    	public  void importBeams() throws IOException{  
    		Node[] nodiestremi = new Node[2];
    		int index = 0;		
    		int propNumber = 0 ;
    		int a = 0;
     
    		while ((string.contains("Beam") != true)){
    			string = scanner.next().trim();
    		} 
     
    		while ((string.contains("Beam") == true)){
    			String[] line = string.split("\\s+"); 
     
    			for (int i = 0 ; i < 2 ; i++){
    				nodiestremi[i] = structure.nodes.get(Integer.parseInt(line[i+4])-1); 
    			}
     
     
    			if (Integer.parseInt(line[3]) != propNumber){
    				propNumber = Integer.parseInt(line[3]);
    				structure.section.add(new Section());
    			}
     
    			index = Integer.parseInt(line[1])-1;		
     
    			structure.element.add( new Beam(index, structure.section.get(propNumber-1) , nodiestremi) );
     
    			//index node that i put on new beam 
    			System.out.println("index nodiestremi on beam with index: " + a + ": "+ nodiestremi[0].getIndex() +" " +nodiestremi[1].getIndex());
     
    			int nodoelemento = structure.element.get(0).getNodes()[0].getIndex();
    			int nodoelemento1 = structure.element.get(0).getNodes()[1].getIndex();
     
    			System.out.println(" index of node on beam 0: " + nodoelemento + " "+ nodoelemento1);
    			string = scanner.next().trim();
    			System.out.print("\n");
    			a++;
    		} 
    		System.out.println("Import beam complete");
    	}

    the output is this:
    index nodiestremi on beam with index: 0: 0 1
    index of node on beam 0: 0 1

    index nodiestremi on beam with index: 1: 1 2
    index of node on beam 0: 1 2

    index nodiestremi on beam with index: 2: 1 0
    index of node on beam 0: 1 0

    index nodiestremi on beam with index: 3: 2 4
    index of node on beam 0: 2 4
    as you can see, the index of node on beam 0, change every time i create a new Beam

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: old object take variabile from the new issue

    The posted code in post#15 does not fix the problem I talked about in post#14.

    Add some more debug code to print out the contents of the object returned by:
    structure.element.get(0)
    so you can see if the contents of that object is being changed or not.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #16
    Junior Member
    Join Date
    Oct 2013
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: old object take variabile from the new issue

    Quote Originally Posted by Norm View Post
    The posted code in post#15 does not fix the problem I talked about in post#14.

    Add some more debug code to print out the contents of the object returned by:
    structure.element.get(0)
    so you can see if the contents of that object is being changed or not.
    I move the definition inside the loop and the problem disappear...

    i'm sorry for taking you busy with a stupid problem like this

    thanks a lot

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: old object take variabile from the new issue

    Glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Error setting up Object. Issue with static vs non-static
    By DaedricSHeep in forum What's Wrong With My Code?
    Replies: 8
    Last Post: April 3rd, 2014, 12:11 PM
  2. Replies: 2
    Last Post: September 26th, 2013, 04:13 PM
  3. read & write object in object file
    By amirsarem in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 8th, 2013, 11:35 AM
  4. Java Issue / Cache issue
    By VisualPK in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2012, 08:43 PM
  5. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM