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

Thread: assigning problem

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

    Default assigning problem

    I have created an array type pkt object of packet class to store a packet object pobj,and once passed to add method it is changed to dataInterest type and stored in the dlist[].....

    In my code when i assign an object reference nodeobj[0].pkt[0]=pobj the error comes Exception in thread "main" java.lang.NullPointerException
    at mhand.add(mhand.java:27)
    at mhand.main(mhand.java:18)

    public class mHand {
     
    	public node[] nodeobj;
    	public static void main(String[] args) {
     
     
    		node[] nodeobj=new node[2];
    		nodeobj[0]=new node(1,2);
    		nodeobj[1]=new node(6,6);
     
    		packet pobj=new packet(3,0);
    		mHand m=new mHand();
    		m.add(pobj);	 // ............................>line 18
     
     
    	}
     
    	public void add(packet pobj)
    	{
    		nodeobj[0].pkt[0]=pobj;   //......................................line 27
     
    		dataInterest dobj=new dataInterest(nodeobj[0].pkt[0].type,nodeobj[0].pkt[0].senderid);
    		System.out.println(dobj.type+"  "+dobj.id);
    		nodeobj[0].dlist[0]=dobj;
    		System.out.println("datalist has first object with type"+nodeobj[0].dlist[0].type);
     
    	}
     
     
     
    }
     
    public class node {
     
    	int x;
    	int y;
    	node nlist[]=new node[5];
    	dataInterest[] dlist=new dataInterest[5];
     
    	packet[] pkt=new packet[1];
    	public node(){}
    	public node(int a,int b)
    	{
    		x=a;
    		y=b;
     
     
    	} 		
     
    }
     
     
    public class Gradient {
    	int id;
     
     
    	public Gradient(int a)
    	{
    		id=a;
     
    	}
    	public Gradient()
    	{
     
     
    	}
    }
     
     
     
    public class packet {
     
    	int senderid;
    	int type;
    	public packet(int a,int b)
    	{
    		type=a;
    		senderid=b;
     
    	}
     
     
     
    }
    i want to assign pkt[] for each nodeobj[0] and nodeobj[1].. so that each nodeobj has a unique pkt[] to store packet.
    Last edited by jack_nutt; July 4th, 2011 at 01:55 PM.


  2. #2
    Junior Member Mrc0d3r's Avatar
    Join Date
    Jun 2011
    Location
    TCP/IP Layer 3
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: assigning problem

    You get a java.lang.NullPointerException because you are trying to use public nodeobj array without creating their respective objects.
    //change the node[] to static so as to use it in main() method
    public static node[] nodeobj;
    //in the main() method change this line
    nodeobj=new node[2];

  3. The Following User Says Thank You to Mrc0d3r For This Useful Post:

    jack_nutt (July 4th, 2011)

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

    Default Re: assigning problem

    If you make the variable static, make sure you don't want to have more than one instance of the mHand class, each with their own versions of nodeobj. When a variable is static, there will only be one version shared between all instances of mHand objects.

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

    jack_nutt (July 4th, 2011)

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

    Default Re: assigning problem

    I'm getting a sense of deja-vue...

    In the code you posted, you have a class member node array called 'nodeobj' which never gets initialized and is hidden in the main(..) method by a local node array also called 'nodeobj' which does get initialized and has 2 nodes added to it, but which is not accessible by other class methods because it is local in scope.

    I'm pretty sure I went though all this in a previous thread, so I'm not inclined to spend any more time repeating myself.

Similar Threads

  1. [SOLVED] Assigning Values to Multi Array
    By dredjohn in forum Collections and Generics
    Replies: 3
    Last Post: April 18th, 2011, 07:43 PM
  2. Assigning an 'int' from a JTextField to a variable...
    By RiskyShenanigan in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 26th, 2011, 04:00 PM
  3. [SOLVED] logic error: cpu assigning incorrect values in for loop
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 25th, 2010, 08:13 PM
  4. Assigning variable to an Array
    By sridevi in forum Collections and Generics
    Replies: 3
    Last Post: August 10th, 2009, 10:58 PM