-1

I am facing this error when I try to write objects to a file.

02-05 11:31:41.818: W/System.err(2414): java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[], int, int)' on a null object reference
02-05 11:31:41.818: W/System.err(2414):     at java.io.DataOutputStream.writeShort(DataOutputStream.java:192)
02-05 11:31:41.818: W/System.err(2414):     at java.io.ObjectOutputStream.writeStreamHeader(ObjectOutputStream.java:1795)
02-05 11:31:41.818: W/System.err(2414):     at java.io.ObjectOutputStream.<init>(ObjectOutputStream.java:271)
02-05 11:31:41.818: W/System.err(2414):     at com.data.mydata.addData(addData.java:173)

The line 173 which shows the error is oos = new ObjectOutputStream(fileStream);

My code is -

path = Environment.getExternalStorageDirectory()
                    + File.separator + "Documents";

        path += File.separatorChar + "Sensor Data";
        Log.d("hi", "path = " + path);
        File file = new File(path, filename);

        new File(path).mkdirs();
        try {
            file.createNewFile();
                        fileStream = new FileOutputStream(filename);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

//AllData extends Serializable

List<AllData>valuelist = new ArrayList<AllData>(sensor.values());

if(valuelist.size() != 5){
                try {
                    //fileStream = new FileOutputStream(filename);
                    oos = new ObjectOutputStream(fileStream);
                    oos.writeObject(valuelist);

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }else{
                try {
                    oos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

Basically when my size of arraylist is not equal to 5, I need to keep writing to the file and once its 5, I need to stop it. My file is created properly in SDCard in Documents/Sensor Data/myfile.txt . Could you please help me how do I resolve this error?

FAZ
  • 255
  • 1
  • 3
  • 13
  • 1
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – m0skit0 Feb 05 '16 at 10:57

1 Answers1

1

From the error it seems that some element of valuelist is null. Do something like

         try {
                //fileStream = new FileOutputStream(filename);
                oos = new ObjectOutputStream(fileStream);
               + List<AllData> nonNullList = new ArrayLis<>();
               + for(AllData aData: valuelist)
               +    if(aData != null) nonNullList.add(aData);
               + oos.writeObject(nonNullList);

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

Of course if your condition list == 5 elements needs to always write 5 elements, this null-elimination list should be in the place of

   if(valuelist.size() != 5){
alex
  • 115
  • 6
  • Thank you for your time and help :) I used this but still same problem – FAZ Feb 05 '16 at 11:21
  • Uhm I don't know the data structure, but surely you are invoking the write on a null object. You should debug and check if some field in the AllData has null's – alex Feb 05 '16 at 11:28