当前位置: 代码迷 >> J2SE >> 空指针错误如何解决啊
  详细解决方案

空指针错误如何解决啊

热度:204   发布时间:2016-04-24 02:20:51.0
空指针异常怎么解决啊...
这是一个简单的窗体,但运行时不能显示内部窗体,并报空指针异常,这是怎么回事啊...求指教...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class swing窗体
{ public static void main(String args[])
  { Mywindow win=new Mywindow();
  win.validate();
  }
}
class Mywindow extends JFrame
{ JButton button1,button2;
  JTextArea text;
  JScrollPane scroll;
  JInternalFrame interframe;
  JSplitPane splitOne,splitTwo;
  Mywindow()
  { setSize(300,300);
  setVisible(true);
  Container con=getContentPane();
  con.setLayout(new GridLayout(1,2));
  button1=new JButton("button1");
  button2=new JButton("button2");
  text=new JTextArea(6,12);
  scroll=new JScrollPane(text);
  splitOne=new JSplitPane(JSplitPane.VERTICAL_SPLIT,true,button1,button2);
  splitTwo=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,splitOne,scroll);
  interframe.setSize(200,200);
  interframe.setVisible(true);
  Container interCon=interframe.getContentPane();
  interCon.setLayout(new FlowLayout());
  interCon.add(splitTwo);
  JDesktopPane desk=new JDesktopPane();
  desk.add(interframe);
  getContentPane().add(desk);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

------解决方案--------------------
Java code
Mywindow() {        setSize(300, 300);        setVisible(true);        Container con = getContentPane();        con.setLayout(new GridLayout(1, 2));        button1 = new JButton("button1");        button2 = new JButton("button2");        text = new JTextArea(6, 12);        scroll = new JScrollPane(text);        splitOne = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, button1,                button2);        splitTwo = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, splitOne,                scroll);        interframe = new JInternalFrame("Remember to init");   《======= 这里啊        interframe.setSize(200, 200);        interframe.setVisible(true);        Container interCon = interframe.getContentPane();        interCon.setLayout(new FlowLayout());        interCon.add(splitTwo);        JDesktopPane desk = new JDesktopPane();        desk.add(interframe);        getContentPane().add(desk);        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }
------解决方案--------------------
interframe没有new - - 2L已经正解了 就不贴代码了
------解决方案--------------------
Java code
import java.awt.Container;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.GridLayout;import javax.swing.JButton;import javax.swing.JDesktopPane;import javax.swing.JFrame;import javax.swing.JInternalFrame;import javax.swing.JScrollPane;import javax.swing.JSplitPane;import javax.swing.JTextArea;public class swingFrame {    public static void main(String args[]) {        Mywindow win = new Mywindow();        win.validate();    }}class Mywindow extends JFrame {    JButton button1, button2;    JTextArea text;    JScrollPane scroll;    JInternalFrame interframe = new JInternalFrame();//没有初始化 对三楼无话可说了    JSplitPane splitOne, splitTwo;    Mywindow() {        setSize(300, 300);        setVisible(true);        Container con = getContentPane();        con.setLayout(new GridLayout(1, 2));        button1 = new JButton("button1");        button2 = new JButton("button2");        text = new JTextArea(6, 12);        scroll = new JScrollPane(text);        splitOne = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, button1,                button2);        splitTwo = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, splitOne,                scroll);                interframe.setSize(new Dimension(200, 200));        interframe.setVisible(true);        Container interCon = interframe.getContentPane();        interCon.setLayout(new FlowLayout());        interCon.add(splitTwo);        JDesktopPane desk = new JDesktopPane();        desk.add(interframe);        getContentPane().add(desk);        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }}
  相关解决方案