0

I have a class called AgendaFunctions, a class called Main, and a class called ReadFiles. Main has a reference variable for Agenda Functions and ReadFiles. AgendaFunctions has an array of reference variables. I have code to instantiate the array, but i need to instantiate it from ReadFiles. If I instantiate it from main, it works fine. But if i call the method from ReadFiles, it doesn't work. I get a java.lang.NullPointerException error. Here is the code for Main:

    public class Main {
        public static void main(String[] args) throws Exception {
            ReadFiles fw = new ReadFiles(); fw.read();
            agendafunctions link = new agendafunctions();

AgendaFunctions:

    public class agendafunctions {
        int amount = 20;
        public void setamount(int data) {

        }
        static String input = "true";
        agendaitem item[] = new agendaitem[amount];
        int counter = 0;
        public void instantiate() {
                item[1] = new agendaitem();
                item[2] = new agendaitem();
                item[3] = new agendaitem();
        }
        public void createobject(String name, Boolean complete, String Comments) {
            item[counter].name = name;
            item[counter].complete = complete;
            item[counter].comments = Comments;
            counter++;
        }

ReadFiles:

    public class ReadFiles {
        public void read() throws IOException {
            agendafunctions af = new agendafunctions(); af.instantiate();
            int readitem = 1;
            BufferedReader data = new BufferedReader(new FileReader("C:/Agenda Dev Docs/data.txt"));
            int filestoread = Integer.parseInt(data.readLine());
            while (readitem <= filestoread) {
                String name;
                String complete;
                String comments = null;
                String line;
                Boolean bc = null;
                BufferedReader read = new BufferedReader(new FileReader("C:/Agenda Dev Docs/"+readitem+".txt"));
                readitem++;
                name = read.readLine();
                complete = read.readLine();
                comments = "";
                while((line = read.readLine()) != null) {
                    comments = comments + line;
                }
                if(complete.equals("Complete")) {
                    bc = true;
                } else if(complete.equals("Incomplete")) {
                    bc = false;
                }
                af.createobject(name, bc, comments);
            }
        }

If I call the method instantiate from ReadFiles, i get a NullPointerException. If I call it from Main, everything works. But further development requires me to call the method from ReadFiles. How would i fix this? Thanks.

4
  • 7
    Show us your code. Commented Sep 7, 2013 at 14:59
  • 1
    you should post your code and stacktrace Commented Sep 7, 2013 at 15:00
  • programmers usually have an easier time understanding the problem when they look at the code, not when they read a paragraph about what the code looks like. Commented Sep 7, 2013 at 15:00
  • 1
    What line of code is the error on? Commented Sep 7, 2013 at 15:13

2 Answers 2

2

You have this

    int counter = 0;
    public void instantiate() {
            item[1] = new agendaitem();
            item[2] = new agendaitem();
            item[3] = new agendaitem();
    }
    public void createobject(String name, Boolean complete, String Comments) {
        item[counter].name = name;
        item[counter].complete = complete;
        item[counter].comments = Comments;
        counter++;
    }

where item is an array with 20 indices, ie 20 elements, but your instantiate method only initializes elements at indices 1 through 3, missing 0 and 4 through 19.

In your ReadFiles#read() method, you do

  agendafunctions af = new agendafunctions(); af.instantiate();

which instantiates one agendafunctions object and calls instantiate() which initializes elements at indices 1, 2, and 3 in your item array.

You then loop in the while and call

  af.createobject(name, bc, comments);

a bunch of times on the same object.

The reason it fails the first time is because you haven't initialized the element in item at index 0. Arrays always start at 0, not 1.

Another cause for error (which you'll see if you fix the above problem), is that if your while loop loops more than 3 times, you'll again get a bunch of NullPointerExceptions because counter keeps growing, but you aren't initializing the elements that you'll then try to access at counter index.

item[counter].name = name; // if counter is 4, you'll get NullPointerException because 
                           // the element there hasn't been initialized as 'new agendaitem();'
Sign up to request clarification or add additional context in comments.

Comments

0

@SotiriosDelimanolis explained why you are getting NPE, to fix that you just can get rid of the instantiate() method and add item[counter] = new agendaitem(); as first line in your createobject method. Also you have to make sure your while loop does not exceed amount. To avoid these worries better use an ArrayList for agendaitem

Comments

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.