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 16 of 16

Thread: passing object and storing

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default passing object and storing

    hi ,
    I three classes .I am passing an object from class node to class mainhandler's method mtd(int object). and want it to be stored in an array ,its not working

    public class mainhandler {
    public node[] n;
     public static int a;
     
    	public static void main(String[] args) {
     
     
    		node[] n=new node[0];
     
     
    	}
    	public int mtd(int a,packet pobj)
    { 
    		n[0].packet[0]=pobj; .......................................................>i can not assign pobj to array here
    		return a=a+ pobj.interval;
    }}
     
    public class node {
    int b;
    	node[] packet=new node[0];
     
    	public void add(){
     
     
    	 packet pobj=new packet(1,2,3);
    	 mainhandler obj=new mainhandler();
    	obj.mtd(2,pobj);
     
     
    }
     
    }
     
    public class packet {
     
    int type;
    int interval;
    int senderID;
     
    public packet(int a,int b,int c)
    {
    	type=a;
    	interval=b;
    	senderID=c;
    }
     
     
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: passing object and storing

    Objects are never passed, only their reference is.

    How many times do people have to be told "it's not working" provides zero information? Do you get errors? Then post the exact message. Does it have incorrect behviour/output? Then show what it does do and what it shoudl do instead.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: passing object and storing

    Looks like a scope problem. Make sure the variables you use are defined at the scope where you are using them.

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: passing object and storing

    A node array is being initialized, but no node objects are added. The code then attempts to add an object to a nested array in a non-existent node at position 0.

    The solution is to add nodes to the array before trying to access any nodes in the array.

    In fact it's worse than that - a local node array is initialized in the main method (still without any nodes being added), but the class member array is never even initialized before being used.
    Last edited by dlorde; June 30th, 2011 at 11:23 AM.

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: passing object and storing

    Even if initialize node objects ,i still find the same problem

    Error: Type mismatch: cannot convert from packet to node

    public static void main(String[] args) {
    	n=new node[1];
    	n[0]=new node();
    	n[1]=new node();

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: passing object and storing

    Please copy and paste here the full text of the error message. It has valuable information in it that Your edited version left out.

  7. #7
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: passing object and storing

    I suggest you post the new code in full, because that extract doesn't give me a type mismatch error. Also, if you get an error, post the full text of the error message. I had to guess what the error was because you didn't post it before. Now I'm not going to guess - I'll wait for you to post the full error information.

    I know why you're getting that error - the node class contains an array of nodes, and you're trying to add a packet to that array. A packet isn't a node...

    Incidentally, it is simple courtesy to use the Java Naming Conventions when posting code - class names should start with an uppercase letter, method and variable names should start with a lowercase letter. That way we can distinguish class names from variable names.
    Last edited by dlorde; June 30th, 2011 at 06:12 PM.

  8. #8
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: passing object and storing

    public class mainhandler {
    public static node[] n;
    public static int a;
    public static void main(String[] args) {
    	n=new node[1];
    	n[0]=new node();
     
    	}
    	public int mtd(int a,packet pobj)
    {		
    		n[0].packet[0]=pobj;    .............................................there is this error on this line which says: Type mismatch: cannot convert from packet to node
     
     
    }
    	}
     
    public class node {
     
     
    	public  node[] packet=new node[0];
     
    	public void add(){
     
     
    	 packet pobj=new packet(1,2,3);
    	 mainhandler obj=new mainhandler();
    	 obj.mtd(2,pobj);
     
     
    }
     
    }
     
    public class packet {
     
    int type;
    int interval;
    int senderID;
     
    public packet(int a,int b,int c)
    {
    	type=a;
    	interval=b;
    	senderID=c;
    }
     
     
    }

    The only error is Type mismatch: cannot convert from packet to node and it comes on that very line .
    Last edited by jack_nutt; June 30th, 2011 at 06:23 PM.

  9. #9
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: passing object and storing

    I already told you why you get that error - the array called 'packet' in the node class is an array of nodes, not an array of packets, so you can't add a packet to it. A packet is not a node.

    If you followed the Java Naming Conventions as I suggested, you'd probably be less confused and you might not make such a mistake.

  10. #10
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: passing object and storing

    what i am trying to do is to assign each node an array named packet and to store packet object in that array so that i can be called by node.packet .

  11. #11
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: passing object and storing

    waht can be an apropriate memory allocation in the node class to store objects of packet type that can be called from main class..

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: passing object and storing

    array named packet and to store packet object
    Your namings make for a problem. Class names should start with a Capital letter.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: passing object and storing

    waht can be an apropriate memory allocation in the node class to store objects of packet type that can be called from main class
    class Node {
      Packet aPacket;  // Store Packet object here
      ...
      public Packet getPacket() {  // This method can be called from Main class to get Packet object
        return aPacket;
      }
     ...

  14. #14
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: passing object and storing

    even if i do that my problem remains the same .. i am looking to store my packet object `pobj`in an array that is unique for each node (i.e n[0] and n[1] )
    i have defined an array in the Node class so that i can access it like n[0].pkt[0] = store object here.
    Please modify my code or tell me how to store it

     
     
     
    public class Control {
    public static Node[] n;
     
    public static void main(String[] args) {
     
     
     
    	n=new Node[0];
    	n[0]=new Node();
     
    	Node aa=new Node();
    	 aa.add();
     
    	} 
    	public int mtd(int a,Packet pobj)
    {		n[0].pkt[0]=pobj;
    		return a+pobj.type ;
     
     
    }
    	}
     
    public class Node {
     
     
    	  Packet[] pkt=new Packet[0];
     
    	public void add(){
     
     
    		Packet pobj=new Packet(1,2,3);
    	 System.out.println(pobj.type);
    	 System.out.println(pobj.interval);
    	 System.out.println(pobj.senderID);
    	 Control obj=new Control();
    	 int b=obj.mtd(2,pobj);
    		 System.out.println(b);
     
    }
     
    }
     
    public class Packet {
     
    int type;
    int interval;
    int senderID;
     
    public Packet(int a,int b,int c)
    {
    	type=a;
    	interval=b;
    	senderID=c;
    }
     
    public Packet(){
     
    }
    }




    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at mainhandler.main(mainhandler.java:10)

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: passing object and storing

    .ArrayIndexOutOfBoundsException: 0
    What code is at line 10?
    The array was defined with 0 elements! There is no 0th element.

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

    jack_nutt (June 30th, 2011)

  17. #16
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: passing object and storing

    yea i figured that out.thanks making array of packet type works fine

Similar Threads

  1. 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
  2. Passing reference via object
    By Stefan_Lam in forum Java Theory & Questions
    Replies: 1
    Last Post: January 7th, 2011, 10:57 AM
  3. Storing data
    By Joyce in forum Collections and Generics
    Replies: 1
    Last Post: September 20th, 2010, 09:16 AM
  4. 2D Object makes my object smaller, Why?
    By MassiveResponse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 15th, 2010, 02:33 PM
  5. Reading .txt and storing into an array
    By vluong in forum Collections and Generics
    Replies: 1
    Last Post: January 4th, 2010, 01:07 PM