Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6181851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:05:59+00:00 2026-05-24T01:05:59+00:00

What I’m doing exactly is this: I have a class Welcome that calls another

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:06:00+00:00Added an answer on May 24, 2026 at 1:06 am

    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 Activity from textWriter and instead pass in a Context in the constructor, i.e.:

    public class textWriter{
        private Context m_context;
    
        public textWriter(Context ctx) {
            m_context = ctx;
        }    
        public void write(String name, String something) {
    
            try {
                FileOutputStream output;
                output = m_context.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();
            }
        }
    }
    

    You’d then need to construct it like so: textWriter tw = new textWriter(this);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I have a text area in my form which accepts all possible characters from
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.