简体   繁体   中英

Java - How to store a variable via main method in a different class?

I'm supposed to store variables of an object based on user input inside a main method, to a different class. I'm getting static to non-static reference errors for "gerbil.id" and "gerbil.name"; I don't even know if it would save to the variables in the object in the gerbil class. Any ideas? Here's what I have so far:

EDIT: Now I get a nullpointerexceptor when I run with the fixes posted; here is the updated code. the exception points to the gerbilArray[i].id = keyboard.next() line specifically. I'm not sure what could've caused it. I've listed id as a string and also input a string (one word) when run.

Inside of my information class:

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("How many gerbils in the lab?");
    int numberOfGerbils = keyboard.nextInt();
    Gerbil[] gerbilArray = new Gerbil[numberOfGerbils];

    for(int i = 0; i < numberOfGerbils; i++){

        System.out.print("Lab ID:");
        gerbilArray[i].id = keyboard.next();

}

Inside of the gerbil class:

import java.util.Scanner;
public class Gerbil {

public String id;


public Gerbil(String name) {
    this.id = "";
  }
}
public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("How many gerbils in the lab?");
    int numberOfGerbils = keyboard.nextInt();
    Gerbil[] GerbilArray = new Gerbil[numberOfGerbils]; // initialization of array

    for(int i = 0; i < numberOfGerbils; i++){
        GerbilArray[i] = new Gerbil();
        System.out.print("Lab ID:");
        // String Gerbil.id = keyboard.next(); should be replaced by
        GerbilArray[i].id=keyboard.next(); // because you should call non static members using objects of a class. Here object is an array element which requires an array index.

        System.out.print("Gerbil Nickname:");
        //String name = keyboard.next(); replace by
        GerbilArray[i].name=keyboard.next();
        // as stated earlier. and you don't need to write String again because you have already declared it in the class declaration. If you try to do this way, you are creating a new String object only. This name has already been assigned to array, so it will result in an error.
    }
}

in the Gerbil class, contructor should be corrected as

public Gerbil(String name, String id) {
    this.name = name;
    this.id = id;
}

because local members hides class members. here name and id of constructor will hide name and id of class. the above statements does nothing. it is an assignment made to itself only. this keyword is used to point to invoking object.

and since you are providing a parameterized constructor, but you are creating objects using default constructor, you need to explicitely create a default constructor as well.

Gerbil(){}

It should be

GerbilArray[i].id = keyboard.next();

instead of

Gerbil.id = keyboard.next();

It would help to avoid this kind of confusion if you followed the convention of starting variable names with lowercase letters. So then it would be:

gerbilArray[i].id = keyboard.next();

You are getting a NullPointerException as there is a problem in allocating the space for the array. For example: numberofGerbils=5 then gerbilArray[0],gerbilArray[1],gerbilArray[2],gerbilArray[3],gerbilArray[4] are created .

But as you are trying to access the gerbilArray[5], it is showing NullPointerException. You have used "i<=numberofGerbils" . it should not be "<=" . It should be "<" only. This will solve the problem.As when you are using "=", the array index 5 is called, which actually does not exists!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM