-1

This might be a very naive question but I can't get this java.lang.Exception error to go away. I also just started learning java and android so... this might be an easy problem to solve.

So I have a main activity in android and I want to implement a weka ml classifier within the app. Right now I'm just trying to load some data.

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ml_classifiers ml_object = new ml_classifiers();
    int number = ml_object.loadData();

Trying to load data...

public class ml_classifiers {
public int loadData() {
    ConverterUtils.DataSource source = new ConverterUtils.DataSource("C:/Users/Seth/Desktop/iris.arff");
    Instances data = source.getDataSet();
    int num = data.numInstances();
    return num;
    }
}

Why does java.lang.exception occur?

Seth
  • 3
  • 1
  • 1
    `C:/Users/Seth/Desktop/iris.arff` on your mobile device? – Blackbelt Oct 12 '16 at 16:29
  • Oh.... it is not :( I can see how that would be a problem. Eventually the data will stream through bluetooth and get put in a database but for now I was just trying to run some sample classifiers. – Seth Oct 12 '16 at 16:37

1 Answers1

1

Why does java.lang.exception occur?

In the future, when you encounter crashes, use LogCat to examine the Java stack trace associated with the crash. If you do not understand the stack trace or otherwise cannot identify the problem, and you want help here, post the stack trace along with the code. As it stands, we have to guess exactly what is going wrong in your app.

In this case, while you may be crashing elsewhere, you will definitely crash with:

ConverterUtils.DataSource source = new ConverterUtils.DataSource("C:/Users/Seth/Desktop/iris.arff");

assuming that this is code in your Android app.

You are attempting to read data from C:/Users/Seth/Desktop/iris.arff. That is a path to a file on a Windows machine. Android is not Windows. There are ~2 billion Android devices in use, and none of them have access to files on your Windows' machine's desktop.

You need to get this data onto the Android device, such as:

  • by putting it in the assets/ directory in your module (to package it with your app), then using AssetManager to get an InputStream on that asset, hopefully passing that directly to ConverterUtils.DataSource

  • downloading the file from the Internet, perhaps into internal storage (e.g., getCacheDir())

  • expecting the user to copy the file onto their device by hand, such as via external storage

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491