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

  • Home
  • SEARCH
  • 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 6131031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:55:37+00:00 2026-05-23T16:55:37+00:00

I have been trying for a while now to figure out how to save

  • 0

I have been trying for a while now to figure out how to save data on the Android device or even an SD card to be read by the app. So far no luck. I used the Android SAX Parser to read xml from a server just file, and even from the res/raw folder as well. However, my troubles are in writing the xml file itself. This is just a test program, and my problem at the moment is that while trying to create the file, like 26 ( File dataDir = getDir(“DataDir”,0); ) generates an error. Here’s the code below, followed by the logcat. Any and all help, even just in general about file-writing, would be greatly appreciated. Thank you.

MainActivity.java

package sample.matt.filemanip;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import org.xmlpull.v1.XmlSerializer;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    TextView tv;
    File dataDir = getDir("DataDir",0);
    final String fileLoc = dataDir.getPath()+"/stringfile.xml";
    private ArrayList<String> strings = new ArrayList<String>();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        for(int r = 0; r<10; r++)
            strings.add("iteration "+r);

        tv = (TextView)findViewById(R.id.text_view);
        writeXmlToFile();

        loadFeed(ParserType.ANDROID_SAX);

    }

    private void loadFeed(ParserType type){
        try {
            Log.i("AndroidNews", "ParserType="+type.name());
            FeedParser parser = FeedParserFactory.getParser(fileLoc);
            strings = parser.parse();
            tv.setText(strings.get(0));
        } catch (Throwable t){
            Log.e("AndroidNews",t.getMessage(),t);
            Toast.makeText(MainActivity.this, "An error occured while reading quotes.", Toast.LENGTH_SHORT).show();
        }
    }

    private void writeXmlToFile(){

        File newxmlfile = new File(getCacheDir()+"/stringfile.xml");
        try{
                newxmlfile.createNewFile();
        }catch(IOException e){
                Log.e("IOException", "exception in createNewFile() method");
        }
        FileOutputStream fileos = null;        
        try{
                fileos = new FileOutputStream(newxmlfile);
        }catch(FileNotFoundException e){
                Log.e("FileNotFoundException", "can't create FileOutputStream");
        }
        XmlSerializer serializer = Xml.newSerializer();
        try {
                        serializer.setOutput(fileos, "UTF-8");
                        serializer.startDocument("", Boolean.valueOf(true));
                        //serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
                        serializer.startTag("", "root");
                        for (String str: strings){
                            serializer.startTag("", "string");
                                serializer.startTag(null, "content");
                                serializer.text(str);
                                serializer.endTag("", "content");
                            serializer.endTag(null, "string");
                        }
                        serializer.endTag(null, "root");
                        serializer.endDocument();
                        serializer.flush();
                        fileos.close();

                        tv.setText("file has been created");
                } catch (Exception e) {
                        Log.e("Exception","error occurred while creating xml file");
                }
    }
}

Logcat

07-13 12:03:02.499: ERROR/AndroidRuntime(7531): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{sample.matt.filemanip/sample.matt.filemanip.MainActivity}: java.lang.NullPointerException
07-13 12:03:02.499: ERROR/AndroidRuntime(7531):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
07-13 12:03:02.499: ERROR/AndroidRuntime(7531):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-13 12:03:02.499: ERROR/AndroidRuntime(7531):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-13 12:03:02.499: ERROR/AndroidRuntime(7531):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-13 12:03:02.499: ERROR/AndroidRuntime(7531):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-13 12:03:02.499: ERROR/AndroidRuntime(7531):     at android.os.Looper.loop(Looper.java:123)
07-13 12:03:02.499: ERROR/AndroidRuntime(7531):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-13 12:03:02.499: ERROR/AndroidRuntime(7531):     at java.lang.reflect.Method.invokeNative(Native Method)
07-13 12:03:02.499: ERROR/AndroidRuntime(7531):     at java.lang.reflect.Method.invoke(Method.java:521)
07-13 12:03:02.499: ERROR/AndroidRuntime(7531):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
07-13 12:03:02.499: ERROR/AndroidRuntime(7531):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-13 12:03:02.499: ERROR/AndroidRuntime(7531):     at dalvik.system.NativeStart.main(Native Method)
07-13 12:03:02.499: ERROR/AndroidRuntime(7531): Caused by: java.lang.NullPointerException
07-13 12:03:02.499: ERROR/AndroidRuntime(7531):     at android.content.ContextWrapper.getDir(ContextWrapper.java:198)
07-13 12:03:02.499: ERROR/AndroidRuntime(7531):     at sample.matt.filemanip.MainActivity.<init>(MainActivity.java:21)
07-13 12:03:02.499: ERROR/AndroidRuntime(7531):     at java.lang.Class.newInstanceImpl(Native Method)
07-13 12:03:02.499: ERROR/AndroidRuntime(7531):     at java.lang.Class.newInstance(Class.java:1429)
07-13 12:03:02.499: ERROR/AndroidRuntime(7531):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-13 12:03:02.499: ERROR/AndroidRuntime(7531):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)

Thank you for your time, if something is unclear, please ask!

edit: Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="sample.matt.filemanip"
      android:versionCode="1"
      android:versionName="1.0">


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

edit: new logcat error:

07-13 12:44:50.334: ERROR/AndroidNews(7640): Caused by: java.net.MalformedURLException: Protocol not found: /data/data/sample.matt.filemanip/app_DataDir/stringfile.xml
07-13 12:44:50.334: ERROR/AndroidNews(7640):     at java.net.URL.<init>(URL.java:275)
07-13 12:44:50.334: ERROR/AndroidNews(7640):     at java.net.URL.<init>(URL.java:159)
07-13 12:44:50.334: ERROR/AndroidNews(7640):     at sample.matt.filemanip.BaseFeedParser.<init>(BaseFeedParser.java:17)
  • 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-23T16:55:37+00:00Added an answer on May 23, 2026 at 4:55 pm

    Don’t call the method out of a method. declare File dataDir where you did, and call dataDir = getDir("DataDir",0); inside onCreate. Same for the next statement.

    // Snip
    File dataDir;
    final String fileLoc;
    private ArrayList<String> strings = new ArrayList<String>();
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        dataDir = getDir("DataDir",0);
        fileLoc = dataDir.getPath()+"/stringfile.xml";
    
    // snap
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to figure this out for a while now and even
I have been trying to figure this out for a little while now and
I'm been trying to figure this out for a while now, and have just
I have been trying to figure out this problem for a while now and
I have been trying for a while now, but I can't figure out how
Okay I've been trying to figure this out for a while now. I have
I have been trying for a while now trying to figure out how to
I have been trying to figure out a Java RegEx for some while now
For a while now i have been trying to figure out the algorithms behind
I've been trying this for quite a while now, but can't figure it out.

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.