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 7159599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:15:28+00:00 2026-05-28T13:15:28+00:00

I try to save some data in internal storage on android but I keep

  • 0

I try to save some data in internal storage on android but I keep getting a NullPointerException I think it has to do with the getFilesDir() I’m using but I’m not sure. Can Some Please help clarify if that is that case and help me write this file to the device. Here the Error message im am getting

01-20 22:11:59.020: E/AndroidRuntime(329): FATAL EXCEPTION: main
01-20 22:11:59.020: E/AndroidRuntime(329): java.lang.NullPointerException

01-20 22:11:59.020: E/AndroidRuntime(329):  at android.content.ContextWrapper.getFilesDir(ContextWrapper.java:178)

01-20 22:11:59.020: E/AndroidRuntime(329):  at yantz.imageapp4.main.writeElement(main.java:89)

01-20 22:11:59.020: E/AndroidRuntime(329):  at yantz.imageapp4.Panel.onTouchEvent(Panel.java:102)

Here is the oncreate of the main class

   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   FrameLayout sv = new FrameLayout(this);
    LinearLayout ll = new LinearLayout(this);
     test = new Panel(this);
    test.settest(1)   ;   
    ll.setOrientation(LinearLayout.VERTICAL);
    sv.addView(test);
    sv.addView(ll);
    setContentView(sv);}

Here my OnTouch method in the panel class

public boolean onTouchEvent(MotionEvent event) {
    mainactivity=new main();    
    mainactivity.writeElement(new Element(getResources(),(int) event.getX(),(int)  event.getY()));
        Log.v("Gesture", "is 1 ");   
        return super.onTouchEvent(event);
 }

Here is my writeObject method inside my main class

public void writeElement(Element obj){
    Log.v("main", "made it to method writeElement" );
    File f = new File(getFilesDir()+FILENAME);
    try {
fos = new FileOutputStream(f);
    ObjectOutputStream objectwrite = new ObjectOutputStream(fos);
    objectwrite.writeObject(obj);
 fos.close(); 
 Log.v("main", "file was  made File ");

 }catch (FileNotFoundException e){
    e.printStackTrace();
    Log.v("main", "file was not made File not found ");
 } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    Log.v("main", "file was not made File IOException ");
}
}

manifest

<uses-sdk android:minSdkVersion="10" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name="yantz.imageapp4.main" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

        <activity android:name=".charactors" android:label="@string/app_name"
        android:theme="@android:style/Theme.Black">
        <intent-filter>
    <action android:name="yantz.imageapp4.charactors" />
    <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
<activity android:name=".main2" android:label="@string/app_name"
        android:theme="@android:style/Theme.Black">
        <intent-filter>
<action android:name="yantz.imageapp4.main2" />
<category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
 </application>

</manifest>
  • 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-28T13:15:28+00:00Added an answer on May 28, 2026 at 1:15 pm

    I know what’s wrong. You can’t just write

    mainactivity=new main();
    

    getFilesDir should get the correct context instance but it doesn’t now.
    Try something like that:

    public boolean onTouchEvent(MotionEvent event) {
        main.writeElement(new Element(...), this.getContext);
        return super.onTouchEvent(event);
    }
    
    public static void writeElement(Element obj, Context context){
        ...
        File f = new File(context.getFilesDir(), FILENAME);
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

according to this image, i try to save data in NSUserDefaults but my App
I'm using Hibernate 3.2.1 and database SQLServer2000 while I'm try to insert some data
I'm trying to catch WM_QUERYENDSESSION to save some data in the app, but it
i use C# threading and parsing some data and when i try to save
Using CakePHP 1.3 I'm trying to save some data to a MySQL database. The
My requirement is i want to save some data in database by using web-services.
I try to create a form that can save a person's form data so
I am coding a little script to save some data from Internet every single
I'm trying to use a function with PostgreSQL to save some data. Here is
I'm saving some data using a series of NSDictionaries, stored in an NSMutableArray 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.