I’ve had this really annoying problem for a couple of days, and I cannot quite seem to solve it. I am trying to open a .csv file, so I imported it into the res/raw/ folder of my project. Then I am trying to open and read it via the getResources() method, and that’s exactly where I get the NullPointerException. Here’s the method that reads the file and fills an array with the available lines.
Here’s my Activity where I create an Object newwords from the class Words, and then I want to call the PlayWithRawFiles() method from class Words.
public class Swear_Activity extends Activity implements OnInitListener, OnClickListener {
private Words newwords;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swear);
}
public void onClick(View view){
try {
newwords.PlayWithRawFiles();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("greshka");
e.printStackTrace();
}
}
}
now this is the class where i get the error
public class Words{
public Word[] wordsArray;
private String locale = "de";
public Words(String locale) {
if (locale != null ) {
this.locale = locale;
}
}
Context c;
public void PlayWithRawFiles() throws IOException {
String str="";
StringBuffer buf = new StringBuffer();
int i = 0;
InputStream is = c.getResources().openRawResource(R.raw.est);
BufferedReader reader = null;
try{
if (is != null) reader = new BufferedReader(new InputStreamReader(is));
}
catch(Exception e) {
System.out.println ("ss");
e.printStackTrace();
}
if (is!=null) {
while ((str = reader.readLine()) != null) {
Word wd = new Word(1,"str");
this.wordsArray[i] = wd;
i++;
}
}
is.close();
}
}
here is class Word
public class Word {
private int type;
private String data;
public Word(int type, String data){
this.type = type;
this.data=data;
}
public int getType(){
return this.type;
}
public String getData(){
return this.data;
}
}
Here is the Stack Trace
07-10 13:28:58.558: E/AndroidRuntime(647): FATAL EXCEPTION: main
07-10 13:28:58.558: E/AndroidRuntime(647): java.lang.NullPointerException
07-10 13:28:58.558: E/AndroidRuntime(647): at de.android.swearapp.Swear_Activity.onClick(Swear_Activity.java:32)
07-10 13:28:58.558: E/AndroidRuntime(647): at >android.view.View.performClick(View.java:2408)
07-10 13:28:58.558: E/AndroidRuntime(647): at android.view.View$PerformClick.run(View.java:8816)
07-10 13:28:58.558: E/AndroidRuntime(647): at android.os.Handler.handleCallback(Handler.java:587)
07-10 13:28:58.558: E/AndroidRuntime(647): at android.os.Handler.dispatchMessage(Handler.java:92)
07-10 13:28:58.558: E/AndroidRuntime(647): at android.os.Looper.loop(Looper.java:123)
07-10 13:28:58.558: E/AndroidRuntime(647): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-10 13:28:58.558: E/AndroidRuntime(647): at java.lang.reflect.Method.invokeNative(Native Method)
07-10 13:28:58.558: E/AndroidRuntime(647): at java.lang.reflect.Method.invoke(Method.java:521)
07-10 13:28:58.558: E/AndroidRuntime(647): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-10 13:28:58.558: E/AndroidRuntime(647): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-10 13:28:58.558: E/AndroidRuntime(647): at dalvik.system.NativeStart.main(Native Method)
What have I done wrong? I’m really stuck at this one. Thank you in advance!
Two things I see here,
newwordsas innewwords = new Words("");cis null and not assigned a context.I recommend you re-code the class like this:
What is done is, we simply added the
Contextas part of the constructor for the classWords. Then to instantiate the classWordsdo this, from yourSwearActivityclass: