我定义了一个文本区  JText text=new JText();
 public void actionPerformed(ActionEvent e)
  {
   if(e.getSource()==butn)
            N=Integer.parseInt(text.getText());
         p.setLayout(new GridLayout(N,N));
          for (int i=0; i<N; i++)
           for (int j=0; j<N; j++)
           p.add(cell[i][j] = new Cell());  }
然后在文本区里输入整数,在文本区的一旁,定义了一个按钮,输入整数后,按按钮,
定义个一个变量N就得到了整数。但运行时有错误,怎么回事啊

解决方案 »

  1.   


    try{
        N=Integer.parseInt(text.getText());
    }catch(Exception e){}
      

  2.   

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at TicTacToe.actionPerformed(TicTacToe.java:52)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(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.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(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)好多异常啊
      

  3.   

    at TicTacToe.actionPerformed(TicTacToe.java:52) 52行是这行: p.setLayout(new GridLayout(N,N));
      

  4.   

    p是啥?
    先确定下p是否为null
      

  5.   

    JPanel p = new JPanel();
    p是一个面板,前面定义的,这个程序编译没错,运行没错。运行时,我输入数值,按按钮,没反应。然后就有错了
      

  6.   

    是不是应该先判断text.getText()取到的值是否为null,再断断 N 是否为0,然后才是下面的这句呢?
    p.setLayout(new GridLayout(N,N));