-8
テキストファイルから情報を取り込み、16進数から2進数および10進数のバイナリに変換するコードを作成しています。しかし、それは私にnullポインタ例外エラーを与えます。また、102行目と39行目にエラーが表示されます。16進数から16進数へのヌルポインタ例外エラー
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class MartinezRPgm2 {
public static void main(String[] args) {
String filename = "RAMerrors.txt";
String line = null;
int counter = 0;
// Try-catch block that will catch any errors while opening the file.
try {
FileReader fileReader = new FileReader(filename);
BufferedReader bufferReader = new BufferedReader(fileReader);
while ((line = bufferReader.readLine()) != null) {
System.out.println(line);
counter++; // Counter to keep track of the amount of lines in
// the given file.
}
bufferReader.close();
} catch (FileNotFoundException ex) {
System.out.println("Unable to open file '" + filename + "'");
} catch (IOException ex) {
System.out.println("Error reading file '" + filename + "'");
}
ArrayList<String> list1 = new ArrayList<String>();
list1.addAll(hexToBinary(line)); // Converts all the lines to binary and
// adds them to the list.
System.out.println("Binary: "); // Print statement to organize output.
// This for-loop simply prints all the lines contained the converted hex
// values.
for (int i = 0; i < counter; i++) {
System.out.println(list1.get(i));
}
long[] list2 = new long[counter]; // Array of type long to pass to
// binaryToDecimal.
// This for-loop will traverse through the list of binary numbers,
// convert them to decimal
// and save them in list2[].
for (int i = 0; i < counter; i++) {
list2[i] = binaryToDecimal(list1.get(i));
}
System.out.println("\nDecimal: "); // Print statement to organize
// output.
// This for-loop simply prints all the lines with the converted decimal
// values.
for (int i = 0; i < counter; i++) {
System.out.println(list2[i]);
}
long c = 0;
for (int i = 0; i < counter; i++) {
c = list2[i];
if (c <= 8589934584L) {
System.out.println("Error " + i + " is located in RAM Chip 0");
} else if (c <= 17179869184L) {
System.out.println("Error " + i + " is located in RAM Chip 1");
} else if (c <= 25769803768L) {
System.out.println("Error " + i + " is located in RAM Chip 2");
} else if (c <= 34359738368L) {
System.out.println("Error " + i + " is located in RAM Chip 3");
} else {
System.out.println("Error " + i + " is not in range");
}
if (c > 34359738368L) {
c = c/8L;
}
}
}
public static ArrayList<String> hexToBinary(String line) {
int stringLength = line.length();
char c;
ArrayList<String> result = new ArrayList<String>();
// For-loop that will traverse through every hex character until the end
// of the string.
for (int i = 0; i < stringLength; i++) {
c = line.charAt(i); // Saves the character onto the temporary
// variable, c.
// Switch-statement replaces each hex character with the appropriate
// binary value.
switch (c) {
case '0':
result.add("0000");
break;
case '1':
result.add("0001");
break;
case '2':
result.add("0010");
break;
case '3':
result.add("0011");
break;
case '4':
result.add("0100");
break;
case '5':
result.add("0101");
break;
case '6':
result.add("0110");
break;
case '7':
result.add("0111");
break;
case '8':
result.add("1000");
break;
case '9':
result.add("1001");
break;
case 'A':
result.add("1010");
break;
case 'B':
result.add("1011");
break;
case 'C':
result.add("1100");
break;
case 'D':
result.add("1101");
break;
case 'E':
result.add("1110");
break;
case 'F':
result.add("1111");
break;
}
}
return result; // Returns the converted string of type
// ArrayList<String>.
}
public static long binaryToDecimal(String value) {
StringBuilder newString = new StringBuilder(value);
// Reverse the string because binary is read from right to left while
// strings are read from left to right.
String flippedString = newString.reverse().toString();
// stringLength is necessary to properly traverse the string and to know
// when to stop.
int stringLength = flippedString.length();
long result = 0;
// For-loop that will traverse through the string until the of the
// string and, using the proper math formula,
// evaluate the numeric value of the binary number.
for (int i = 0; i < stringLength; i++) {
result += (Math.pow(2, i) * Character.getNumericValue(flippedString.charAt(i)));
}
return result; // Returns the decimal value in type double.
}
}
スタックトレースしてください。 –
エラーを生成している行に注釈を付けてください。書式設定されたコードは、特に行番号なしでは非常に理解しにくいです。 –
これは無料のデバッグサービスではありません。あなたは[スタックトレース](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to)を理解しようとしたようではありません。 -debug-my-application-errors)を実行します。あなたは[デバッガを使用した]ようではありません(http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems)。 – Raedwald