开发者

Java - NullPointerException

开发者 https://www.devze.com 2023-01-12 17:54 出处:网络
I\'m trying to make a toggle button (F) to hide or show my chat area (JTextArea) which is on a JScrollPane (sp) and it\'s not working...

I'm trying to make a toggle button (F) to hide or show my chat area (JTextArea) which is on a JScrollPane (sp) and it's not working...

Here is what I do...

I have it set as public...

public JScrollPane sp;

and I set it inside the init() like this: JScrollPane sp = new JScrollPane(c);

c is the JTextArea....

 if (e.getKeyCode() == KeyEvent.VK_F)
{
    if (sp.isVisible())
    {
        sp.setVisible(false);
    }
    else
    {
        sp.setVisible(true);
    }
}

This is my error:

Exception in thread "AWT-EventQueue-2" java.lang.NullPointerException
    at tileGen.keyPressed(tileGen.java:522)
    at java.awt.Component.processKeyEvent(Unknown Source)
    at javax.swing.JComponent.processKeyEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt开发者_运维百科.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

This is line 522: if (sp.isVisible()) {

What am I doing wrong?


If you intend to set the instance variable, get rid of the JScrollPane sp in init() (the line should just be sp = new JScrollPane(c);). With it in, you're declaring and setting a local variable, and the object's sp never gets set.


--- Edited in response to having line 522 ---

Because you have JScrollPane sp = new JScrollPane(...); you are hiding the member variable sp with a variable having the same name. This means that once you leave the init() block the locally defined sp variable will disappear and the JScrollPane will not have been stored in the member variable sp.

Which leads to the following ....

You thought that the member variable sp was assigned to an object, but it is still null. You need to make sure that the member variable sp is not null before you can call methods on it.

--- Original post follows ---

Look at line 522 of tileGen.java. Odds are you are calling a method on an object there, like so

someObject.setValue(someValue);

the problem is that someObject isn't set to an object, it is still null.


Why there are two declarations of sp ?

public JScrollPane sp;

JScrollPane sp = new JScrollPane(c);

0

精彩评论

暂无评论...
验证码 换一张
取 消