I am currently doing a tutorial on Java, working with swing. I am getting this error:
Exception in thread "main" java.lang.NullPointerException
at JavaLesson21.<init>(JavaLesson21.java:58)
at JavaLesson21.main(JavaLesson21.java:28)
It has to do with those 2 lines, that I know, but I do not know what the problem is. Any ideas?
And this is my code:
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.*;
public class JavaLesson21 extends JFrame {
JButton button1;
JTextField textField1;
JTextArea textArea1;
int buttonClicked;
public static void main(String[] args)
{
new JavaLesson21();
}
public JavaLesson21()
{
this.setSize(400, 400);
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = tk.getScreenSize();
int xPos = (dim.width / 2) - (this.getWidth() / 2);
int yPos = (dim.height / 2) - (this.getHeight() / 2);
this.setLocation(xPos, yPos);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("My 2nd JFrame.");
JPanel thePanel = new JPanel();
button1 = new JButton("Click here!");
ListenForButton lForButton = new ListenForButton();
button1.addActionListener(lForButton);
ListenForKeys lForKeys = new ListenForKeys();
textField1.addKeyListener(lForKeys);
thePanel.add(button1);
textField1 = new JTextField("", 15);
thePanel.add(textField1);
textArea1 = new JTextArea(15, 20);
textArea1.setText("Tracking events...\n");
textArea1.setLineWrap(true);
textArea1.setWrapStyleWord(true);
JScrollPane scrollbar1 = new JScrollPane(textArea1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
thePanel.add(scrollbar1);
this.add(thePanel);
ListenForWindow lForWindow = new ListenForWindow();
this.addWindowListener(lForWindow);
ListenForMouse lForMouse = new ListenForMouse();
thePanel.addMouseListener(lForMouse);
this.setVisible(true);
}
private class ListenForButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == button1)
{
buttonClicked++;
textArea1.append("Button clicked " + buttonClicked + " times\n");
}
}
}
private class ListenForKeys implements KeyListener
{
public void keyPressed(KeyEvent e)
{
textArea1.append("Key hit: " + e.getKeyChar() + "\n");
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
}
private class ListenForWindow implements WindowListener
{
public void windowActivated(WindowEvent e) {
textArea1.append("Window is active.");
}
public void windowClosed(WindowEvent arg0) {
}
public void windowClosing(WindowEvent arg0) {
}
public void windowDeactivated(WindowEvent e) {
textArea1.append("Window is not active.");
}
public void windowDeiconified(WindowEvent arg0) {
textArea1.append("Window in Normal State.");
}
public void windowIconified(WindowEvent arg0) {
textArea1.append("Window in Minimized State.");
}
public void windowOpened(WindowEvent arg0) {
}
}
private class ListenForMouse implements MouseListener {
public void mouseClicked(MouseEvent e) {
textArea1.append("Mouse Panel pos: " + e.getX() + " " + e.getY() + "\n");
textArea1.append("Mouse Screen pos: " + e.getXOnScreen() + " " + e.getYOnScreen() + "\n");
textArea1.append("Mouse Button: " + e.getButton() + "\n");
textArea1.append("Mouse Clicks: " + e.getClickCount() + "\n");
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
}
}