0

I'm trying to figure out how to read a CSV file stored in the assets folder. What I tried doing is using an InputFileStream as I couldn't get the a FileReader to find the files. So I put the files into the assets folder and used an InputFileStream.

To do this I used an AssetManager. The problem is this is a non activity class and it would be really hard to pass the context from the main activity because it is a large series of methods.

It starts with loadCategory():

  loadCategory(0);

Which in term calls the class ToolSource (a singleton):

public void loadCategory(int categoryIndex) {
    ...
ToolsCategory cat = ToolsSource.getInstance().getCategory(categoryIndex);

Which creates an array of ToolCategories[]:

 public static ToolsSource getInstance() {
    if (instance == null) {
        try {
            instance = new ToolsSource();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return instance;
}

public ToolsSource() throws IOException {
    int i;
    mCategory = new ToolsCategory[CATEGORIES.length];
    for (i = 0; i < CATEGORIES.length; i++) {
        mCategory[i] = new ToolsCategory(CATEGORIES[i]);
    }
}

Which finally gets to the main class where this error is occurring. The point of this class is to populate a Tool[] array (individual categories) which in turn populate each of the ToolCategories in the mCategory[] array. LoadCategory loads the calls getCategory(0) which pulls the name of the first category in mCategory[] and assigns it through an adapter.

What I want to do is parse a CSV that contains the information of all the tools as individual lines in a CSV that represents each category. e.g. categoryName.csv contains 3 lines ergo 3 tools.

It then iterates and assigns the information in the 3 lines to the member fields of the class Tool(). e.g. mTools[i].setTitle(nextLine[0]);

package com.anothergamedesigner.listviewtest;
import android.content.Context;
import android.content.res.AssetManager;
import com.opencsv.CSVReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ToolsCategory {

final int TOOLS_PER_CATEGORY = 10;

Context context;

private CSVReader reader;
private String categoryName;
Tool[] mTools;

public ToolsCategory(String name) throws IOException {
    this.context = context.getApplicationContext();
    AssetManager assetManager = context.getAssets();
    categoryName = name;
    mTools = new Tool[TOOLS_PER_CATEGORY];
    assignValues(categoryName, assetManager);
}

private void assignValues(String name, AssetManager am) {
    String categoryCSV = name + ".csv";
    System.out.println("category name is: "+ categoryCSV);

    int i;
    for(i= 0; i<mTools.length; ++i){
        try {
            InputStream csvStream = am.open(categoryCSV);
            InputStreamReader csvStreamReader = new InputStreamReader(csvStream);
            reader = new CSVReader(csvStreamReader);

            String [] nextLine;

            while ((nextLine = reader.readNext()) != null) {
                // nextLine[] is an array of values from the line
                System.out.println("New title is: " + nextLine[0]);
                mTools[i].setTitle(nextLine[0]);
                System.out.println("New Subtitle is: " + nextLine[0]);
                mTools[i].setSubtitleTxt(nextLine[1]);
                System.out.println("New Description is: " + nextLine[0]);
                mTools[i].setDescriptionTxt(nextLine[2]);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public int getToolCount(){
    return mTools.length;
}

public Tool getTool(int index){
    return mTools[index];
}
}

However, I'm getting an error that states: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference.

Is there any way for me to fix this or an alternate way to retrieve the Uri/file path of the csv so that I can give it to the CSVReader parser?

NappyXIII
  • 195
  • 2
  • 17
  • @mbmc Yeah, it essentially is at this point. I saw that, but I couldn't get it to work for some reason that I can't remember at this point. But after trying again and people resuggesting that solution, I got it to work. I think it had to do with me just not knowing how to apply the context retrieval in a singleton (ToolsSource) – NappyXIII Jun 17 '16 at 16:33

2 Answers2

1

You can just pass the context as parameter in the constructor.

 Context c;
 ...
 public ToolsCategory(String name, Context c) throws IOException {
     this.c= c;
     ...
}
Erik Minarini
  • 4,795
  • 15
  • 19
1

You need to give the Context of Activities

loadCategory(0); - > loadCategory(0, this);

like this fix the code :

1.

   public void loadCategory(int categoryIndex, Context context) {
    ...
   ToolsCategory cat = ToolsSource.getInstance(context).getCategory(categoryIndex);

2.

public static ToolsSource getInstance(Context context) {
    if (instance == null) {
        try {
            instance = new ToolsSource(context);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return instance;
}

3.

public ToolsSource(Context context) throws IOException {
    int i;
    mCategory = new ToolsCategory[CATEGORIES.length];
    for (i = 0; i < CATEGORIES.length; i++) {
        mCategory[i] = new ToolsCategory(CATEGORIES[i], context);
    }
}

4.

public ToolsCategory(String name, Context context) throws IOException {
    this.context = context;
    AssetManager assetManager = context.getAssets();
    categoryName = name;
    mTools = new Tool[TOOLS_PER_CATEGORY];
    assignValues(categoryName, assetManager);
}
Leejunho
  • 41
  • 4