0

Trying to finish a lab in Android Studio. After attempting to read a text file from assets to a string, using InputStream, BufferedReader, and StringBuilder the string is apparently null and I can't seem to figure out why. Any help is appreciated. Any help is greatly appreciated. The text file content is:

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      

Part of it is commented out from trying to test and fine what was wrong, code is:

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]);
                    }




                }
            }); */

    }
}

EDIT: Error Log:

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.access$2300(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.access$2300(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) 

EDIT: after adding System.out.println(storage) to while loop:

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  
Matt
  • 81
  • 2
  • 11

1 Answers1

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

Shouldn't inputT be replaced by storage here?

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

The condition of your while loop is "when storage == null, break off"

Here is how to put the builded String inside of storage again

        while (( (storage = bufferedReader.readLine())) !=null) {
            stringBuilder.append(storage);
        }
        storage = stringBuilder.toString();
        inputT.close();
        }
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • Unfortunately that didn't fix the error, though that may have helped some. – Matt Oct 08 '15 at 05:35
  • Try System.out.println(storage) each time you loop and tell me if it prints out something in your console – Yassin Hajaj Oct 08 '15 at 05:37
  • Ok so it's been read right... When do you see it is equals to null? – Yassin Hajaj Oct 08 '15 at 06:59
  • As posted in my edit, I get a null pointer exception and the app crashes, apparently at line 126. Commenting out the second half of my code results in setting off an alert dialog based on an else statement where the previous if statement is `if (storage != null)` – Matt Oct 08 '15 at 12:10
  • I've inserted println statements in different places throughout my code, and apparently some time right after the while loop, storage becomes null. I even put a println in the finally statement, which also shows storage as null. – Matt Oct 08 '15 at 14:21