Results 1 to 5 of 5
  1. #1
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default NullPointerException error when running the program, I cant figure out why

    I have been trying to figure out why I am getting the error but cant seem to find the answer on my own.
    Java Code:
    import java.util.ArrayList;
    
    
    public class EditDistance 
    {
    
    	public static void main(String[] args) 
    	{
    		ArrayList<Character> a = new ArrayList<Character>();
    		char[] xArray = null;
    		char[]yArray = null;
    		char[] finalY = null;
    		String x = "AACAGTTACC";
    		String y = "TAAGGTCA--";
    		xArray = x.toCharArray();
    		yArray = y.toCharArray();
    		a = toArrayList(a , yArray);
    
    		System.out.println(editDistance(a, xArray, finalY, y));
    
    
    	}
    
    	public static int editDistance(ArrayList<Character> a, char[] xArray, char[] finalY, String y)
    	{
    		int temp;
    		for(int i = 0; i<xArray.length; i++)
    		{
    			temp = y.indexOf(xArray[i]);
    			if(temp != -1)
    			{
    				finalY[i] = a.get(temp);
    				a.remove(temp);
    				y = (y.substring(0, temp) + y.substring(temp));
    			}
    
    			else if(y.indexOf(xArray[i])==-1)
    			{
    				xArray[i]='z';
    			}			
    
    		}
    		for(int j =0; j<xArray.length; j++)
    		{
    			if(xArray[j]=='z')
    			{
    				finalY[j]=a.get(j);
    			}
    		}
    
    		int result = calcScore(finalY, xArray);
    		return result;
    	}
    
    	public static ArrayList<Character> toArrayList(ArrayList<Character> a, char[] yArray)
    	{
    		for(int i=0; i<yArray.length;i++)
    		{
    			a.add(yArray[i]);
    		}
    		return a;	
    	}
    
    	public static int calcScore(char[] finalY, char[] xArray )
    	{
    		int number = 0;
    		for(int i=0; i <xArray.length; i++ )
    		{
    			if( finalY[i]==(xArray[i]) )
    			{
    
    			}
    			else if(finalY[i] == '-')
    			{
    				number++;
    				number++;
    			}
    			else if (finalY[i] != xArray[i])
    			{
    				number++;
    			}
    		}
    
    		return number;
    	}
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: NullPointerException error when running the program, I cant figure out why

    Please post your full error message and indicate which line it occurs on.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: NullPointerException error when running the program, I cant figure out why

    Java Code:
    char[] finalY = null;
    Most likely culprit.

  4. #4
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default Re: NullPointerException error when running the program, I cant figure out why

    here is the full message :

    Exception in thread "main" java.lang.NullPointerException
    at EditDistance.editDistance(EditDistance.java:32)
    at EditDistance.main(EditDistance.java:19)


    and what is wrong with
    Java Code:
    char[] finalY = null;
    ?

  5. #5
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default Re: NullPointerException error when running the program, I cant figure out why

    finalY was the problem, i forgot that it could not be null when it was passed to editDistance if i wanted to change it

Similar Threads

  1. Replies: 3
    Last Post: 11-08-2010, 03:23 AM
  2. Replies: 7
    Last Post: 07-17-2010, 12:22 PM
  3. Replies: 6
    Last Post: 07-10-2010, 05:35 PM
  4. Error running java program using URL
    By gio123bg in forum New To Java
    Replies: 6
    Last Post: 06-30-2009, 06:26 PM
  5. error running java program
    By bdasilva in forum New To Java
    Replies: 1
    Last Post: 06-29-2009, 01:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •