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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:56:01+00:00 2026-06-14T06:56:01+00:00

I’m trying to build a small app that takes a data file in external

  • 0

I’m trying to build a small app that takes a data file in external storage and emails it. I keep getting ‘null pointer exceptions’ right away in logcat and then my app dies. I can’t seem to locate my exception and I am wondering if there is a way to determine the line of code that is causing this. I have the MainActivity class and then a class called SendData- the code is below. I’m new at this so any help would be greatly appreciated- thank you.

    private static final String TAG = "MainActivity_ErrorLog";

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

// Create the getData intent
    Intent intentgetData = new Intent(MainActivity.this, SendData.class); 

    public void onStart()
    {
        {       
        try 
        {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

            if (sd.canWrite()) 
            {
        // verify the paths
                String currentDBPath = "TLC_COMMON/database.db";
                String backupDBPath = "database.db"; 
                File currentDB = new File(data, currentDBPath);
                File backupDB = new File(sd, backupDBPath);

                if (currentDB.exists()) 
                {
                    FileChannel src = new FileInputStream(currentDB).getChannel();
                    FileChannel dst = new FileOutputStream(backupDB).getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();                    
                }
            }
        }

        catch (Exception e)     
        {   
            // change the V below to E when complete
            Log.v(TAG,"ERROR: Database file not created"); 
        }
        startActivity(intentgetData);
        }

    }

  } 

–new class

public class SendData extends Activity
{
private static final String TAG = “MainActivity”;

/* Checks if external storage is available to read */   
public boolean isExternalStorageReadable() 
    {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state) ||
        Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) 
        {
        return true;
        }
        return false;
    }

/** Called when the user clicks the Send My Data button */
public SendData(View view) 
    {

    // Send data by email
        {
            File root = Environment.getExternalStorageDirectory(); 
    // verify it is saving as this file name; also path in previous class
            String fileName = "database.db"; 
            if (root.canWrite()) 
                { 
                File attachment = new File(root, fileName); 
                Intent email = new Intent(Intent.ACTION_SENDTO);                
                email.putExtra(android.content.Intent.EXTRA_SUBJECT, "Exercise data");
                email.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"test@gmail.com"});
                // is the Uri necessary?
                email.putExtra(android.content.Intent.EXTRA_TEXT, Uri.fromFile(attachment));
                // look at this VVV
                startActivity(Intent.createChooser(email, "Send the data via Email"));} 
            else 
                {
                // Change the V below to E when complete
                Log.v(TAG, "Email send failed");
                }
            }
        }
public void finish() 
{   
}

}

11-13 13:29:37.343: W/dalvikvm(3319): threadid=1: thread exiting with uncaught exception (group=0x418e3300)
11-13 13:29:37.343: E/AndroidRuntime(3319): FATAL EXCEPTION: main
11-13 13:29:37.343: E/AndroidRuntime(3319): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.va.datasender/com.example.va.datasender.MainActivity}: java.lang.NullPointerException
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.os.Looper.loop(Looper.java:137)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at java.lang.reflect.Method.invokeNative(Native Method)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at java.lang.reflect.Method.invoke(Method.java:511)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at dalvik.system.NativeStart.main(Native Method)
11-13 13:29:37.343: E/AndroidRuntime(3319): Caused by: java.lang.NullPointerException
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.content.ComponentName.<init>(ComponentName.java:75)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.content.Intent.<init>(Intent.java:3301)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at com.example.va.datasender.MainActivity.<init>(MainActivity.java:36)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at java.lang.Class.newInstanceImpl(Native Method)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at java.lang.Class.newInstance(Class.java:1319)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
11-13 13:29:37.343: E/AndroidRuntime(3319):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
11-13 13:29:37.343: E/AndroidRuntime(3319):     ... 11 more

  • 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-06-14T06:56:02+00:00Added an answer on June 14, 2026 at 6:56 am

    Find the lowest “Caused by” and continue down until you reach a line from your code:

    Caused by: java.lang.NullPointerException
         at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
         at android.content.ComponentName.<init>(ComponentName.java:75)
         at android.content.Intent.<init>(Intent.java:3301)
         at com.example.va.datasender.MainActivity.<init>(MainActivity.java:36)
    

    The <init> means you are doing something as a class variable or inside a constructor before the Activity has a valid Context… But the specific problem is on line 36.

    I would guess that you are trying to create an Intent with this. Initialize your Intent inside onCreate() instead.


    Found it. Change this:

    // Create the getData intent
    Intent intentgetData = new Intent(MainActivity.this, SendData.class); 
    

    to:

    Intent intentgetData;
    

    and inside onCreate() (or onStart() just before calling startActivity()) add:

    intentgetData = new Intent(MainActivity.this, SendData.class);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to create an if statement in PHP that prevents a single post
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.