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
Code Java:
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;
}
}
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.
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.
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.
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
Code :
public static void main(String[] args) {
n=new node[1];
n[0]=new node();
n[1]=new node();
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.
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.
Re: passing object and storing
Code :
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 .
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.
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 .
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..
Re: passing object and storing
Quote:
array named packet and to store packet object
Your namings make for a problem. Class names should start with a Capital letter.
Re: passing object and storing
Quote:
waht can be an apropriate memory allocation in the node class to store objects of packet type that can be called from main class
Code :
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;
}
...
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
Code :
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)
Re: passing object and storing
Quote:
.ArrayIndexOutOfBoundsException: 0
What code is at line 10?
The array was defined with 0 elements! There is no 0th element.
Re: passing object and storing
yea i figured that out.thanks making array of packet type works fine