0
package tinoonline.apscalculator;

import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class ReadUniversityData extends AppCompatActivity {
List<StoreUniversityData> the_row_contect = new ArrayList<>();

public void setuniversityConetct(){

    //importing the file into the memory
    Log.d("MyActivity", "File " + R.raw.alluniversitystuff);

    InputStream unistuff = this.getClass().getResourceAsStream("C:\\Users\\Tino\\AndroidStudioProjects\\APS_Calc\\APS_Calculator\\app\\src\\main\\res\\raw\\uni.csv");

   // creating a reader for it

   BufferedReader reader = new BufferedReader(new 
InputStreamReader(unistuff, Charset.forName("UTF-8")));

   //A string array to then store the the tokens , tokens are the individual text segments
   String line = "";
   //tryimg to see if the thing will work or not
   try{
   while((line = reader.readLine()) != null){
    // split by commas
       String[] tokeens = line.split(",");

   //read the data then initialize it into the university class

        StoreUniversityData sample = new StoreUniversityData(tokeens[0],tokeens[1]
       ,tokeens[2],tokeens[3],tokeens[4],tokeens[5],tokeens[6],
                tokeens[7],tokeens[8],tokeens[9],tokeens[10],tokeens[11]);

    the_row_contect.add(sample);

    Log.d("MyActivity","Just created: " + sample);}
   } catch (IOException e){
       Log.wtf("MyActivity","Error Reading data file" + line + e);
       e.printStackTrace();
   }
}

public List getuniversitydata(){

    return the_row_contect;

}

}

when I run the application i get an error message:

java.lang.RuntimeException: Unable to start activity ComponentInfo{tinoonline.apscalculator/tinoonline.apscalculator.UniRequirments}: java.lang.NullPointerException

and it says caused by:

Caused by: java.lang.NullPointerException

I don't understand why it is saying that the variable is empty ,I have the file there in the correct format and I directly linked it via the path

This is how the .csv file looks internally the first set of text being the column headinsg

    ID,Faculty,Course Name,Degree Type,University ,Course length,APS ,Subjects 1,Subjects 2,Subjects 3,Extra Requirements,Description

1,Engineering and the Built Environment,Aeronautical Engineering,Bachelor of Science in Engineering BSc (Eng),University of Witwatersrand,4,40,5English HL or 1st Add Lang,5Mathematics,5Physical Science,"Note: Due to the limited number of places available, admission is not guaranteed. Selection will be based on final NSC results. Generally students who achieve at least 70% in Mathematics, Physical Science and English stand a greater chance of being accepted. ","The aeronautical engineer is involved in the design, development and modification of the components and systems of all types of flight vehicles - including fixed wing aircraft, helicopters, sail planes, airships and missiles."

This is the file which I am using

  • If you are running your app inside an Android emulator, it cannot access your local pc file system. You need to store the csv file as an asset. See this answer https://stackoverflow.com/questions/19974708/reading-csv-file-in-resources-folder-android – Sanj Aug 19 '18 at 19:43
  • Look at https://stackoverflow.com/questions/16570523/getresourceasstream-returns-null – Ashraff Ali Wahab Aug 19 '18 at 19:49
  • `unistuff` is null because `this.getClass().getResourceAsStream("C:\\Users\\Tino\\AndroidStudioProjects\\APS_Calc\\APS_Calculator\\app\\src\\main\\res\\raw\\uni.csv");` gives null because there is no `this.getClass().getResourceAsStream("C:\\Users\\Tino\\AndroidStudioProjects\\APS_Calc\\APS_Calculator\\app\\src\\main\\res\\raw\\uni.csv");` on the emulator – Vladyslav Matviienko Aug 19 '18 at 20:21
  • Thank you so much for the help I think I got past that error, but even now with the use of assest `InputStream unistuff = null; try { unistuff = (getAssets().open("alluniversitystuffassets.csv")); } catch (IOException e) { e.printStackTrace(); }` I am still getting a null error ` java.lang.NullPointerException: Attempt to invoke virtual method` on thsi line unistuff = (getAssets().open("alluniversitystuffassets.csv"));` – Tino kurimwi Aug 20 '18 at 08:53

0 Answers0