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.