New Posts  All Forums:Forum Nav:

Java help please!

post #1 of 4
Thread Starter 
I'm trying to code a calculator in Eclipse.

I am getting an error on this line:
Code:
Calculator calc = new Calculator();
Here is my entire code
Code:
import javax.swing.*;
import java.awt.GridLayout;
import java.awt.BorderLayout;
public class Calculator {
// Declaration of all calculators components.
JPanel windowContent;
JTextField displayField;
JButton button0;
JButton button1;
JButton button2;
JButton button3;
JButton button4;
JButton button5;
JButton button6;
JButton button7;
JButton button8;
JButton button9;
JButton buttonPoint;
JButton buttonEqual;
JPanel p1;


Calculator(){
windowContent= new JPanel();

// Setting the layout manager
BorderLayout b1 = new BorderLayout();
windowContent.setLayout(b1);

//Create display field
displayField = new JTextField(30);
windowContent.add("North",displayField);

// Create buttons
button0=new JButton("0");
button0=new JButton("1");
button0=new JButton("2");
button0=new JButton("3");
button0=new JButton("4");
button0=new JButton("5");
button0=new JButton("6");
button0=new JButton("7");
button0=new JButton("8");
button0=new JButton("9");
buttonPoint = new JButton(".");
buttonEqual = new JButton("=");

// Create the panel
p1 = new JPanel();
GridLayout g1 =new GridLayout(4,3);
p1.setLayout(g1);

// Add window controls
p1.add(button1);
p1.add(button2);
p1.add(button3);
p1.add(button4);
p1.add(button5);
p1.add(button6);
p1.add(button7);
p1.add(button8);
p1.add(button9);
p1.add(button0);
p1.add(buttonPoint);
p1.add(buttonEqual);

// Add the panel p1 to the center area
windowContent.add("Center",p1);

// Create the frame
JFrame frame = new JFrame("Calculator");
frame.setContentPane(windowContent);

// Set the size of the window to hold all buttons
frame.pack();

// Diaplay the window
frame.setVisible(true);
}

public static void main(String[] args) {
Calculator calc = new Calculator();
}

}


Here are my error codes in the console
Code:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at Calculator.<init>(Calculator.java:54)
at Calculator.main(Calculator.java:82)

Can anyone help?
Thanks
Chris-PC
(19 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-920 Gigabyte GA-EX58-UD3R HIS R9 280X IceQ X2 Turbo OC 6GB DDR3 SuperTalent 
Hard DriveHard DriveHard DriveOptical Drive
Samsung 250GB EVO SSD (Internal) 2TB WD Black HDD (Internal) 4TB WD My Book HDD (External) LG 18X DVD-RW 
CoolingCoolingOSMonitor
Antec TruQuiet 140mm (X5) Antec TruQuiet 120mm (X2) Windows 10 Pro 64-bit 23" Samsung 2333HD 1080p 
KeyboardPowerCaseMouse
Saitek Eclipse II XFX Pro 650W Core Edition Corsair Carbide 300R Logitech MX518 
Mouse PadAudioAudio
SteelSeries QcK+ (XL) Epiphany Acoustics EHP-02 AMP + DAC Audio-Technica ATH-M50X 
  hide details  
Reply
Chris-PC
(19 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-920 Gigabyte GA-EX58-UD3R HIS R9 280X IceQ X2 Turbo OC 6GB DDR3 SuperTalent 
Hard DriveHard DriveHard DriveOptical Drive
Samsung 250GB EVO SSD (Internal) 2TB WD Black HDD (Internal) 4TB WD My Book HDD (External) LG 18X DVD-RW 
CoolingCoolingOSMonitor
Antec TruQuiet 140mm (X5) Antec TruQuiet 120mm (X2) Windows 10 Pro 64-bit 23" Samsung 2333HD 1080p 
KeyboardPowerCaseMouse
Saitek Eclipse II XFX Pro 650W Core Edition Corsair Carbide 300R Logitech MX518 
Mouse PadAudioAudio
SteelSeries QcK+ (XL) Epiphany Acoustics EHP-02 AMP + DAC Audio-Technica ATH-M50X 
  hide details  
Reply
post #2 of 4
I'm guessing this isn't what you intended to write

Code:

// Create buttons
button0=new JButton("0");
button0=new JButton("1");
button0=new JButton("2");
button0=new JButton("3");
button0=new JButton("4");
button0=new JButton("5");
button0=new JButton("6");
button0=new JButton("7");
button0=new JButton("8");
button0=new JButton("9");
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #3 of 4
Lol at mistake
WaterWorks
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core 2 Quad Q6700 (3.79GHz) ASUS Maximus Formula X38 PNY XLR8 GTX280 1024MB 6GB Dual Channel PC6400 5-4-4-12 (800MHz) 
Hard DriveOptical DriveOSMonitor
1.18TB RAID 0 + 500GB RAID 0 + 400GB Spare Lite-On Blu-Ray Drive Windows 7 Ultimate x64 I-INC 28" HDMI 1920x1200 
KeyboardPowerCaseMouse
Logitch G15 Corsair 900w Cooler Master Cosmos 1000 (Modded) Logitch G5 
Mouse Pad
Ergonomic Wrist Support 
  hide details  
Reply
WaterWorks
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core 2 Quad Q6700 (3.79GHz) ASUS Maximus Formula X38 PNY XLR8 GTX280 1024MB 6GB Dual Channel PC6400 5-4-4-12 (800MHz) 
Hard DriveOptical DriveOSMonitor
1.18TB RAID 0 + 500GB RAID 0 + 400GB Spare Lite-On Blu-Ray Drive Windows 7 Ultimate x64 I-INC 28" HDMI 1920x1200 
KeyboardPowerCaseMouse
Logitch G15 Corsair 900w Cooler Master Cosmos 1000 (Modded) Logitch G5 
Mouse Pad
Ergonomic Wrist Support 
  hide details  
Reply
post #4 of 4
Thread Starter 
Quote:
Originally Posted by rabidgnome229 View Post
I'm guessing this isn't what you intended to write

Code:

// Create buttons
button0=new JButton("0");
button0=new JButton("1");
button0=new JButton("2");
button0=new JButton("3");
button0=new JButton("4");
button0=new JButton("5");
button0=new JButton("6");
button0=new JButton("7");
button0=new JButton("8");
button0=new JButton("9");
Thanks man you are a legend!
Chris-PC
(19 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-920 Gigabyte GA-EX58-UD3R HIS R9 280X IceQ X2 Turbo OC 6GB DDR3 SuperTalent 
Hard DriveHard DriveHard DriveOptical Drive
Samsung 250GB EVO SSD (Internal) 2TB WD Black HDD (Internal) 4TB WD My Book HDD (External) LG 18X DVD-RW 
CoolingCoolingOSMonitor
Antec TruQuiet 140mm (X5) Antec TruQuiet 120mm (X2) Windows 10 Pro 64-bit 23" Samsung 2333HD 1080p 
KeyboardPowerCaseMouse
Saitek Eclipse II XFX Pro 650W Core Edition Corsair Carbide 300R Logitech MX518 
Mouse PadAudioAudio
SteelSeries QcK+ (XL) Epiphany Acoustics EHP-02 AMP + DAC Audio-Technica ATH-M50X 
  hide details  
Reply
Chris-PC
(19 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-920 Gigabyte GA-EX58-UD3R HIS R9 280X IceQ X2 Turbo OC 6GB DDR3 SuperTalent 
Hard DriveHard DriveHard DriveOptical Drive
Samsung 250GB EVO SSD (Internal) 2TB WD Black HDD (Internal) 4TB WD My Book HDD (External) LG 18X DVD-RW 
CoolingCoolingOSMonitor
Antec TruQuiet 140mm (X5) Antec TruQuiet 120mm (X2) Windows 10 Pro 64-bit 23" Samsung 2333HD 1080p 
KeyboardPowerCaseMouse
Saitek Eclipse II XFX Pro 650W Core Edition Corsair Carbide 300R Logitech MX518 
Mouse PadAudioAudio
SteelSeries QcK+ (XL) Epiphany Acoustics EHP-02 AMP + DAC Audio-Technica ATH-M50X 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming