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: NullPointer exception error

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default NullPointer exception error

    Hello!

    I'm wondering what I've done wrong with my code....It returns nullPonter exception and I don't know why, please help me.

     
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Scanner;
     
    public class Naloga2 {
     
     
    	 private static  int  m_cnt;
    	 private static int  m_in[];
     
     
    	 public static int Sort(int in [],int cnt)
    	 {
    		    int m_in[]=new int[cnt];
    	        m_cnt = cnt;
     
    	        for(int i=0;i<m_cnt;i++)
    	        {
    	                m_in[i]=in[i];
    	        }
     
    	        return mSort(0, m_cnt);
    	 }
     
    	  public static int mSort(int  ind, int  cnt)
    	    {
    	        int    tmp;
    	        int  left, right;
     
    	        if (cnt == 1) 
    	        {
    	            return 0;
    	        }
     
    	        left  = (cnt + 1) / 2;
    	        right = cnt / 2;
     
    	        if (cnt == 2) 
    	        {
    	            mDisplay(ind, 1, 1);
     
    	            if (m_in[ind] > m_in[ind+1]) 
    	            {
    	                tmp = m_in[ind];
    	                m_in[ind]= m_in[ind+1];
    	                m_in[ind+1]= tmp;
    	            }
     
    	            mDisplay(ind, 2, 0);
     
    	        } else {
    	            mDisplay(ind, left, right);
     
    	            mSort(ind, left);       // left
    	            mSort(ind+left, right); //right
     
    	            mOrder(ind, left, right);
     
    	            mDisplay(ind, left + right, 0);
    	        }
     
    	        return 0;
    	    }
     
    	  public static int mOrder(int  ind, int  left, int  right)
    	    {   
    	        int   i, a, b;
    	        int   tmp []=new int [left+right];
     
    	        a = b = 0;
     
    	        for (i = 0; i < (left+right); ++i) 
    	        {
    	            if ((m_in[ind+a] > m_in[ind+left+b] && b < right) || a >= left)
    	            {
    	                tmp[i] = m_in[ind+left+b];
    	                ++b;
    	            } 
    	            else if (a < left)
    	            { 
    	                tmp[i] = m_in[ind+a];
    	                ++a;
    	            }
     
    	        }
     
    	        for(int x=0;x<(left+right);x++)
    	        {
    	                m_in[ind+x]=tmp[x];
    	        }
     
     
     
     
    	        return 0;
    	    } 
     
    	  public static void mDisplay(int  ind, int  left, int  right)
    	    {
    	       int   i, j;
     
    	        for (i = 0; i < left; ++i) 
    	        {
    	            System.out.print(m_in[ind+i]+" ");
    	        }    
     
    	        if (right == 0) 
    	        {
    	            System.out.println();
    	            return;
    	        }
     
    	        System.out.print("| ");
     
    	        for (j = 0; j < right; ++j) 
    	        {
    	            System.out.print(m_in[ind+i+j]+" ");
    	        }    
     
     
    	        System.out.println();
    	    }
     
     
     
    		public static ArrayList<Integer> input(ArrayList<Integer> list,Scanner sc) {
     
    			while(true)
    			{
    				if(sc.hasNextInt())
     
    				{
    					list.add(sc.nextInt());
    				}
    				else 
    				{
    					break;
    				}
     
     
    			}
     
    			return (list);
    		}
     
     
    		public static int [] retTable(int [] table, ArrayList<Integer> list) {
     
     
    			for(int i=0;i<list.size();i++)
    			{
    				table[i]=(int)list.get(i);
    			}
     
     
    			return (table);
    		}	
     
     
     
     
     
    		public static void main(String [] args) {
     
    			String algorithm=args[0];
    			Scanner sc = new Scanner(System.in);
     
     
    			ArrayList<Integer> list = new ArrayList<Integer>();
    			ArrayList<Integer> listNew = new ArrayList<Integer>();
     
    			listNew=input(list,sc);
     
    			int [] table = new int[listNew.size()];
    			int [] tableNew = new int[listNew.size()];
     
    			tableNew=retTable(table,listNew);
     
     
     
     
    			if(algorithm.equals("QS"))
    			{
    				//izpisiSledQS(tabelaNova);	
    			}
    			else if(algorithm.equals("MS"))
    			{
    				Sort(tableNew,tableNew.length);
     
    			}
    			else
    			{
    				System.out.println("Pass sorting algorithm as argument.");
    			}
     
     
     
     
     
     
     
     
    		}
     
     
     
     
     
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: NullPointer exception error

    Of critical importance is your telling us which line throws this exception. When asking a question such as this, you'll *always* want to post the complete exception message and always want to indicate the line, possibly by obvious comment like // ******* NPE is thrown here! ******.

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: NullPointer exception error

    check out your first method Sort... did u mean to delcare another m_in array because your static int[] member is never being initialized and used correctly in the program

Similar Threads

  1. NullPointer Exception problem
    By anshu in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 26th, 2012, 06:38 AM
  2. java.lang.nullpointer exception in beginner code
    By Bobba in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 6th, 2012, 05:37 PM
  3. [SOLVED] Nullpointer exception when sending string
    By treshr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 26th, 2011, 04:36 AM
  4. Replies: 8
    Last Post: August 9th, 2011, 08:25 PM
  5. Java NullPointer Exception in Server chat program
    By Valtros in forum Exceptions
    Replies: 1
    Last Post: May 8th, 2009, 05:06 AM