0

I'm making a stock simulator app in java for and android phone. Im trying to get a csv file from a URL and parse the columns into different ArrayLists but it keeps crashing. I made a class for it and added 2 lines to the manifest since I thought that's what crashed it:

Permissions in my manifest:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

My code:

public class StockALLDownloader {
    public static final String LOG_TAG = "com.example.someone.stocksimulator.StockALLDownloader";
    String url = "https://api.iextrading.com/1.0/ref-data/symbols?format=csv";

    private ArrayList<String> symbols;
    private ArrayList<String> names;

    public ArrayList<String> getSymbols() {
        return symbols;
    }
    public ArrayList<String> getNames() {
        return names;
    }

    public StockALLDownloader(){ //gets url and adds to the symbols and names arrays
        try{
            URL iexfin = new URL(url);
            URLConnection data = iexfin.openConnection();
            Scanner input = new Scanner(data.getInputStream());
            if (input.hasNext()){ //skip header line
                input.nextLine();
            }
            while (input.hasNextLine()){
                String line = input.nextLine();
                Scanner scanner  =  new Scanner(line);
                scanner.useDelimiter(",");
                symbols.add(scanner.next());
                names.add(scanner.next());
                scanner.close();
            }
            input.close();
        }
        catch (Exception e){
            Log.e(StockALLDownloader.LOG_TAG, "error stock all downloader");
        }
    }
}
Banana
  • 2,435
  • 7
  • 34
  • 60
  • What error do you get when it crashes? What line does it crash on? Separate responsibilities, so fetch the data in one function and pass the data to a different function so you can write unit tests. – Dragonthoughts Feb 07 '18 at 11:37
  • And return List not ArrayList. That way you can change to an alternative List implementation without changing the unit tests or function signature. – Dragonthoughts Feb 07 '18 at 11:39
  • If the names correspond to symbols, and one is unique, store the results together in a Map. That will reduce possible errors should you add logic which could make your lists get out of sync. – Dragonthoughts Feb 07 '18 at 11:41
  • Try to use the try with resource pattern to ensure autocloseable classes, like the scanner, are always closed and resources released when an exception is thrown. – Dragonthoughts Feb 07 '18 at 11:44
  • @Dragonthoughts Thanks for the tips :) it crashes on the line: Scanner input = new Scanner(data.getInputStream()); – Noad Klein Feb 07 '18 at 11:47
  • What exactly is the error that you receive. E.g. null pointer exception? – Dragonthoughts Feb 07 '18 at 11:48
  • 1
    I found the error: "android.os.NetworkOnMainThreadException" might be because i'm running it on the main thread – Noad Klein Feb 07 '18 at 11:51
  • So, split up your functions and delegate to another thread, probably using a CompletableFuture. And build unit tests. Single Responsibility part of SOLID really is your friend. – Dragonthoughts Feb 07 '18 at 11:56
  • For future questions, work out what we need to know and tell us, please, rather than having to get the location and error type by having to ask. – Dragonthoughts Feb 07 '18 at 11:59

0 Answers0