java.io.FileNotFoundException: votes.txt (The system cannot find the file specified)
Exception in thread "main" java.lang.NullPointerException
I was unable to fix this , please help me
Exception in thread
package MayorRace;
public class MayorRace {
public static void main(String[] args)
{
int[][] Matrix1 = load("votes.txt");
int[][] Matrix2 = load("votes1.txt");
AT THIS LINE
printAll(Matrix1);
printAll(Matrix2);
}
@SuppressWarnings("resource")
private static int[][] load(String filename)
{
ArrayList<ArrayList<Integer>> data = new ArrayList<ArrayList<Integer>>();
BufferedReader in = null;
// For now, we are assuming the text file is in the correct format
try
{
in = new BufferedReader(new FileReader(new File(filename)));
String dataLine;
while ((dataLine = in.readLine()) != null)
{
StringTokenizer tokenizer = new StringTokenizer(dataLine);
ArrayList<Integer> temp = new ArrayList<Integer>();
while (tokenizer.hasMoreTokens())
{
temp.add(Integer.parseInt(tokenizer.nextToken()));
}
data.add(temp);
}
} catch (FileNotFoundException ex )
{
System.out.println(ex);
return null;
} catch (IOException ex)
{
return null;
}
return convertmatrix(data);
}
// TODO Auto-generated method stub
private static int[][] convertmatrix(ArrayList<ArrayList<Integer>> integers)
{
int coloumns = integers.size();
if (coloumns == 0)
{
return null;
}
int rows = integers.get(0).size();
if (rows== 0)
{
return null;
}
int[][] output = new int[coloumns][rows];
for (int i = 0; i < coloumns; i++)
{
for (int j = 0; j < rows; j++)
{
output[i][j] = integers.get(i).get(j);
}
}
return output;
}
// TODO Auto-generated method stub GETSUM METHOD private static int[] getsum(int[][] matrix) { AT THIS LINE int[] sum = new int[matrix[0].length];
for (int i = 0; i < matrix[0].length; i++)
{
for (int[] matrixColumn : matrix)
{
sum[i] += matrixColumn[i];
}
}
return sum;
}
private static float[] getPer(int[] sum)
{
float total = 0;
for (int i = 0; i < sum.length; i++)
{
total += sum[i];
}
float[] per = new float[sum.length];
for (int i = 0; i < sum.length; i++)
{
per[i] = (sum[i] / total) * 100.0f;
}
return per;
}
GETTOPTWO METHOD
private static int[] getTopTwo(float[] totals)
{
float max = 0.0f;
int value1 = 0;
int value2 = 0;
for (int i = 0; i < totals.length; i++)
{
if (max < totals[i])
{
max = totals[i];
value2 = value1;
value1 = i;
}
}
int[] output =
{
value1, value2
};
return output;
}
PRINTMATRIX METHOD
private static void printMatrix(int[][] matrix)
{
if (matrix != null)
{
System.out.print("\t");
for (int i = -2; i < matrix.length; i++)
{
if (i == -1)
{
System.out.print("Precinct");
}
else if (i >= 0)
{
System.out.print(i + 1);
}
for (int j = 0; j < matrix[0].length; j++)
{
if (i == -2)
{
System.out.print("Candidate\t");
}
else if (i == -1)
{
System.out.print(" " + (char) (65 + j) + "\t\t");
}
else
{
System.out.print("\t " + matrix[i][j] + "\t");
}
}
System.out.println();
}
}
}
//METHOD
private static void printTotal(int[] totals)
TODO Auto-generated method stub
{
System.out.print("sum ");
for (int i = 0; i < totals.length; i++)
{
System.out.print("\t " + totals[i] + "\t");
}
System.out.println();
}
private static void printPercentages(float[] per)
{
System.out.print("\nPercentage");
for (int i = 0; i < per.length; i++)
{
System.out.print(String.format(" %.1f ", per[i]));
}
System.out.println();
}
private static void printWinner(float[] per)
{
int champ = -1;
for (int i = 0; i < per.length; i++)
{
if (per[i] > 50)
{
champ = i;
}
}
if (champ != -1)
{
System.out.println("Candidate "+(char)(65+champ)+ " is the winner");
}
else
{
int[] topTwo = getTopTwo(per);
System.out.println("\nThere is a run-off between candidate "
+ (char) (65 + topTwo[0]) + " and " + (char) (65 + topTwo[1]));
}
}
private static void printAll(int[][] matrix)
{
//EXCEPTION // TODO Auto-generated method stub
AT THIS LINE
int[] sum = getsum(matrix);
float[] per = getPer(sum);
printMatrix(matrix);
printTotal(sum);
printPercentages(per);
printWinner(per);
System.out.println();
}
private static void printTable(int[][] matrix) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
}
}
PLEASE HELP ME TO FIX THIS
"java.io.FileNotFoundException: votes.txt (The system cannot find the file specified)
You catch this error and set the matrix to null
catch (FileNotFoundException ex )
{
System.out.println(ex);
return null;
}
And then when you want to use your matrix in the printAll method you get the NPE on
matrix[0].length
Your program load method has FileNotFoundException, and because of it, method load returns null
. You need to check if load method has passed successfully, before printing matrix:
int[][] matrix1= load("votes.txt");
if(matrix1 != null){ // in this case an exception has occured during read
printAll(matrix1);
}else{
System.out.println("Could not load file into matrix");
}
Without it, you will got an NPE during
int[] sum = new int[matrix[0].length];
because matrix is null
UPDATE :
For me it is working fine, if I replace main body with:
public static void main(String[] args) {
int[][] matrix1 = load("c://Downloads//votes.txt");
if (matrix1 != null) {
printAll(matrix1);
}
}
votes.txt content:
1 1 1 1 1 1
Tips: Lists of lists is created by:
List<List<Integer>> data = new ArrayList<List<Integer>>();
or in jdk7++
List<List<Integer>> data = new ArrayList<>();
Name you variables, starting by lowerCase, so matrix1
, matrix2
, NOT: Matrix1,Matrix2.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.