2016-03-23 5 views
-3

Ich möchte auf mein Array von Objekten von einer anderen Methode in meiner Klasse zugreifen. Mein Code ist wie folgt. Ich erhalte einen NullPointerException-Fehler.NullPointer beim Versuch, auf ein Array von Objekten in einer anderen Methode zuzugreifen

Die nameArray liest einfach eine Textdatei, und Daten werden darin gespeichert.

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()); 
     } 
    } 

} 

Die (wichtige Teile) Student Klasse:

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

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

    public String getName(){ 
     return name; 
    } 
} 

Die Read-Klasse:

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(); 
     } 
    } 
} 
+0

Welche Linie Haben Sie eine Nullpointer bekommen auf? Initialisieren Sie Ihr gesamtes Array, bevor Sie es verwenden? –

+0

Was soll 'askInfo' sein? – khelwood

+0

askInfo ist ein Scanner. Ill einfach setzen Sie es in –

Antwort

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()); 
    } 

} 
Verwandte Themen