1

宿題のために、ファイルとの間でバイト配列を読み書きできるクラスを作成する必要があります。CSV とテキストを読み書きできるクラスを正常に作成できましたが、配列に関しては少し問題があります。以下のコードは、私が書いたクラスを特徴としています。これは主に私の CSV クラス、FileInput クラスhttp://www.devjavasoft.org/SecondEdition/SourceCode/Share/FileInput.java ) および FileOutput クラス ( http://www.devjavasoft.org/SecondEdition/SourceCode/ ) に基づいています。 Share/FileOutput.java )。

プログラムを実行してテキスト ファイルを読み取ると、次のエラー メッセージが表示されます。

    "Exception in thread "main" java.lang.NullPointerException
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
at java.io.FileReader.<init>(FileReader.java:58)
at com.gc01.FileManager.FileInput.<init>(FileInput.java:22)
at com.gc01.FileManager.ByteManager.readByte(ByteManager.java:28)
at com.gc01.FileManager.ByteManager.main(ByteManager.java:85)"

そして私のコード:

import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;


public class ByteManager {

public String getByteFile(){
    Scanner sc = new Scanner (System.in);
    System.out.println("Please enter the file directory of the chosen txt file?");
    System.out.println("For Example: /Users/UserName/Downloads/FileName.txt");
    ///Users/ReeceAkhtar/Desktop/FileName.txt
    final String fileName = sc.nextLine();
    System.out.println("How many columns are in the file?");
    final int columns = sc.nextByte();
    System.out.println("How many rows are in the file?");
    final int rows = sc.nextByte();
    return fileName;
    }



public void readByte(final String fileName, int columns,  int rows){
    FileInput in = new FileInput(fileName);
    int [] [] data = new int[rows] [columns];   
    String [] line;

    for (int i = 0; i < rows; i++){
        line = in.readString().split("\t");
        for (int j = 0; j < columns; i++){

            data [i][j] = Byte.parseByte(line[j]);
        }
    }
    System.out.println("******File Read*****"); 
}   


public String chooseFileOutput(){
    Scanner sc = new Scanner (System.in);
    System.out.println("Please enter the file directory for the output of the chosen file");
    System.out.println("For Example: /Users/UserName/Downloads/FileName.txt");
    ///Users/ReeceAkhtar/Desktop/GeoIPCountryWhois.csv
    final String fileNameOUT = sc.nextLine();
    System.out.println("How many columns are in the file?");
    final int columnsOut = sc.nextByte();
    System.out.println("How many rows are in the file?");
    final int rowsOut = sc.nextByte();
    return fileNameOUT;
    }

public void writeByte(final String fileNameOUT,  int columnsOut, int rowsOut){
    FileOutput createData = new FileOutput (fileNameOUT);
    int newData = 0;

    System.out.println("Enter data. To finish, enter 'TERMINATE_FILE'");

    while(!"TERMINATE_FILE".equals(newData)){
        Scanner input = new Scanner (System.in);
        int [] [] data = new int[rowsOut] [columnsOut]; 
        String [] line = null;
        for (int i = 0; i < rowsOut; i++){
            createData.writeInteger(newData = input.nextByte());
            System.out.println("\t");
            for (int j = 0; j < columnsOut; i++){
                data [i][j] = Byte.parseByte(line[j]);  
            } 
        }   
        createData.close();
    }   
}

public static void main(String[] args) {
    Scanner in = new Scanner (System.in);
    final ByteManager object = new ByteManager ();

    System.out.println("1 for Read File, 2 for Write file");
    String choice = in.nextLine();
    if("1".equals(choice)){
        object.getByteFile();
        object.readByte(null, 0, 0);
    } else if ("2".equals(choice)){
        object.chooseFileOutput();
        object.writeByte(null, 0, 0);
    } else{
        System.out.println("Goodbye!");
        System.exit(0);
    }
}

}


アップデート

以下のコメントとアドバイスに感謝します。解決できない別の問題に遭遇しました。readByte メソッドを書き直しました。ただし、実行すると、コンパイラエラーは発生しなくなりました(アドバイスのおかげです)が、ファイルの内容を印刷できません。代わりに、コンソールには「File Read」と表示されるだけです。さまざまなリソースを調べましたが、解決策が見つかりません。どこかの単純な間違いだと確信しています。読み込もうとしているファイルの内容も以下です。

public String getByteFile(){
    Scanner sc = new Scanner (System.in);
    System.out.println("Please enter the file directory of the chosen txt file?");
    System.out.println("For Example: /Users/UserName/Downloads/FileName.txt");
    ///Users/ReeceAkhtar/Desktop/FileName.txt
    final String fileName = sc.nextLine();
    System.out.println("How many columns are in the file?");
    final int columns = sc.nextInt();
    System.out.println("How many rows are in the file?");
    final int rows = sc.nextInt();
    return fileName;
    }



public void readByte(final String fileName, int rows,int columns){

BufferedReader br = null;

String[] line;
String splitBy =  "\t";
int [][] data = new int[rows] [columns];


try {
    br = new BufferedReader(new FileReader(fileName));  
        for (int i = 0; i < rows; i++){
            line = br.toString().split(splitBy);
            for (int j = 0; j < columns; j++){
                data[i] [j] = Integer.parseInt(line[j]);
                System.out.println(data[i][j]);
            }
        }       
    } catch (FileNotFoundException e){
        e.printStackTrace();
    } catch (IOException e) {   
        e.printStackTrace();  
    } finally {  
        if (br != null) {  
        try {  
        br.close();  
    } catch (IOException e) {  
        e.printStackTrace();  
    }
        }
    }
    System.out.println("*****File Read*****");
}

ファイルの内容 (タブ区切り)


123     6565
123     6564
123     6563
123     6562
4

2 に答える 2

1

このコードはエラーの原因です

object.readByte(null, 0, 0);

パラメータnullは に対して無効な状態ですFileInput。ファイル名の文字列である必要があります。

于 2013-10-28T12:49:31.990 に答える
1

fromnullに引数を渡していますreadByte()main()

object.readByte(null, 0, 0); 

そして、readByte()

FileInput in = new FileInput(fileName); //here it throws NPE  

有効なファイル名を渡します。

NullPointerException

public class NullPointerException
    extends RuntimeException

オブジェクトが必要な場合に、アプリケーションが null を使用しようとするとスローされます。これらには以下が含まれます:

  • null オブジェクトのインスタンス メソッドの呼び出し。
  • null オブジェクトのフィールドへのアクセスまたは変更。
  • null の長さを配列であるかのように取得します。
  • 配列であるかのように null のスロットにアクセスまたは変更します。
  • Throwable 値であるかのように null をスローします。
于 2013-10-28T12:49:45.743 に答える