What I’m doing exactly is this:
I have a class “Welcome” that calls another class “textWriter” which contains a method “write”. Welcome passes two strings to “write”, “name” is the name of the text file to write to and “something” is the thing to print.
i keep getting null pointer error on prt = new PrintStream(output);
here is the logcat
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): FATAL EXCEPTION: main
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): java.lang.NullPointerException
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:158)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at com.rhobot.budget.textWriter.write(textWriter.java:24)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at com.rhobot.budget.Welcome.setSubChoices(Welcome.java:37)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at com.rhobot.budget.Welcome$1.onClick(Welcome.java:76)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at android.view.View.performClick(View.java:2408)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at android.view.View$PerformClick.run(View.java:8816)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at android.os.Handler.handleCallback(Handler.java:587)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at android.os.Handler.dispatchMessage(Handler.java:92)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at android.os.Looper.loop(Looper.java:123)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at java.lang.reflect.Method.invoke(Method.java:521)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at dalvik.system.NativeStart.main(Native Method)
here is the Welcome class:
public class Welcome extends Activity
{
P p = new P(); //shared prefs helper
loadArray la = new loadArray(); //array loader
textWriter tw = new textWriter();
private EditText txtBalance;
private Button btnEnter;
int subItemCount, addItemCount;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.welcomescreen);
capture();
}
public static String sub_choice_key = "sub_choice_key.txt";
public static String sub_compare = "sub_compare.txt";
public static String add_choice_key = "add_choice_key.txt";
public static String add_compare = "add_compare.txt";
public void setSubChoices() throws FileNotFoundException {
// TODO create a master list of choices
tw.write(sub_choice_key,"-1");
tw.write(sub_choice_key,"-2");
tw.write(sub_choice_key,"-3");
tw.write(sub_compare,"-1");
tw.write(sub_compare,"-2");
tw.write(sub_compare,"-3");
p.pisI("subItemCount", 3, this);
}...
Here is the textWriter class
public class textWriter extends Activity{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
public void write(String name, String something) {
try {
FileOutputStream output;
output = openFileOutput(name,MODE_APPEND);
PrintStream prt;
prt = new PrintStream(output);
prt.println(something);
prt.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
id like to emphasize that these are two different files as well, and i am importing all the right stuff i believe.. also i have the line
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in my android manifest.
what am i doing wrong? is it context? or lack there of? if so how should i implement it?
i am doing it this way because i want to use this textWrite in other programs as an easy (less messy) way of writing data. in my main it could save me about 50 lines of code if i do it this way.
Thanks in advance-
Tricknology
As probablykevin said, the issue is a lack of a valid Context.
One simple fix you could do if you want your class structure to stay the same is to remove the
extends ActivityfromtextWriterand instead pass in a Context in the constructor, i.e.:You’d then need to construct it like so:
textWriter tw = new textWriter(this);