2

I need help with calling methods from definition class using an array in main method.

public class Student{
    private String id;

    public void setId(String id){
        this.id=id;
    }
    public String getId(){
        return id;
    }

}

and in main, with an array of students like this,

public static void main(String[] args){
    int numOfStudent=0;
    Student[] students = new Student[numOfStudent];

    students[numOfStudent].setId(JOptionPane.showInputDialog("Enter id:"));
    numOfStudent++;

}

I keep getting an error message saying

"java:6: error: cannot find symbol

students[numOfStudent].setId(JOptionPane.showInputDialog("Enter id:"));

symbol: variable JOptionPane location: class Whatever

1 error "

what is the problem here??

3
  • 4
    Did you import JOptionPane? It does not seem to be able to resolve that. Commented Sep 1, 2014 at 4:47
  • 2
    Also, note that the length of the array is 0. So, there won't be any element inside. Commented Sep 1, 2014 at 4:48
  • 1
    Are you sure this is the full error message? Please copy and paste the entire output from your console. It would help if you also include the exact command you are using to compile your code. Commented Sep 1, 2014 at 4:53

3 Answers 3

3

The compiler thinks that JoptionPane is a variable (which you did not declare).

You need to import this class at the top of your file.

Also, you will get an ArrayIndexOutOfBoundsException, because your array has length 0, so there is no element (at index 0 or any other index).

Sign up to request clarification or add additional context in comments.

Comments

0

That's because you're initializing your array with 0

Student[] students = new Student[numOfStudent];

Where numOfStudent is zero. So when you're trying to run this line

students[numOfStudent].setId(JOptionPane.showInputDialog("Enter id:"));

The compiler complains because no student is in index 0. In fact. your Students array is empty.

8 Comments

so should I add 1 to numOfStudent first and call a method?
@user3373529, you can initialize your students array to 100 for instance. May I know what are you trying to achieve first?
This is certainly a logic error, but is not the cause of the compiler error in the OP.
The above answer will result in a NullPointerException, NOT the error that the OP has posted. It would be great if The compiler complains because no student is in index 0. but alas this is not the case.
+1 because this will likely prevent a "So I'm getting a NullPointerException..." question in about 10 minutes.
|
0

To solve your immediate problem, you need to add an import statement at the top of Whatever.java:

import javax.swing.JOptionPane;

However, you have several other problems in your code:

  1. You allocate an array with 0 elements. This means that when you try to access an element in the array, you will get an ArrayIndexOutOfBoundsException. To fix this, change

    int numOfStudent=0;
    

    to

    int numOfStudent=100; // or some other number, depending on how many students you want to keep track of
    
  2. After fixing #1, you have only created an array with empty slots. You have not yet created any Student objects. I suggest that you read about creating objects. To get you started, you can do something like this to create a single Student as the first element of the array:

    students[0] = new Student();
    

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.