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

Thread: Problem with array initialization

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with array initialization

    I have been working in creating a B+ tree. I have certain problem in code. Please tell me what are the errors and how can I overcome it? I am giving the snippet where the error occurs.

     
    class record
    		{
    			public int value;
    		}
     
     
    		class node
    		{
    			Object[] pointers ;
    			int[] keys;
    			node parent;
    			boolean is_leaf;
    			int num_keys;
    			node next;
    			record r;
    		}
     
                 node start_new_tree(int key, record pointer)
    	   {
     
    		System.out.println(key+pointer.value);
     
    		node root = d.makeleaf();
    		root.keys[0]=key;                                        // The error is here.It gives null pointer exception
    		root.pointers[0] = (Object)pointer;
    		root.pointers[order - 1] = null;
    		root.parent = null;
    		root.num_keys++;
     
    		//System.out.println(root.keys[0]+" "+root.pointers[0]+" "+root.num_keys);
    		return root;
    	}

    When I use start_new_tree in my main,this gives nullpointer exception at the place mentioned.Can you tell me how to overcome this?

    Thank you


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem with array initialization

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/57772-problem-array-assignment.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with array initialization

    Does method called makeleaf() initialize arrays of node class?
    For example:

    public node makeleaf(){
        node newnode = new node();
     
        ...
        newnode.num_keys = 5;
     
        // this line is important otherwise keys is null and throws NullPointerException
        newnode.keys = new int[newnode.num_keys];
        ...
     
        return newnode;
    }
    Last edited by gaszabo; April 4th, 2012 at 06:01 AM.

Similar Threads

  1. Inner class initialization and declaration
    By longwu in forum Object Oriented Programming
    Replies: 2
    Last Post: August 31st, 2011, 07:44 AM
  2. Initialization of a class
    By Merik in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2010, 07:18 PM
  3. [SOLVED] JUnit initialization error
    By raphytaffy in forum Java IDEs
    Replies: 2
    Last Post: September 20th, 2010, 05:33 PM
  4. Initialization parameter of servlets
    By rakesh in forum Java Servlet
    Replies: 3
    Last Post: April 13th, 2010, 03:14 AM
  5. Re-Initialization of FrameView app components??
    By DarkJoy in forum Java Networking
    Replies: 2
    Last Post: November 13th, 2009, 01:19 AM