Hello guys,
I am pretty new at (good) programming, so I had some pretty bad code I was using until I
ran across some code and decided to model my code after it.

I have the code so it has no errors, but when I run it (NetBeans) it doesn't show up, and
gives me this very long error:

run:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at java.awt.Container.addImpl(Container.java:1045)
        at java.awt.Container.add(Container.java:927)
        at jPad.jpad.<init>(jpad.java:18)
        at jPad.jpad.createAndShowGUI(jpad.java:29)
        at jPad.jpad.access$000(jpad.java:12)
        at jPad.jpad$1.run(jpad.java:41)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
        at java.awt.EventQueue.access$000(EventQueue.java:84)
        at java.awt.EventQueue$1.run(EventQueue.java:602)
        at java.awt.EventQueue$1.run(EventQueue.java:600)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
BUILD SUCCESSFUL (total time: 1 second)

Here is the file I am trying to run:

// Re-writing the start.java file
package jPad;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author JimmyD
 */

public class jpad extends JPanel implements ActionListener {
    JTextArea mainTextArea;

    public jpad() {
        super(new BorderLayout());

        add(mainTextArea, BorderLayout.CENTER);
    }

    public void actionPerformed(ActionEvent e) {
        mainTextArea.append(" Once! ");
    }

    private static void createAndShowGUI() {
        JFrame mainWin = new JFrame("JPad v0.1");
        mainWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JComponent newContentPane = new jpad();
        newContentPane.setOpaque(true);
        mainWin.setContentPane(newContentPane);

        mainWin.setSize(400, 600);
        mainWin.setLocation(100, 100);
        mainWin.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

Where is the error coming from? And it could be a simple thing I messed up, since I
don't completely understand how all of the code in this file works.

- WolfShield

Recommended Answers

All 4 Replies

your maintextarea variable does not hold anything. just assign it something, I guess new JTextArea();

Eiting: it's null. that's what your error message is telling you.

null pointer exception means you have tried to use a variable that has not been initialised. Reading down the messages we see it happened in the add method for Container, and that was called from line 18 of jpad.java
That line reads
add(mainTextArea, BorderLayout.CENTER);
and there's the problem. You have declared the variable mainTextArea but that variable has never been initialised.

at java.awt.Container.add(Container.java:927)
at jPad.jpad.<init>(jpad.java:18)

You'll need to know how to read the error message. Here are two lines with some info.
Read from the bottom up until you find your java class. (jPad)
That line has the source line where the error occured: 18 and shows what method you were in: <init> is the constructor
The next line up shows the method and class that was being called from the code on line 18: the Container's add method.

Wow,
Thanks guys! I can't believe I missed something as huge as that!

Anyway, the line

mainTextArea = new JTextArea();

solves the error. Thanks!

- WolfShield

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.