0

I have been stuck on this for a while now, if someone could show me some insight it would be much appreciated. The stack trace console points to the frame.add(showname); as well as to new GradeProg(); The console states:

Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1040)
at java.awt.Container.add(Container.java:958)
at javax.swing.JFrame.addImpl(JFrame.java:540)
at java.awt.Container.add(Container.java:363)
at GradeProg.<init>(GradeProg.java:39)
at GradeProg.main(GradeProg.java:68)

From what I understand, due in part to this great site, was that the showname variable was not initialized, though I believe I already initialized it. Here is the code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class GradeProg extends JFrame implements ActionListener {

JLabel label, label2, showname, showgrade;
TextField field, field2;
JFrame frame;
JButton enter;
String grade, name;
int parse;

public GradeProg() {

    frame = new JFrame("Student Grades");
    frame.setLayout(new GridBagLayout());
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
    frame.setSize(600, 300);

    label = new JLabel("Student Name: ");
    field = new TextField("", 12);

    label2 = new JLabel("  Score  ");
    field2 = new TextField("", 5);

    enter = new JButton("Enter");
    enter.addActionListener(this);

    frame.add(label);
    frame.add(field);
    frame.add(label2);
    frame.add(field2);
    frame.add(enter);
    frame.add(showname);
    frame.add(showgrade);

}

@Override
public void actionPerformed(ActionEvent e) {

    parse = Integer.parseInt(field2.getText());

    if (parse >= 90 && parse <= 100) {
        grade = "A";
    } else if (parse >= 80 && parse <= 89) {
        grade = "B";
    } else if (parse >= 70 && parse <= 79) {
        grade = "C";
    } else if (parse >= 60 && parse <= 69) {
        grade = "D";
    } else if (parse >= 0 && parse <= 59) {
        grade = "F";
    }

    showgrade = new JLabel("Grade: " + grade);
    showname = new JLabel(field.getText());
}

public static void main(String[] args)

{
    new GradeProg();
}

}

Again, any help or overall suggestions would be much appreciated. Thakns

  • I see you initialize `label`, `field`, `label2`, `field2`, and `enter`, but where are the others? – Sotirios Delimanolis Nov 04 '14 at 01:32
  • 2
    `showname` and `showgrade` are `NULL` before adding them to `frame`. You are initializing them in `actionPerformed` but rather they need to initialize in `GradeProg()` constructor and most probably causing `NPE` – Smit Nov 04 '14 at 01:35
  • I agree with @Smit, initialize the `JLabels` in the constructor, and use the `setText(String)` method to change the text of the `JLabels`in the action listener – czifro Nov 04 '14 at 01:40
  • Also, `GradeProg` extends `JFrame`. So when you say `new GradeProg();`, that creates a new `JFrame` object. Then you create another `JFrame` object in the constructor. `GradeProg` inherits all the methods of `JFrame`, so just call them. I edited your code and posted it here: [http://ideone.com/rTpjcq](http://ideone.com/rTpjcq) – czifro Nov 04 '14 at 01:54

0 Answers0