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)
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.
Re: Exception in thread "main" java.lang.NullPointerException
Quote:
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.
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?
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
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.
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.
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.
Re: Exception in thread "main" java.lang.NullPointerException
Where do you put objects into your array?
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.
Re: Exception in thread "main" java.lang.NullPointerException
Quote:
Originally Posted by
lenois
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:
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?
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?
Re: Exception in thread "main" java.lang.NullPointerException
Quote:
Originally Posted by
lenois
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. :o:
Re: Exception in thread "main" java.lang.NullPointerException
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
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().
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?