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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:03:42+00:00 2026-06-01T10:03:42+00:00

Hi i am reading inbox using service but i m getting null pointer exception

  • 0

Hi i am reading inbox using service but i m getting null pointer exception when i am creating cursor to read inbox previously i was able to read the sms with the same code now i want to store these sms into xml file in sd card but i am getting a null pointer exception . any help will be appreciated.
my code is here

// enter code here

public class CreateBackupXml extends Service 
{
CreateBackup createBackup = null;
@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}
public void onCreate(Bundle savedinstance)
{

    Log.d("service","inside on create of service");
}
public void onDestroy()
{

}
public void onStart(Intent intent, int startid) 
{
    Toast.makeText(this, "Creating Back up", Toast.LENGTH_LONG).show();
    Log.d("animation", "animation should be Start");
    createBackup = new CreateBackup();
    createBackup.createXml("all");
}

public class CreateBackup
{
    Uri uriSMSURI = Uri.parse("");
    Cursor cur=null;
    public void createXml(String choice)
    {
        if(choice=="all")
        {
            this.readSms("Inbox");
        }
    }
    public void readSms(String folder)
    {

        if(folder.equals("inbox"))
        {
            uriSMSURI = Uri.parse("content://sms/inbox");
        }
        else if(folder.equals("sent"))
        {
                uriSMSURI = Uri.parse("content://sms/sent");
        }
        try{
            Log.d("folder","inside of inbox");
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            Log.d("checking","1111111111111");
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            Log.d("checking","2222222222");
            Document document = documentBuilder.newDocument();
            Log.d("checking","333333333333");
            Element rootElement = document.createElement("SMSBackUp");
            Log.d("checking","444444444");
            rootElement.setAttribute("category", folder);
            Log.d("checking","5555555555");
            rootElement.setAttribute("owener", "gaurav");      
            Log.d("checking","6666666666");             
            cur = getContentResolver().query(uriSMSURI, null, null, null,null);
            Log.d("checking",""+cur);
            Log.d("checking","7777777777777");
            while (cur.moveToNext()) 
            {
                Log.d("checking","88888888");
                Element smsElement = document.createElement("sms");
                rootElement.appendChild(smsElement);

                Element text = document.createElement("text");
                smsElement.appendChild(text);
                text.appendChild(document.createTextNode(cur.getString(11)));

                Element phonenoElement = document.createElement("number");
                smsElement.appendChild(phonenoElement);
                phonenoElement.appendChild(document.createTextNode(cur.getString(2)));

                Element nameElement = document.createElement("author");
                smsElement.appendChild(nameElement);
                nameElement.appendChild(document.createTextNode(getContact(cur.getString(2))));                 
            }
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            Properties outFormat = new Properties();
            outFormat.setProperty(OutputKeys.INDENT, "yes");
            outFormat.setProperty(OutputKeys.METHOD, "xml");
            outFormat.setProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
            outFormat.setProperty(OutputKeys.VERSION, "1.0");
            outFormat.setProperty(OutputKeys.ENCODING, "UTF-8");
            transformer.setOutputProperties(outFormat);
            DOMSource domSource = new DOMSource(document.getDocumentElement());
            OutputStream output = new ByteArrayOutputStream();
            StreamResult result = new StreamResult(output);
            String xmlString = output.toString();
            Log.d("output xml",xmlString);

        }
        catch(Exception e)
        {
            Log.e("execption ","can not create back up" +e);
        }
    }
    private String getContact(String number)
    {

        Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
        String returnName= "";
        if(cur.getCount() > 0)
        {
            while(cur.moveToNext())
            {
                  String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                  Log.d("Contact","ID:" + id );
                  String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                  Log.d("Contact","name:" + name);
                  if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
                  {
                      Cursor pcur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                      ContactsContract.CommonDataKinds.Phone.NUMBER + "=?",new String[]{number},null);
                      int numindex = pcur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA);
                      if(pcur.moveToFirst())
                      {
                          String dbNum = pcur.getString(numindex);
                          if(dbNum.equals(number))
                          {
                              returnName = name;
                              Log.d("number","number:" + dbNum);
                          }
                          else 
                          {
                              Log.d("number","numbers dont match!");
                          }
                      }
                      else 
                      {
                          Log.d("number", "no result");
                      }
                  }
                  else 
                  {
                       Log.d("number", "No Number");
                  }
            }
        }
        return returnName;
    }

}

}

logcat view

04-05 12:00:07.770: D/reading sms(456): inside of create backup for inbox
04-05 12:00:07.830: D/animation(456): animation should be Start
04-05 12:00:07.830: D/folder(456): inside of inbox
04-05 12:00:07.858: D/checking(456): 1111111111111
04-05 12:00:07.860: D/checking(456): 2222222222
04-05 12:00:07.881: D/checking(456): 333333333333
04-05 12:00:07.890: D/checking(456): 444444444
04-05 12:00:07.920: D/checking(456): 5555555555
04-05 12:00:07.920: D/checking(456): 6666666666
04-05 12:00:07.920: D/checking(456): null
04-05 12:00:07.920: D/checking(456): 7777777777777
04-05 12:00:07.920: E/execption(456): can not create back upjava.lang.NullPointerException
  • 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-01T10:03:43+00:00Added an answer on June 1, 2026 at 10:03 am

    //because of the case sensitive your condition is filed

    change your Inbox to inbox

    or try with

    equalsIgnoreCase()
    
          {
                this.readSms("inbox");
     Log.d("entering into ","inbox");
            }
        }
        public void readSms(String folder)
        {
    
            if(folder.equals("inbox"))
    
    uriSMSURI = Uri.parse("content://sms/inbox");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently developing application that does following: Read Inbox (using MAPI) & SMS
I using below code to insert new sms in inbox, but when i click
I'm having trouble on reading my inbox in gmail using Oauth 2.0. I'm using
I have a mail reading service that reads every email from an inbox, parses
I am trying to learn how to read the SMS outbox on Android. There
Reading this old but classic document Writing High-Performance Managed Applications - A Primer ,
I'm using javamail to check an IMAP inbox, and at the moment I'm simply
Reading conflicting opinions on using SQL session state vs custom db table in MVC
Reading about the Dispose pattern , I see the documentation repeatedly refer to cleaning
Reading some questions here on SO about conversion operators and constructors got me thinking

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.