Reputation: 13
So this works now, and i fixed the variable calling errors. but I get :
Exception in thread "main" java.lang.NullPointerException
at Radio.buildPanel(Radio.java:56)
at Radio.<init>(Radio.java:33)
at Radio.main(Radio.java:74)
My GUI Pops up but is blank, what is it now? I cannot figure out what the problem is at this time.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Radio extends JFrame
{
private JPanel Panel;
private JPanel buttonPanel;
private JTextField base;
private JTextField width;
private JRadioButton squareArea;
private JRadioButton parallelogramArea;
private JLabel messageLabel;
private JTextField text;
private final int WINDOW_WIDTH = 550;
private final int WINDOW_HEIGHT = 550;
private ButtonGroup radioButtonGroup;
private JRadioButton radioButton1;
private JRadioButton radioButton2;
double pTotal;
double sTotal;
public Radio()
{
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setTitle("Area Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
buildPanel();
add(Panel);
}
public void init()
{
setLayout(new BorderLayout());
add(buttonPanel, BorderLayout.SOUTH);
}
private void buildPanel()
{
JLabel messageLabel1 = new JLabel("Please enter the base: ");
JTextField base = new JTextField(10);
JLabel messageLabel2 = new JLabel("Please enter the width: ");
JTextField width = new JTextField(10);
JRadioButton squareArea = new JRadioButton("Choice 1", true);
JRadioButton parallelogramArea = new JRadioButton("Choice 2");
ButtonGroup group = new ButtonGroup();
JButton calcButton = new JButton("Calculate");
calcButton.setBackground(Color.BLUE);
calcButton.setForeground(Color.PINK);
calcButton.addActionListener(new CalcButtonListener());
Panel.add(messageLabel1);
Panel.add(base);
Panel.add(messageLabel2);
Panel.add(width);
group.add(squareArea);
group.add(parallelogramArea);
buttonPanel.add(squareArea);
buttonPanel.add(parallelogramArea);
Panel.add(calcButton);
Panel.add(buttonPanel);
}
public static void main (String[] args)
{
Radio radio = new Radio();
}
private class CalcButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if (parallelogramArea.isSelected());
{
pTotal = Double.parseDouble(base.getText()) * Double.parseDouble(width.getText());
JOptionPane.showMessageDialog(null, "The Area is: " + pTotal);
}
if (squareArea.isSelected())
{
sTotal = Double.parseDouble(base.getText()) * Double.parseDouble(width.getText());
JOptionPane.showMessageDialog(null, "The Area is: " + sTotal);
}
}
}
}
This is very important to get resolved, thank you in advance.
Upvotes: 1
Views: 551
Reputation: 48592
You are calling this buildPanel();
method.
Its wrong because this method accept two parameter as arguments.
So call this method like as `buildPanel(str1,str2);
private void buildPanel(String width1, String base1){
.............
}
Declare all variable which you not declare like.
String base1 = null;
Upvotes: 1
Reputation: 9040
the first error means when you call buildPanel(String, String)
you need to use 2 string parameters to pass to the function. At the moment you are not passing the parameters. Second error means the variables you attempt to use are undefined, because of scope issues meaning they are not defined when you try to use them inside a method.
Upvotes: 1
Reputation: 117589
You have issues with scopes. For example, you are defining parallelogramArea
within buildPanel()
method's scope, and you are trying to access it from inside another scope (The scope of the inner class CalcButtonListener
).
Also, you are calling buildPanel()
without the two String arguments.
Upvotes: 1
Reputation: 691655
In Eclipse, go to the Window - Show view menu, and choose to open the view named "Problems". This view should be always open. It contains all the compilation errors and warnings. While there are compilation errors (marked red), you shouldn't even try to run your program.
It seems try to use undefined variables, and that you're calling a method buildPanel(String, String)
taking two Strings as argument without passing any. Double-click on a compilation error in the problems view, and eclipse will put the cursor in the line where the problem is.
How comes you're using Swing before having learned how to compile a program and solve such basic errors? That's like trying to drive a Ferrari before learning to drive. Start with the basics.
Upvotes: 1