2

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) {

    }
  }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    BTW - is `textField1 = new JTextField("", 15);` at line 58 of that source? – Andrew Thompson Aug 18 '14 at 01:53
  • Yes, it is at line 58. – Matthew Colescott Aug 18 '14 at 01:55
  • 1
    I'd have thought that it would be this line: `textField1.addKeyListener(lForKeys);` since here you call a method on textField1 **before** you've initialized it and while the variable is still null. Not good. Much more importantly, you need to learn the general concepts of how to debug a NPE (NullPointerException). **You should inspect the line carefully that throws the exception**, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Aug 18 '14 at 01:55
  • It's just that I see no way that that line of code could throw an NPE. It is probably the line identified by @HovercraftFullOfEels. Generally though, you need to figure out how to read a stack trace. It typically takes you directly to the code line that caused the problem. – Andrew Thompson Aug 18 '14 at 01:58
  • 2
    Oh, I see it now. My first code wanted to use the text field before it actually existed. I just moved it up to where I make the ListenForKeys lines and it works now! Thanks for the help! I will definitely inspect my code when I get my next NPE! – Matthew Colescott Aug 18 '14 at 02:03
  • 1
    See also [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556). – Andrew Thompson Aug 18 '14 at 02:03
  • And I almost forgot to add this tip: For Swing, we typically use [key bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) over the lower level, AWT based, `KeyListener`. – Andrew Thompson Aug 18 '14 at 02:04
  • *"Oh, I see it now."* Glad you got it sorted, and +1 for reporting back. :) – Andrew Thompson Aug 18 '14 at 02:06

0 Answers0