ilugd
Beware of the innocent
SOLVED
I am having trouble with the following code.
I get this error from the compiler
The part of the code that is the problem is this.
what i am trying is to create an array of 16 buttons and then go into a loop and assign strings as button labels to each of the individual buttons. I am just learning Java, so not sure what is wrong here. can someone please help?
I am having trouble with the following code.
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package calcapp;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.lang.String;
/**
*
* @author jsemmanuel
*/
public class calculator extends JFrame {
calculator() {
super("This is a frame");
setSize(200, 200);
JPanel valuePanel = new JPanel();
JPanel buttonPanel = new JPanel();
JLabel valueLabel = new JLabel("0");
//valueLabel.setSize(40, 1800); ; //this one doesn't seems to work
valueLabel.setBorder(new EtchedBorder());
valuePanel.add(valueLabel);
String buttonText[] = {"+", "-", "*", "/", "7", "8", "9", "4", "5", "6", "1", "2", "3", "0", ".", "="};
JButton[] b=new JButton[16];
for (int i = 0; i < 16; i++) {
b[i].setText(buttonText[i]);
buttonPanel.add(b[i]);
}
Container container;
container = getContentPane();
container.setLayout(new GridLayout(2, 1));
add("North", valuePanel);
add("South", buttonPanel);
addWindowListener(new WindowHandler());
show();
}
}
class WindowHandler extends WindowAdapter implements ActionListener {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void actionPerformed(ActionEvent arg0) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
I get this error from the compiler
Code:
compile:
run:
Exception in thread "main" java.lang.NullPointerException
at calcapp.calculator.<init>(calculator.java:32)
at calcapp.Main.main(Main.java:13)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
Code:
JButton[] b=new JButton[16];
for (int i = 0; i < 16; i++) {
b[i].setText(buttonText[i]);
buttonPanel.add(b[i]);
}
Last edited: