-1

I'm trying to read this list of names and phone numbers in. But the the program keeps throwing nullpointerexception. Why is it doing this?

int i=0;
        Datab[] b = null;
         //b = null;

        // The name of the file to open.
        String fileName = "datt.dat";

        try {
            // Use this for reading the data.
            byte[] buffer = new byte[1000];
            FileInputStream FileIS = new FileInputStream("datt.dat");
            ObjectInputStream input = new ObjectInputStream(FileIS);

            for(i=0; i<b.length; i++)
             b[i]=(Datab)input.readObject();

            input.close();
  • Where does it throw it? Which line? – Michał Szydłowski Apr 01 '15 at 14:25
  • Please provide the exception stack trace. Side note on your code: you are defining a variable with the file name .. just to reuse the same string again (instead of using the string variable that you declared in front of the try block. – GhostCat Apr 01 '15 at 14:26

1 Answers1

1

b is null, so b.length throws the exception

shibley
  • 1,528
  • 1
  • 17
  • 23