1

I'm very new to android programming, but I have some experience in Java and C++. While I have been able to do much of the program, I'm stuck with a NPE in FileInputStream. I'm trying to create an Attendance program, which tracks the attendance of a student in lectures. Here's the code which throws the NPE:

    public class Attendance extends Activity {
Subject s[] = new Subject[13];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    for(int i=0;i<13;i++) {
        s[i] = new Subject();
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

public void loadData(Subject s[]) throws IOException{
    for(int i=0;i<13;i++) {
        int a[] = new int [2];
        int x=0;
        try {
            FileInputStream fIn = openFileInput("s["+i+"].txt");
            InputStreamReader isr = new InputStreamReader(fIn); //NPE occurs here
            //char buff[] = new char[100];
            //isr.read(buff);
            BufferedReader br = new BufferedReader(isr);
            String str = new String();
            while ((str=br.readLine())!=null) {
                a[x]=Integer.parseInt(str);
                x++;
            }
            s[i].acceptAttd(a[0]);
            s[i].acceptLecs(a[1]);
        }
        catch(IOException e) {
            //do nothing.
        }
    }
}

public void addAttnd(View v) throws IOException{
    setContentView(R.layout.addattnd2);
    Attendance a = new Attendance();
    a.loadData(s); //this line calls the method containing FileInputStream
}
3
  • FileInputStream fIn = openFileInput("s["+i+"].txt"); This line cause the NPE. Is there any better method to save and load an array of objects? I'm using FileOutputStream and FileInputStream. Commented Mar 11, 2012 at 13:43
  • are you sure, that there is a file named "s[1].txt" and you're looking in the right folder? Commented Mar 11, 2012 at 13:52
  • Actually, I have no idea how to check if the file has been created or not. But, if the file hasn't been created, it would throw FileNotFoundException, not NPE, so I don't know why is it giving NPE. I have created the file in a different method, which I haven't shown here as the code would get too long. Commented Mar 11, 2012 at 13:54

1 Answer 1

1

I'll assume that you're getting the NullPointerException when you access your Subject array.

I'm not going to guarantee that it will work, but try this. In your onCreate(), instantiate the Subject objects after calling super.onCreate().

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    for(int i=0;i<13;i++) {
        s[i] = new Subject();
    }
    setContentView(R.layout.main);
}
Sign up to request clarification or add additional context in comments.

6 Comments

afaik you're right: super has to be the first thing in constructors.
I tried it, but it didn't work. The NPE is because of the FileInputStream, when the method a.loadData(s) is called. I had an NPE when there was no instantiation of the Subject array, so the Subject array is not throwing the NPE.
How are you calling this method? Is a an object of Attendance?
Yes, 'a' is an object of Attendance, I put that code just now in the main question. I had to do that since openFileInput() method would not work if I put it in a static method, hence I needed to create an object of the Attendance class. Otherwise there is no use for the object 'a'.
As far as I know, you cannot instantiate Activities like this. If you want to start an Activity, you use an intent. You'll have to change the way you are calling this method. If you don't want to start the Attendance Activity at all, you don't need Attendance to extend Activity. It all comes down to what Activities you have and how you plan to use them.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.