0

I have one class called DisplayResults that contains an array of strings called answers. I need to take this array, and use it to locate certain rows in an Excel spreadsheet. DisplayResults is an activity class, and ProductFinder is a non activity class (just a Java class). For some reason, I cannot get Apache POI to create a workbook without throwing a NullPointerException.

DisplayResults.java

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;

public class DisplayResults extends AppCompatActivity {

    ArrayList answers; // List of answers from the survey

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


        Intent i = getIntent();
        answers = (ArrayList)i.getSerializableExtra("answers");


        ProductFinder p = new ProductFinder(answers,this);
    }


}

Once I declare the ProductFinder p object, I would like to use it to interact with an excel sheet.

ProductFinder.java

public class ProductFinder {


    ArrayList answers;

    ProductFinder(ArrayList answers, Context context)
    {
        this.answers = answers;
        InputStream inputStream = context.getResources().openRawResource(
                context.getResources().getIdentifier("product_list",
                        "raw", context.getPackageName()));

        Workbook wb = null;
        try {
            wb = WorkbookFactory.create(inputStream);

        } catch (IOException e)
        {
            System.err.print("error opening file");
        }


            System.out.println("Workbook has " + wb.getNumberOfSheets() + " Sheets ");


    }
}

I am getting a NullPointerException on the line where I call getNumberOfSheets(), and the stack trace points to that function as the problematic one. I made an if statement to check if the InputStream is null, and it is not. Also, my product_list.xlsx sheet is stored in the res/raw directory of my project.

Connor S
  • 353
  • 2
  • 12
  • 1
    You should certainly use a `File` object to create the workbook instead of an `InputStream`. Can you try with this code: `try (Workbook wb = new XSSFWorkbook(new File(getClass().getResource("/res/raw/product_list.xlsx").getFile()))) { System.out.println(wb.getNumberOfSheets()); }` ? – XtremeBaumer Nov 16 '18 at 07:08
  • This doesn't work. The ProductFinder class is not an activity, so it doesn't share the /res folder. That's what I'm using the context variable for, I'm just not sure how to use it. This solution gives a java.lang.NoClassDefFoundError on the line inside the try block – Connor S Nov 16 '18 at 07:29
  • And for which class do you get the error? – XtremeBaumer Nov 16 '18 at 07:51
  • In the ProductFinder class – Connor S Nov 16 '18 at 08:11
  • Apologies, error happens in Product finder, but the exact text is this: java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/xml/stream/XMLEventFactory; – Connor S Nov 16 '18 at 08:13
  • 1
    Have a look [here](https://stackoverflow.com/a/38512164/7109162) – XtremeBaumer Nov 16 '18 at 08:27
  • Not sure how to use that. A big problem with these old threads is they talk about having the exact right versions of everything, but Java is totally different now. For example, that github link everyone is posting shows a project that fixes all of this for Android 5, but it hasn't been updated in 3 years – Connor S Nov 16 '18 at 16:49

0 Answers0