尝试读取文件后 rrString 为空

rrString is null after trying to read file

尝试在 Android Studio 中完成实验。在尝试使用 InputStream、BufferedReader 和 StringBuilder 将文本文件从资产读取为字符串后,字符串显然为空,我似乎无法弄清楚原因。任何帮助表示赞赏。任何帮助是极大的赞赏。文本文件内容为:

Name            Test1   Test2   Test3   Final
Adam    Anderson    81  90  85  87
Ben Brown       77  80  68  94
Chris   Cross       74  80  56  62
Don Dare        86  94  90  89
Eric    Earl        96  93  90  98
Fred    Foley       79  92  59  86
Gina    Gray        80  83  95  87
Holly   Hank        74  77  75  78
Ian Ingram      66  64  56  60
Jill    Johnson     90  98  78  89      

其中一部分被注释掉,试图测试并修正错误,代码是:

import android.app.AlertDialog;
import android.content.res.AssetManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class App1Act1 extends AppCompatActivity {
    String storage;
    int[] testAverages;
    int[] testScores;
    String[] studentNames;
    String[] arrayStorage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app1_act1);



        /*for(i=0; i<11; i++){
            studentNames[i] = inputT.split(" "); */
        //error message
        AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
        //StringTokenizer
        AssetManager am = getAssets();

        try {
            InputStream inputT = am.open("grades.txt");
            InputStreamReader inputStreamReader = new InputStreamReader(inputT);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            storage = " ";
            StringBuilder stringBuilder = new StringBuilder();

            while (( (storage = bufferedReader.readLine())) !=null) {
                stringBuilder.append(inputT);
            }

            inputT.close();
            }



        catch(FileNotFoundException e) {

            dlgAlert.setMessage("File was not found, please import file and try again.");
            dlgAlert.setTitle("Error Message...");
            dlgAlert.setPositiveButton("OK", null);
            dlgAlert.setCancelable(true);
            dlgAlert.create().show();
        }

        catch(IOException e){
            dlgAlert.setMessage("Oops!  Something happened!"); //in the tradition of windows 10
            dlgAlert.setTitle("Error Message...");
            dlgAlert.setPositiveButton("OK", null);
            dlgAlert.setCancelable(true);
            dlgAlert.create().show();
        }

        finally {


        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_app1_act1, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onStart () {
        super.onStart();

        AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);

        if (storage != null) {
             arrayStorage = storage.split("\s+");
        }
        else {
            dlgAlert.setMessage("...crap...");
            dlgAlert.setTitle("Error Message...");
            dlgAlert.setPositiveButton("OK", null);
            dlgAlert.setCancelable(true);
            dlgAlert.create().show();
        }
        /*//array to store names
        for (int i=5, s=0; i <= 64; i+=6, s++) {

            studentNames[s] = arrayStorage[i] + arrayStorage[i+1];
        }
        //parse string scores to in array
        for(int i=7, p=0; i<=64; i+=6, p+=4) {
            testScores[p] = Integer.parseInt(arrayStorage[i]);
            testScores[p+1] = Integer.parseInt(arrayStorage[i+1]);
            testScores[p+2] = Integer.parseInt(arrayStorage[i+2]);
            testScores[p+3] = Integer.parseInt(arrayStorage[i+3]);
        }
        //calculate and store student averages

        for(int i=0, p=0; i<=59; i+=4, p++) {
            testAverages[p] = (testScores[i] + testScores[i+1] + testScores[i+2] +testScores[i+3]) / 4;
        }

        List<String> spinnerArray = new ArrayList<String>(Arrays.asList(studentNames));

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_spinner_item, spinnerArray);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
        final Spinner sItems = (Spinner) findViewById(R.id.spinner);
        sItems.setAdapter(adapter);

        final TextView textView = (TextView) findViewById(R.id.textView);

        //set up button for getting grade
        Button getGrade = (Button) findViewById(R.id.button);
            getGrade.setOnClickListener(new View.OnClickListener() {

                String selected = sItems.getSelectedItem().toString();

                public void onClick(View v) {
                    if (selected.equals(studentNames[0])) {
                        textView.setText(testAverages[0]);
                    }
                    else if (selected.equals(studentNames[1])) {
                        textView.setText(testAverages[1]);
                    }
                    else if (selected.equals(studentNames[2])) {
                        textView.setText(testAverages[2]);
                    }
                    else if (selected.equals(studentNames[3])) {
                        textView.setText(testAverages[3]);
                    }
                    else if (selected.equals(studentNames[4])) {
                        textView.setText(testAverages[4]);
                    }
                    else if (selected.equals(studentNames[5])) {
                        textView.setText(testAverages[5]);
                    }
                    else if (selected.equals(studentNames[6])) {
                        textView.setText(testAverages[6]);
                    }
                    else if (selected.equals(studentNames[7])) {
                        textView.setText(testAverages[7]);
                    }
                    else if (selected.equals(studentNames[8])) {
                        textView.setText(testAverages[8]);
                    }
                    else if (selected.equals(studentNames[9])) {
                        textView.setText(testAverages[9]);
                    }




                }
            }); */

    }
}

编辑:错误日志:

10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime: FATAL EXCEPTION: main
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{name.csit.lab4app1/name.csit.lab4app1.App1Act1}: java.lang.NullPointerException
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at android.app.ActivityThread.access00(ActivityThread.java:125)
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:123)
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4627)
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:521)
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:  Caused by: java.lang.NullPointerException
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at name.csit.lab4app1.App1Act1.onStart(App1Act1.java:126)
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at android.app.Activity.performStart(Activity.java:3781)
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at android.app.ActivityThread.access00(ActivityThread.java:125) 
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99) 
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:123) 
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4627) 
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:521) 
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
10-08 05:34:07.554 273-273/name.csit.lab4app1 E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 

编辑:将 System.out.println(存储)添加到 while 循环后:

10-08 05:46:39.575 273-273/name.csit.lab4app1 I/System.out: Name            Test1   Test2   Test3   Final
10-08 05:46:39.585 273-273/name.csit.lab4app1 I/System.out: Adam    Anderson    81  90  85  87
10-08 05:46:39.585 273-273/name.csit.lab4app1 I/System.out: Ben Brown       77  80  68  94
10-08 05:46:39.585 273-273/name.csit.lab4app1 I/System.out: Chris   Cross       74  80  56  62
10-08 05:46:39.585 273-273/name.csit.lab4app1 I/System.out: Don Dare        86  94  90  89
10-08 05:46:39.585 273-273/name.csit.lab4app1 I/System.out: Eric    Earl        96  93  90  98
10-08 05:46:39.585 273-273/name.csit.lab4app1 I/System.out: Fred    Foley       79  92  59  86
10-08 05:46:39.585 273-273/name.csit.lab4app1 I/System.out: Gina    Gray        80  83  95  87
10-08 05:46:39.585 273-273/name.csit.lab4app1 I/System.out: Holly   Hank        74  77  75  78
10-08 05:46:39.585 273-273/name.csit.lab4app1 I/System.out: Ian Ingram      66  64  56  60
10-08 05:46:39.585 273-273/name.csit.lab4app1 I/System.out: Jill    Johnson     90  98  78  89  
while (( (storage = bufferedReader.readLine())) !=null) {
                stringBuilder.append(inputT);
            }

这里的inputT不应该换成storage吗?

while (( (storage = bufferedReader.readLine())) !=null) {
                stringBuilder.append(storage);
            }

你的 while 循环的条件是 "when storage == null, break off"

下面是如何将构建的字符串再次放入存储中

        while (( (storage = bufferedReader.readLine())) !=null) {
            stringBuilder.append(storage);
        }
        storage = stringBuilder.toString();
        inputT.close();
        }