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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:56:17+00:00 2026-06-05T22:56:17+00:00

im developing a app in Android. in this project im using a List of

  • 0

im developing a app in Android. in this project im using a List of Objects and i need to convert the list object in byte array. but there is a null pointer exception shown in logcat? here is my coading.

class MyContact
{
    String Name;
    Bitmap Image;
    String Number;

    public MyContact(String cName, String cNumber, Bitmap cImage) {

        Name    = cName;
        Number  = cNumber;
        Image   = cImage;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public Bitmap getImage() {
        return Image;
    }

    public void setImage(Bitmap image) {
        Image = image;
    }

    public String getNumber() {
        return Number;
    }

    public void setNumber(String number) {
        Number = number;
    }
}



List<MyContact> list = new ArrayList<MyContact>();

I added some values to the List object, using

MyContact contactObj = new MyContact(name, phoneNo, photo);
 list.add(contactObj);

bla bla bla ....

and i need store the entire list object into the Android Mobiles local file. i used ,

FileOutputStream fos = openFileOutput( fileName, Context.MODE_PRIVATE);
 fos.write(toByteArray(MyTrackList));   // null pointer error occured here
 fos.close();

here is a byte array conversion coding.

public static byte[] toByteArray (Object obj)
    {
      byte[] bytes = null;
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      try {
        ObjectOutputStream oos = new ObjectOutputStream(bos); 
        oos.writeObject(obj);
        oos.flush(); 
        oos.close(); 
        bos.close();
        bytes = bos.toByteArray ();
      }
      catch (IOException ex) {
        //TODO: Handle the exception
      }
      return bytes;
    }

but returns null pointer exception in the line of above i marked in comment
how can i resolve this problem. any suggestion? thanks in advance….

  • 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-05T22:56:18+00:00Added an answer on June 5, 2026 at 10:56 pm
      catch (IOException ex) {
        //TODO: Handle the exception
      }
    

    There is your problem: If there is an IOException, your method will just return null and not tell you about it.

    Do not catch the exception, change the method to throws IOException (since there is nothing you can do at that point anyway) and then find out what caused it.

    Most likely it is because your MyContact class does not implement Serializable, so that it cannot be written to an ObjectOutputStream.

    Also, performance and memory-usage can be improved by not having that intermediate byte array and connecting the ObjectOutputStream directly to the FileOutputStream instead.

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

Sidebar

Related Questions

I'm developing an Android App using PhoneGap, but I found a problem very soon.
I have project where i'm developing an Android App using a lot of existing
I am developing an android app for my project, I need to find room
I'm developing an Android app (Android 1.6), but this is probably a more general
I am very new to android and I am developing this app which allows
I am developing a simple sip-based voip app for android. I used this sample
Main issue is that I'm developing an app for Android and iOS but can't
I'm developing a fun little project using Foursquare API. In this project, I have
I am developing a chat application for Android, using AIR/Flex and Red5. But Acoustic
I'm developing an app to android but I can't use the Override annotations. It

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.