I struggled for 3 days with loading the csv file on the android. I have created the raw folder and added the brojler.csv file to it. However, every time I try to load it, I get a NullPointerException error. I tried to read and solve it myself, but based on this topic (What is a NullPointerException, and how do I fix it?) I can not find a reason. I am a freshman in android, however, I think that all variables have been defined well. Strangely, in another application the same code works and correctly loads the file.
Brojler.csv have 9 columns and 12 rows.
In addition, I changed the way of declaring the lists like below but without success
List<String> ingredients = new ArrayList<>();
List<String> em = new ArrayList<>();
List<String> bo = new ArrayList<>();
And so on,
My code:
package com.example.nick.calcy;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Mix extends AppCompatActivity {
InputStream inputStream;
List<String> ingredients,em,bi,ws,ca,fs,na,L,M = new ArrayList<>();
String[] ids;
EditText et1,et2,et3,et4,et5,et6,et7;
Float sum;
Button buttonSum;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mix);
et1 = findViewById(R.id.editText11);
et2 = findViewById(R.id.editText12);
et3 = findViewById(R.id.editText13);
et4 = findViewById(R.id.editText14);
et5 = findViewById(R.id.editText15);
et6 = findViewById(R.id.editText16);
et7 = findViewById(R.id.editText17);
buttonSum = findViewById(R.id.button11);
inputStream = getResources().openRawResource(R.raw.brojler);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String csvLine;
while ((csvLine = reader.readLine()) != null) {
ids=csvLine.split(",");
try{
ingredients.add(ids[0]);
em.add(ids[1]);
bi.add(ids[2]);
ws.add(ids[3]);
ca.add(ids[4]);
fs.add(ids[5]);
na.add(ids[6]);
L.add(ids[7]);
M.add(ids[8]);
}catch (Exception e){
Log.e("Unknown error",e.toString());
}
}
}
catch (IOException ex) {
throw new RuntimeException("Error in reading CSV file: "+ex);
}
buttonSum.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if(et1.getText().toString().trim().length() == 0 ||
et2.getText().toString().trim().length() == 0 ||
et3.getText().toString().trim().length() == 0 ||
et4.getText().toString().trim().length() == 0 ||
et5.getText().toString().trim().length() == 0 ||
et6.getText().toString().trim().length() == 0 ||
et7.getText().toString().trim().length() == 0) {
CharSequence text = "Blank fields detected";
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
}
else{
Intent i = new Intent(getApplicationContext(),Summary.class);
Float x1 = Float.parseFloat(et1.getText().toString());
Float x2 = Float.parseFloat(et2.getText().toString());
Float x3 = Float.parseFloat(et3.getText().toString());
Float x4 = Float.parseFloat(et4.getText().toString());
Float x5 = Float.parseFloat(et5.getText().toString());
Float x6 = Float.parseFloat(et6.getText().toString());
Float x7 = Float.parseFloat(et7.getText().toString());
sum = x1+x2+x3+x4+x5+x6+x7;
i.putExtra("x1", x1);
i.putExtra("x2", x2);
i.putExtra("x3", x3);
i.putExtra("x4", x4);
i.putExtra("x5", x5);
i.putExtra("x6", x6);
i.putExtra("x7", x7);
if (sum == 100){
startActivity(i);
} else{
CharSequence text = "Sum must equal 100%";
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
}
}}
});
}
}
My errors:
E/Unknown error: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
I/chatty: uid=10083(u0_a83) com.example.nick.calcy identical 1 line
E/Unknown error: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
E/Unknown error: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
I/chatty: uid=10083(u0_a83) com.example.nick.calcy expire 7 lines
E/Unknown error: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
I suppose that the problem may be the level of access to the file. In a working application, i use the code in main_activity, and in another app it fails cuz it's called in another (not main) activity.
Help would be really appreciated