0

I need to create a String ArrayList from another String ArrayList by splitting the strings from the first one into the second one. The data comes from a .txt file being read in by a BufferedReader. This app is supposed to display a spinner from which the user can choose a student's name then calculate their grade and display that as well. The biggest hurdle right now is that I get a null pointer exception when I try to use .split. As you can probably tell, I'm fairly new to this and could use some help. Thanks!

grades.txt

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      

java code

    public class MainActivity extends AppCompatActivity {

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

    String strName, strLastName, strFirstName, strFinalGrade;

    int intTest1, intTest2, intTest3, intFinal, intFinalGrade;

    int intPointsPossible = 400;

    int finalGrades[] = new int[0];

    AssetManager am = getAssets();
    ArrayList<String> list = new ArrayList<String>();
    ArrayList<String> finalList = finalList = new ArrayList<String>();
            BufferedReader reader;
    String line = " ";
    String item = " ";

    try {
        InputStream input = am.open("grades.txt");
        reader = new BufferedReader(new InputStreamReader(input));

        while (line != null){
            line = reader.readLine();
            list.add(line);

        }
        while (item != null){
            for (int i = 0; i < list.size(); i++){
                line.split("\\s+");
                finalList.add(item);
            }
        }

        input.close();

    } catch (IOException e) {
        e.printStackTrace();
    }




    Spinner spinner = new Spinner(this);

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

    spinner.setAdapter(dataAdapter);

    dataAdapter.notifyDataSetChanged();

}}
2
  • line.split("\\s+"); returns a String[], should you be doing something with that? Commented Oct 5, 2016 at 3:34
  • while (item != null) ... this is an infinite loop Commented Oct 5, 2016 at 3:35

1 Answer 1

1

The biggest hurdle right now is that I get a null pointer exception when I try to use .split

You are running while (line != null){

Which means that in-order for this loop to stop, line will be null.

Let's assume this is a typo....

ArrayList<String> finalList = finalList = new ArrayList<String>();

Anyways, you can Arrays.asList(line.split("\\s+") to get a list, which you can addAll into the finalList.

BufferedReader reader;

try {
    InputStream input = am.open("grades.txt");
    reader = new BufferedReader(new InputStreamReader(input));
    String line;

    while ((line = reader.readLine()) != null) {
        String[] parts = line.split("\\s+");
        finalList.addAll(Arrays.asList(parts));
    }

    input.close();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (reader != null) reader.close();
}

Though, realistically, I don't think this is what you want. because

This app is supposed to display a spinner from which the user can choose a student's name then calculate their grade and display that as well

Each line should be parsed into a Student object, probably, then finalList can be List<Student>

public class Student {
    String firstName, lastName;
    int test1, test2, test3, final; // or List<Integer> tests;
}

Then you need an ArrayAdapter<Student> that you can customize to display only the name of the student.

Next, you need an item selection listener on the Spinner to calculate and display the grades on the Activity.

These are separate issues you should post new questions for, though.


Then, you have Spinner spinner = new Spinner(this);, but this Spinner is not coming from your XML layout... maybe you should use findViewById to get the Spinner that is

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! At least I have a direction now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.