Results 1 to 15 of 15
  1. #1
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Exception in thread "main" java.lang.NullPointerException

    here is the error i am getting

    Exception in thread "main" java.lang.NullPointerException
    at SortingSearching.bubbleSort(SortingSearching.java: 47)
    at SortingSearching.main(SortingSearching.java:20)
    Java Code:
    import java.io.*;
    import java.util.Scanner;
    import javax.swing.JFileChooser;
    import javax.swing.JOptionPane;
    
    
    public class SortingSearching //this take a file which is chosen by the user, and alphabetizes it.
    {
    		public static void main (String[] args) throws IOException
    		{
    			final int size=20;//sets a string up with a max size of 20
    			Comparable [] list=new String[size];
    			JFileChooser fileChooser = new JFileChooser();
    			if (fileChooser.showOpenDialog(null) ==
                   JFileChooser.APPROVE_OPTION)//lets the user select the file.
    			{          
             	File inFile = fileChooser.getSelectedFile();
            	 	Scanner input = new Scanner(inFile);
    				Lister(input,list);//performs actions to the file by calling functions
    				bubbleSort(list);
    				Output(list);
    			}
    			
    		}
    		public static void Lister (Scanner input,Comparable[]List)throws IOException// changes the file to an array
    		{
    		int n=0;
    			
    		while (List[n] != null)//goes through the file until the line is an empty string.
    			 {	
    			
    				List [n]= input.nextLine();
    				n++;
    			 }input.close();
    		}
    		public static void bubbleSort (Comparable[]list)//bubble sorts the array.
    		{
    					
    			int lastPos;
    			int index;
    			Comparable temp;
    			
    			for(lastPos= list.length-1;lastPos>= 0;lastPos--)
    			{
    				for(index=0;index<= lastPos-1;index++)
    				{
    					if(list[index].compareTo(list[index+1])>0)
    					{
    						temp=list[index];
    						list[index]=list[index+1];
    						list[index+1]=temp;
    		
    					}
    				}
    			}
    		}			
    		public static void Output(Comparable[]List)throws IOException
    		{
    			String outFile;
    			outFile = JOptionPane.showInputDialog("please enter the file name for output:");
    			PrintWriter outputFile=new PrintWriter(outFile);
    			
    			for(int i = 0; i< List.length;i++)
    				{
    				outputFile.println(List[i]);
    				}outputFile.close();
    		}
    }
    so it says my mistake is in the bubble sorter I took the code for the bubble sort word for word from my text book, so i'm asking y' all to help me figure out what i did wrong.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Exception in thread "main" java.lang.NullPointerException
    at SortingSearching.bubbleSort(SortingSearching.java: 47)
    The error is because the code on line 47 is trying to use a variable with a null value for referencing.
    Look at line 47, find the variable with the null value and then backtrack in the code to see why that variable does not have a valid value.

  3. #3
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.NullPointerException

    The variable in 47 is index, right didn't i assign it a value at the beginning of the for look, or is the array empty?

  4. #4
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.NullPointerException

    I just checked the array, it isn't the problem, so it is the index, but why doesn't it have a value if i assigned it 0

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Exception in thread "main" java.lang.NullPointerException

    int variables can NOT hold a null value.
    What is the statement on line 47?
    Add a println to print out the variables that are on line 47 so you can see which is null.

    Print out the contents of the list array using Arrays toString(list) to format it for printing.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Print out the contents of the list array using Arrays toString(list) to format it for printing.

  7. #7
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.NullPointerException

    So it is the array that is null, or at least the printing made that apparent. So the method for writing the values to the array is not working correctly. Am I correct or still way off, I am sorry I am really new to this.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  9. #9
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.NullPointerException

    I thought I was assigning values from the input file in the lister method, but that is obviously not working as intending, this is my first exercise with IO so I am new to the functions that work with it.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Quote Originally Posted by lenois View Post
    I thought I was assigning values from the input file in the lister method, but that is obviously not working as intending, this is my first exercise with IO so I am new to the functions that work with it.
    Go through the logic of the file step by step:

    Java Code:
            int n=0;
                 
            while (List[n] != null) {
    This loop starts with List[0], right? What is the initial value of List[0] when you've not put anything into it yet?

  11. #11
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Thank you, I see exactly what is happening it is already null, so the loop never executes. So change the conditions of the while loop, while(input.hasNext()) should work right?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Quote Originally Posted by lenois View Post
    Thank you, I see exactly what is happening it is already null, so the loop never executes. So change the conditions of the while loop, while(input.hasNext()) should work right?
    Better to ask the compiler and the JVM this question.

  13. #13
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    import javax.swing.JFileChooser;
    import javax.swing.JOptionPane;
    
    
    public class SortingSearching //this take a file which is chosen by the user, and alphabetizes it.
    {
    		public static void main (String[] args) throws IOException
    		{
    			int size=20;//sets a string up with a max size of 20
    			String[] list=new String[size];
    			JFileChooser fileChooser = new JFileChooser();
    			if (fileChooser.showOpenDialog(null) ==
                   JFileChooser.APPROVE_OPTION)//lets the user select the file.
    			{          
             	File inFile = fileChooser.getSelectedFile();
            	 	Scanner input = new Scanner(inFile);
    				int newSize=Lister(input,list);//performs actions to the file by calling functions
    				if(newSize<size){
    					size=newSize;}
    				bubbleSort(list);
    				Output(list);
    			}
    			
    		}
    		public static int Lister (Scanner input,String[]list)throws IOException// changes the file to an array
    		{
    		int n=0;
    			
    		while (input.hasNext())//goes through the file until the line is an empty string.
    			 {
    			 	String Like=input.nextLine();
    				list[n]=Like;
    				n++;
    				
    			 }input.close();return n;
    		}
    		public static void bubbleSort (Comparable[]list)//bubble sorts the array.
    		{
    					
    			int lastPos;
    			int i=0;
    			Comparable temp;
    			
    			for(lastPos= list.length-1;lastPos>= 0;lastPos--)
    			{
    				for(i=0;i<= lastPos-1;i++)
    				{
    					if(list[i].compareTo(list[i+1])>0)
    						{
    						temp=list[i];
    						list[i]=list[i+1];
    						list[i+1]=temp;
    		
    						}		
    					
    				}
    			}
    		}			
    		public static void Output(String[]List)throws IOException
    		{
    			String outFile;
    			outFile = JOptionPane.showInputDialog("please enter the file name for output:");
    			PrintWriter outputFile=new PrintWriter(outFile);
    			
    			for(int i = 0; i< List.length;i++)
    				{
    				outputFile.println(List[i]);
    				}outputFile.close();
    		}
    }
    I have an array now with full values, i even did a workaround so that there are also no null values at the end. but it is still giving me the same error, at the same line. the if statement is written exactly like my text book, none of the values are null that i can see.

    Exception in thread "main" java.lang.NullPointerException
    at java.lang.String.compareTo(String.java:1177)
    at SortingSearching.bubbleSort(SortingSearching.java: 52)
    at SortingSearching.main(SortingSearching.java:23)

    *edit the java.lang.String.compareTo is new
    Last edited by lenois; 02-22-2012 at 04:24 AM.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Use some System.out.println(...) statements just before the offending code to display the state of the key variables there. Let's see what the program is doing when the NPE strikes. Also, probably not related but when using a Scanner, your Scanner#hasNextXXX() should match the Scanner#nextXXX() method. In other words, if you call input.nextInt() inside the loop, you'd better first check input.hasNextInt(), similarly for next() / hasNext(), nextDouble()/hasNextDouble(), and for your situation nextLine()/hasNextLine().

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Look at your use of the newSize variable on line 21 and think about the ways that arrays work.
    When could newSize be larger than size?
    What if it is smaller?

Similar Threads

  1. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  2. Replies: 5
    Last Post: 10-11-2011, 12:16 AM
  3. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  4. Replies: 1
    Last Post: 07-16-2009, 10:35 AM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 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
  •