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

Thread: How to fix error java.lang.NullPointerException in array of LinkedList?

  1. #1
    Junior Member
    Join Date
    Dec 2019
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to fix error java.lang.NullPointerException in array of LinkedList?

    How to fix error java.lang.NullPointerException in array of LinkedList?
    /* LinkedList MergeSort */
    package layout;
    import java.util.Scanner;
     
    /* Class Node */
    class Node {
    	protected int data;
    	protected Node next, prev;
    	/* Constructor */
    	public Node() {
    		data = 0;
    		next = null;
    	}
    	/* Constructor */
    	public Node( int d, Node n ) {
    		data = d;
    		next = n;
    	}
    	/* Function to set link to next node */
    	public void setLinkNext( Node n ) {
    		next = n;
    	}
    	/* Funtion to get link to next node */
    	public Node getLinkNext() {
    		return next;
    	}
    	/* Function to set data to node */
    	public void setData( int d ) {
    		data = d;
    	}
    	/* Function to get data from node */
    	public int getData() {
    		return data;
    	}
    }
     
    /* Class linkedList */
    class linkedList {
    	protected Node start, end;
    	public int size;
    	/* Constructor */
    	public linkedList() {
    		start = null;
    		end = null;
    		size = 0;
    	}
    	/* Function to check if list is empty */
    	public boolean isEmpty() {
    		return start == null;
    	}
    	/* Function to get size of list */
    	public int getSize() {
    		return size;
    	}
    	public Node getNodeStart() {
    		return start;
    	}
    	public Node getNodeEnd() {
    		return end;
    	}
    	/* Function to insert element */
    	public void insertHead( int val ) {
    		Node p = new Node( val, null );
    		if ( start == null ) {
    			start = p;
    			end = start;
    		}
    		else {
    			p.setLinkNext( start );
    			start = p;
    		}
    		size++;
    	}
    	void insertTail( int val ) {
    		Node p = new Node( val, null );;
    		if ( start == null ) {
    			start = p;
    			end = start;
    		}
    		else {
    			end.setLinkNext( p );
    			end = p;
    		}
    		p.setLinkNext( null );
    		size++;
    	}
    	void Output() {
    		Node p = start;
    		while ( p != null ) {
    			System.out.print( p.getData() + " <-> " );
    			p = p.getLinkNext();
    		}
    	}
    	/* Function to convert List to a String */
    	public String display() {
    		String ListString01 = "";
    		Node ptr = start;
    		ListString01 = ListString01 + ptr.getData() + ",  ";
    		if ( size == 0 ) {
    			ListString01 = "List is empty";
    			return ListString01;
    		}
    		ptr = start.getLinkNext();
    		while ( ptr.getLinkNext() != null ) {
    			ListString01 = ListString01 + ptr.getData() + ",  ";
    			ptr = ptr.getLinkNext();
    		}
    		return ListString01;
    	}
    	void initList( linkedList List ) {
    		List.start = List.end = null;
    	}
    	/* Function to MergeSort */
    	void appendList( linkedList List, linkedList List1 ) {
    		if ( List.start == null )
    			List = List1;
    		else {
    			List.end.setLinkNext( List1.start );
    			if ( List1.start != null )
    				List.end = List1.end;
    		}
    	}
    	int getDigit( int n, int t ) {
    		long temp = 1;
    		for ( int i = 0; i < n; i++ )
    			temp = temp * 10;
    		return (int) ( ( t / temp ) % 10 );
    	}
    	int countDigit( int n ) {
    		int count = 0;
    		while ( n > 0 ) {
    			n = n / 10;
    			count++;
    		}
    		return count;
    	}
    	int findMax( linkedList list ) {
    		Node p;
    		p = new Node();
    		p = list.start;
    		int max = p.data;
    		while ( p != null ) {
    			if ( max < p.data )
    				max = p.data;
    			p = p.getLinkNext();
    		}
    		return max;
    	}
    	int getMaxDigit( linkedList list ) {
    		int max = findMax( list );
    		return countDigit( max );
    	}
    	void sendToBox( linkedList list, int n, linkedList temp[] ) {
    		Node p;
    		p = new Node();
    		p = list.start;
    		while ( p != null ) {
    			temp[ getDigit( n, p.data ) ].insertTail( p.data );
    			p = p.getLinkNext();
    		}
    	}
    	void radixSort( linkedList list ) {
    		linkedList[] temp = null;
    		int i;
    		if ( list.start == list.end )
    			return;
    		for ( i = 0; i < 10; i++ )
    			temp[ i ] = new linkedList();
    		int num = getMaxDigit( list );
    		for ( i = 0; i < num; i++ ) {
    			sendToBox( list, i, temp );
    			for ( int j = 0; j < 10; j++ ) {
    				appendList( list, temp[ j ] );
    				initList( temp[ j ] );
    			}
    		}
    	}
    	@SuppressWarnings( "deprecation")
    	public static void main( String arg[] ) {
    		linkedList List01 = new linkedList();
    		int k = 10;
    		int a[] = { 12, 23, 34, 78, 91, 44, 52, 68, 79, 77 };
    		for ( int i = 0; i < k; i++ ) {
    			List01.insertHead( a[ i ] );
    		}
    		System.out.println( "oOo  m01.Output();  oOo" );
    		List01.Output();
    		System.out.println( " " );
    		System.out.println( "oOo  m01.display( );  oOo" );
    		String s01 = List01.display();
    		System.out.println( s01 );
    		List01.radixSort( List01 );
    		System.out.println( "oOo  m01.display( );  oOo" );
    		String s02 = List01.display();
    		System.out.println( s02 );
    	}
    }

  2. #2
    Junior Member
    Join Date
    Dec 2019
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to fix error java.lang.NullPointerException in array of LinkedList?

    Please help me or take a look.

Similar Threads

  1. Replies: 1
    Last Post: July 1st, 2013, 03:21 PM
  2. When I run my program, it says error: java.lang.NullPointerException
    By ProgrammerNoobE in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 28th, 2013, 04:08 PM
  3. When I run my program, it says error: java.lang.NullPointerException
    By ProgrammerNoobE in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 28th, 2013, 08:19 AM
  4. Replies: 1
    Last Post: November 27th, 2012, 10:19 AM
  5. [SOLVED] Java run time error: 'java.lang.NullPointerException"
    By ChandanSharma in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 20th, 2011, 11:53 AM