Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to convert List<Object> to byte array using java or Android?
    primarykey
    data
    text
    <p>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.</p> <pre><code>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&lt;MyContact&gt; list = new ArrayList&lt;MyContact&gt;(); </code></pre> <p>I added some values to the List object, using</p> <pre><code>MyContact contactObj = new MyContact(name, phoneNo, photo); list.add(contactObj); bla bla bla .... </code></pre> <p>and i need store the entire list object into the Android Mobiles local file. i used ,</p> <pre><code>FileOutputStream fos = openFileOutput( fileName, Context.MODE_PRIVATE); fos.write(toByteArray(MyTrackList)); // null pointer error occured here fos.close(); </code></pre> <p>here is a byte array conversion coding.</p> <pre><code>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; } </code></pre> <p>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....</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload