【问题标题】:I get NullPointerException using CardLayout我使用 CardLayout 得到 NullPointerException
【发布时间】:2012-04-14 13:25:23
【问题描述】:

大家好,我正在尝试制作一个 GUI,当按下 Next 或 Previous 按钮时,它可以在 JTextAreas 中导航。我正在使用 CardLayout 在 JTextArea 之间切换。我可以编译程序,但每次按下下一个或上一个按钮时,我总是会收到 NullPointerException。我不知道为什么 CardLayout 对象在 next 和 previous 方法中为空,这让我很困惑。

错误来自这段代码

cl.show(cardPanel, "" + (currentCard));

感谢大家的帮助!

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


public class AsciiCanvas extends JTextArea
{

private JPanel jp1;
private JPanel jp2;
private JPanel jp3;
private JPanel jp4;
private JPanel jp5;
private JPanel jp6;
private JPanel jp7;
private JPanel jp8;
private JPanel jp9;
private JPanel jp10;
private JTextArea text1;
private JTextArea text2;
private JTextArea text3;
private JTextArea text4;
private JTextArea text5;
private JTextArea text6;
private JTextArea text7;
private JTextArea text8;
private JTextArea text9;
private JTextArea text10;

private JPanel cardPanel;
private CardLayout cl;
private int currentCard = 1;

private JFileChooser fc;


public AsciiCanvas()
{
   Font mono = new Font("Monospaced", Font.PLAIN, 12);

    cardPanel = new JPanel();
    CardLayout cl = new CardLayout();
    cardPanel.setLayout(cl);


    JTextArea text1 = new JTextArea();
    JTextArea text2 = new JTextArea();
    JTextArea text3 = new JTextArea();
    JTextArea text4 = new JTextArea();
    JTextArea text5 = new JTextArea();
    JTextArea text6 = new JTextArea();
    JTextArea text7 = new JTextArea();
    JTextArea text8 = new JTextArea();
    JTextArea text9 = new JTextArea();
    JTextArea text10 = new JTextArea();



   JPanel jp1 = new JPanel();
   JPanel jp2 = new JPanel();
   JPanel jp3 = new JPanel();
   JPanel jp4 = new JPanel();
   JPanel jp5 = new JPanel();
   JPanel jp6 = new JPanel();
   JPanel jp7 = new JPanel();
   JPanel jp8 = new JPanel();
   JPanel jp9 = new JPanel();
   JPanel jp10 = new JPanel();

   jp1.add(text1);
   jp2.add(text2);
   jp3.add(text3);
   jp4.add(text4);
   jp5.add(text5);
   jp6.add(text6);
   jp7.add(text7);
   jp8.add(text8);
   jp9.add(text9);
   jp10.add(text10);

   cardPanel.add(jp1, "1");
   cardPanel.add(jp2, "2");
   cardPanel.add(jp3, "3");
   cardPanel.add(jp4, "4");
   cardPanel.add(jp5, "5");
   cardPanel.add(jp6, "6");
   cardPanel.add(jp7, "7");
   cardPanel.add(jp8, "8");
   cardPanel.add(jp9, "9");
   cardPanel.add(jp10, "10");

   setBorder(BorderFactory.createTitledBorder("Animat ion here"));
   setFont(mono);

   fc = new JFileChooser();
}


public void Next()
{
  if(currentCard < 10)
  {

     currentCard +=1;
     cl.show(cardPanel, "" + (currentCard));
  }


}

public void Previous()
{
  if(currentCard > 1)
  {
     currentCard -= 1;
     cl.show(cardPanel, "" + (currentCard));

  }
}

错误信息***

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at AsciiCanvas.Next(AsciiCanvas.java:108)

【问题讨论】:

    标签: java swing nullpointerexception awt cardlayout


    【解决方案1】:

    问题是cl 为空,原因如下:

    private CardLayout cl;
    
    ...
    
    public AsciiCanvas()
    {
        ...
        CardLayout cl = new CardLayout();
    

    在该构造函数中,您声明了一个新的局部变量,而不是为实例变量赋值。局部变量shadows隐藏实例变量。您的构造函数代码应该是:

    public AsciiCanvas()
    {
        ...
        cl = new CardLayout();
    

    (顺便说一句,我强烈建议使用String.valueOf(...) 而不是"" + ... 以空安全的方式将值转换为字符串。您真的对字符串连接,是吗?)

    【讨论】:

      【解决方案2】:

      这是构造函数中的局部变量,隐藏了成员cl

      CardLayout cl = new CardLayout();
      

      Next()Previous()方法中,意味着cl成员是null

      改为:

      cl = new CardLayout();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-09-07
        • 1970-01-01
        • 2019-12-04
        • 1970-01-01
        • 2014-04-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多