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....


  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.

4

2 に答える 2

4
  catch (IOException ex) {
    //TODO: Handle the exception
  }

問題があります。IOException がある場合、メソッドは null を返すだけで、それについては通知しません。

例外をキャッチせず、メソッドを変更してthrows IOException(とにかくその時点でできることは何もないため)、その原因を突き止めます。

ほとんどの場合、MyContactクラスが を実装していないSerializableため、ObjectOutputStream に書き込むことができません。

また、その中間バイト配列を持たず、代わりに ObjectOutputStream を直接 FileOutputStream に接続することで、パフォーマンスとメモリ使用量を改善できます。

于 2012-06-20T04:40:34.907 に答える
0

The possible problem is nicely pointed out by @Thilo. Can you post the Logcat output please?

[ Suggestion :

I usually recommend using JSON/XML serialization as Android SDK has support for some really nice JSON/XML(DOM/SAX/XML pull) implementations.

  • They make your local files readable, portable.
  • And you can even transfer the files over the network, and store remotely too.

Hope this helps, Cheers! ]

于 2012-06-20T04:54:27.917 に答える