当前位置: 技术问答>java相关
◇简单问题,小妹在线等各位大哥帮助~~
来源: 互联网 发布时间:2015-10-13
本文导语: 这是一道Java的作业题.老师要求用Vector完成堆栈的功能. 我建了两个类,myStackText和myStack.myStackText类是主窗口,myStack实现堆栈的功能.编译能通过,但是点击主窗口中的按钮,却出现NullPointerException例外,怎么回事啊,搞不懂~~~!...
这是一道Java的作业题.老师要求用Vector完成堆栈的功能.
我建了两个类,myStackText和myStack.myStackText类是主窗口,myStack实现堆栈的功能.编译能通过,但是点击主窗口中的按钮,却出现NullPointerException例外,怎么回事啊,搞不懂~~~!
//myStackTest.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class myStackTest extends JFrame {
private JTextField InputField, OutputField;
private JButton Push, Pop, Display;
private Container c;
private FlowLayout layout;
private JLabel status, InLabel, OutLabel;
private myStack stackDemo;
public myStackTest()
{
super( "Stack Demo" );
layout = new FlowLayout();
c = getContentPane();
c.setLayout( layout );
status = new JLabel();
stackDemo = new myStack();
InLabel = new JLabel( "Input" );
c.add( InLabel );
InputField = new JTextField( 10 );
c.add( InputField );
OutLabel = new JLabel( "Output" );
c.add( OutLabel );
OutputField = new JTextField( 10 );
c.add( OutputField );
Push = new JButton( "Push" );
Push.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
stackDemo.push( InputField.getText() );
InputField.setText("");
}
}
);
c.add( Push );
Pop = new JButton( "Pop" );
Pop.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
try {
OutputField.setText( stackDemo.pop().toString() );
}
catch( ArrayIndexOutOfBoundsException exception ) {
status.setText( "Stack is empty." );
}
}
}
);
c.add( Pop );
Display = new JButton( "Display" );
Display.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
stackDemo.display();
}
}
);
c.add( Display );
c.add( status );
setSize( 200, 200 );
show();
}
public static void main( String args[] )
{
System.out.println( "Starting myStackTest..." );
myStackTest app = new myStackTest();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
class myStack {
int point;
private Vector stack;
public void myStack()
{
stack = new Vector();
point = 0;
}
public void push( Object item)
{
stack.addElement( item );
point++;
}
public Object pop()
{
point--;
Object obj = stack.elementAt( point );
stack.remove( point );
return obj;
}
public void display()
{
JOptionPane.showConfirmDialog( null, stack.toString(),
"Display", JOptionPane.PLAIN_MESSAGE );
}
}
我建了两个类,myStackText和myStack.myStackText类是主窗口,myStack实现堆栈的功能.编译能通过,但是点击主窗口中的按钮,却出现NullPointerException例外,怎么回事啊,搞不懂~~~!
//myStackTest.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class myStackTest extends JFrame {
private JTextField InputField, OutputField;
private JButton Push, Pop, Display;
private Container c;
private FlowLayout layout;
private JLabel status, InLabel, OutLabel;
private myStack stackDemo;
public myStackTest()
{
super( "Stack Demo" );
layout = new FlowLayout();
c = getContentPane();
c.setLayout( layout );
status = new JLabel();
stackDemo = new myStack();
InLabel = new JLabel( "Input" );
c.add( InLabel );
InputField = new JTextField( 10 );
c.add( InputField );
OutLabel = new JLabel( "Output" );
c.add( OutLabel );
OutputField = new JTextField( 10 );
c.add( OutputField );
Push = new JButton( "Push" );
Push.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
stackDemo.push( InputField.getText() );
InputField.setText("");
}
}
);
c.add( Push );
Pop = new JButton( "Pop" );
Pop.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
try {
OutputField.setText( stackDemo.pop().toString() );
}
catch( ArrayIndexOutOfBoundsException exception ) {
status.setText( "Stack is empty." );
}
}
}
);
c.add( Pop );
Display = new JButton( "Display" );
Display.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
stackDemo.display();
}
}
);
c.add( Display );
c.add( status );
setSize( 200, 200 );
show();
}
public static void main( String args[] )
{
System.out.println( "Starting myStackTest..." );
myStackTest app = new myStackTest();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
class myStack {
int point;
private Vector stack;
public void myStack()
{
stack = new Vector();
point = 0;
}
public void push( Object item)
{
stack.addElement( item );
point++;
}
public Object pop()
{
point--;
Object obj = stack.elementAt( point );
stack.remove( point );
return obj;
}
public void display()
{
JOptionPane.showConfirmDialog( null, stack.toString(),
"Display", JOptionPane.PLAIN_MESSAGE );
}
}
|
构造器不能有返回类型,void也不行,将
public void myStack()
{
stack = new Vector();
point = 0;
}
改为以下形式即可:
public myStack()
{
stack = new Vector();
point = 0;
}
public void myStack()
{
stack = new Vector();
point = 0;
}
改为以下形式即可:
public myStack()
{
stack = new Vector();
point = 0;
}
|
buick555(王飞) 老兄你好:
我认为他的程序有几个地方判断不完全,比如没有push就pop了,当然得到null
如果push了一个,却pop>1也会得到null
小姐你点了那个按钮出现异常?push pop or display?
请把运行堆栈贴出来吧!
我认为他的程序有几个地方判断不完全,比如没有push就pop了,当然得到null
如果push了一个,却pop>1也会得到null
小姐你点了那个按钮出现异常?push pop or display?
请把运行堆栈贴出来吧!