1

I'm trying to make a program that takes input from the user for how many students they have in their class and their names/grades and calculate average and letter grades. I'm trying to get better with For Loops and Arrays and this is what I chose to do. The only problem is when the init() method is called to make the window. The class is extending JFrame

import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class GradesGUI_2 extends JFrame {

static Scanner s = new Scanner(System.in);
static int num;

public static void main(String[] args) {

    System.out.println("How many students are in your class?");
    num = s.nextInt();

    GradesGUI_2 window = new GradesGUI_2("Title");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

//-------------------------------------------------

static JPanel p;
JTextField[] name = new JTextField[num], grade = new JTextField[num];
JLabel[] nlabel = new JLabel[num], glabel = new JLabel[num];
int height = 50;

//-------------------------------------------------

GradesGUI_2(String title) {
    super(title);
    this.init();
    this.setVisible(true);
    this.setLocationRelativeTo(null);
}

//-------------------------------------------------

void init() {
    //Create JLabel/JTextField
    for(int i = 0; i < num; i++) {
        nlabel[i] = new JLabel("Name " + i);
        glabel[i] = new JLabel("Grade " + i);
        name[i] = new JTextField(10);
        grade[i] = new JTextField(1);

        height += 25;
    }

    for(int i = 0; i < num; i++) {
        p.add(nlabel[i]);
        p.add(glabel[i]);
        p.add(name[i]);
        p.add(grade[i]);
    }



    this.setSize(350, height);
    this.add(p);
}

}

What the console displays:

How many students are in your class?
7
Exception in thread "main" java.lang.NullPointerException
    at GradesGUI_2.init(GradesGUI_2.java:59)
    at GradesGUI_2.<init>(GradesGUI_2.java:40)
    at GradesGUI_2.main(GradesGUI_2.java:20)

The error happens when the components are added to the panel. Please answer. Thank you!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Reddish
  • 11
  • 4

1 Answers1

2

The exception stack trace points to this line:

        p.add(nlabel[i]);

So either nlabel[i] is null, or p is null. But nlabel[i] was initialised to a non null value in the previous loop, pointing to p as the culprit. One way to fix it is to change:

static JPanel p;

To:

JPanel p = new JPanel(); // with default FlowLayout

Tips

  1. Don't declare GUI components as static. Using the static modifier is rarely a good idea, and I've never seen a case where it is a good idea to use it for components - ever.
  2. You'll need to figure out how to understand and solve exceptions from the stack trace. As a programmer, we see many of them, in our code, or the code we are meant to maintain. To reiterate something I wrote above in comments: See What is a stack trace, and how can I use it to debug my application errors? & What is a Null Pointer Exception, and how do I fix it?
  3. Don't mix command line input with GUIs. In this case, one approach is to use a JOptionPane.showInputDialog(..); to query the number of students.
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433