-3

I would like to access my array of objects from another method in my class. My code is as follows. I get a NullPointerException error.

The nameArray is simply reading a text file, and data is being stored in it.

public class menu {

    static Scanner askInfo = new Scanner(System.in);
    public static Student ArrObj[] = new Student[15]; //instantiates an array of student objects 

    public static void main(String args[]) throws IOException {
        for (int i = 0; i < 15; i++) {
            ArrObj[i] = new Student(); //creates an array of student objects
            System.out.println("Here is the list of all children.");
            System.out.println("Which child's information do you want to enter?");
            int InName = askInfo.nextInt();
            switch (InName) {
                case 1:
                    ArrObj[i].setName(Read.nameArray[0].substring(3));
                    break;
                case 2:
                    ArrObj[i].setName(Read.nameArray[1].substring(3));
                    break;
                case 3:
                    ArrObj[i].setName(Read.nameArray[2].substring(3));
                    break;
                case 4:
                    ArrObj[i].setName(Read.nameArray[3].substring(3));
                    break;
            }
        }
        for (int i = 0; i < 15; i++) {
            System.out.println(ArrObj[i].getName());
        }
    }

}

The (important parts of) Student class:

public class Student {
    private String name;      //name of student    

    public void setName(String name) {
            this.name = name;
    }

    public String getName(){
        return name;
    }
}

The Read class:

import java.io.*;
import java.util.*;

public class Read {

    static String nameArray[] = new String[100]; //to be safe, declare plenty

    public static void ReadNames() throws IOException //method to read names
    {
        Scanner read = new Scanner(new File("C:\\IA\\names.txt")); //this will read the names of students
        int counter = -1; //-1 so that when incremented, the first index is 0 
        while (read.hasNext()) {
            counter++;
            nameArray[counter] = read.nextLine();
        }
    }
}
ctst
  • 1,610
  • 1
  • 13
  • 28
  • Which line do you get a nullpointer on? Do you initialize your entire array before using it? – Matthew Wright Mar 23 '16 at 16:14
  • What is `askInfo` supposed to be? – khelwood Mar 23 '16 at 16:15
  • askInfo is a scanner. Ill just put it in – Raghav Kapur Mar 23 '16 at 16:15
  • I get it when I print the array – Raghav Kapur Mar 23 '16 at 16:16
  • @khelwood I went through that. I was unable to figure out what's wrong – Raghav Kapur Mar 23 '16 at 16:16
  • @ctst I have removed the write method and simply copy-pasted the loop into the main method. I get the same error – Raghav Kapur Mar 23 '16 at 16:36
  • @RaghavKapur Could you please clean up the code above, right now it is a mess to read. Also how and where the write-method is called would be interesting. Furthermore does `getName()` return a String or an Object? – ctst Mar 23 '16 at 16:39
  • I think you have a scope issue on ArrObj. can you paste the clean code? – ingrid.e Mar 23 '16 at 16:41
  • @ctst getName returns a string. With respect to the scope issue, I've updated the code. – Raghav Kapur Mar 23 '16 at 16:44
  • Still one bracket before the second for-loop is missing. Also proper code indention would be nice. And the NullPointer is in the line `System.out.println(ArrObj[i].getName());`, right? – ctst Mar 23 '16 at 16:45
  • The name variable is private in another class. I have used getters and setters for it – Raghav Kapur Mar 23 '16 at 16:46
  • Yes, the NullPointer is in that line – Raghav Kapur Mar 23 '16 at 16:46
  • Does this code still throw an NullPointerException? If so, could you please post the setters and getters for `Student.name` – ctst Mar 23 '16 at 16:56
  • Yes, it does. Done, updated in the OP – Raghav Kapur Mar 23 '16 at 17:05
  • @ctst Weirdly enough, when I print the getter in the switch statement, it gives me the right option. However, when I loop it the way I did in the code above, I get a null pointer – Raghav Kapur Mar 23 '16 at 17:10
  • My humble guess is twofold: First (which shouldn't throw an NullPointer, but this might be depending on the version): you don't have a default if InName doesn't fit your cases (you should do something here, else you might have `Student.name=null` . Second I strongly assume your NullPointer is not in the write-method, but in `Read.nameArray=null` or `Read=null`. You should check that (run this code snipplet and look at the line, where the error occurs). – ctst Mar 23 '16 at 17:11
  • Can we see the Read class?? – jonhid Mar 23 '16 at 17:33
  • Well, there is so much failure-potential in here. First, if you don't know the size of the array, use an `ArrayList` instead, second: your names might be `null` (right now), hence you will get a NullPointer when you try to substring them. They might be also smaller than 3! Also a default is missing (you should change your switch-case to something like `if(InName – ctst Mar 23 '16 at 17:47
  • @ctst I called my Read.ReadNames() before the initial for loop begins. No difference. And I'm sampling some data for now, and the strings are larger than 3 – Raghav Kapur Mar 23 '16 at 18:00
  • I can confirm that the issue is not with the reading from the text file, because I changed it to Scanner input and the error persisted – Raghav Kapur Mar 23 '16 at 18:03
  • Just to clarify: You have now something like `for(int i; ...){ ArrObj[i] = new Student(); ArrObj[i].setName(sth definitely not null); System.out.println(ArrObj[i].getName()); //throws NullPointer }` – ctst Mar 23 '16 at 18:06
  • That it!!! the Read.ReadNames(); – jonhid Mar 23 '16 at 18:27

1 Answers1

0
    public static void main(String args[]) throws IOException {

     Read.ReadNames();

    for (int i = 0; i < 3; i++) {
        ArrObj[i] = new Student(); // creates an array of student objects
        System.out.println("Here is the list of all children.");
        System.out.println("Which child's information do you want to enter?");
        int InName = askInfo.nextInt();
        switch (InName) {
        case 1:
            ArrObj[i].setName(Read.nameArray[0]);
            break;
        case 2:
            ArrObj[i].setName(Read.nameArray[1]);
            break;
        case 3:
            ArrObj[i].setName(Read.nameArray[2]);
            break;
        case 4:
            ArrObj[i].setName(Read.nameArray[3]);
            break;
        }

    }

    for (int i = 0; i < 3; i++) {
        System.out.println(ArrObj[i].getName());
    }

}
jonhid
  • 2,075
  • 1
  • 11
  • 18