2016-04-27 23 views
-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. 
    } 
} 
+1

スタックトレースしてください。 –

+0

エラーを生成している行に注釈を付けてください。書式設定されたコードは、特に行番号なしでは非常に理解しにくいです。 –

+1

これは無料のデバッグサービスではありません。あなたは[スタックトレース](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

答えて

0

達成したくないことを理解するのは難しいです。しかし、このスニペットは良くなります:

  String filename = "RAMerrors.txt"; 
      // Try-catch block that will catch any errors while opening the file. 
      try { 
       FileReader fileReader = new FileReader(filename); 
       BufferedReader bufferReader = new BufferedReader(fileReader); 

       int counter = 0; 
       String line = null; 
       while ((line = bufferReader.readLine()) != null) { 
        System.out.println(line); 
        counter++; // Counter to keep track of the amount of lines in 
        BigInteger b = new BigInteger(line, 16); // the BigInteger class does the parsing of very long hex numbers 
        System.out.println(b.toString(10)); // and also conversion to String in decimal 
        long c = b.longValue(); // or get the long value 

        // if the numbers are only long values this will be sufficient: 
        long c2 = Long.parseLong(line, 16); 

        if (c <= 8589934584L) { 
         System.out.println("Error " + counter + " is located in RAM Chip 0"); 
        } else if (c <= 17179869184L) { 
         System.out.println("Error " + counter + " is located in RAM Chip 1"); 
        } else if (c <= 25769803768L) { 
         System.out.println("Error " + counter + " is located in RAM Chip 2"); 
        } else if (c <= 34359738368L) { 
         System.out.println("Error " + counter + " is located in RAM Chip 3"); 
        } else { 
         System.out.println("Error " + counter + " is not in range"); 
        } 
       } 
       bufferReader.close(); 
      } catch (FileNotFoundException ex) { 
       System.out.println("Unable to open file '" + filename + "'"); 
      } catch (IOException ex) { 
       System.out.println("Error reading file '" + filename + "'"); 
      } 

いくつかの注意:

  • チェックはまた、16進数で行うことができる - 多分それは、より読みやすいのか?
  • バイトを反転する必要がある場合は、事前に文字列上でそれを実行するか、 バイトをlong値に置き換えます。