好吧  
是一个小程序中出的问题 希望高手帮忙分析下问题出在那里
不要一上来 就把代码改好了给我 
这样我还是没有把问题该清楚 
恩恩 
下面是我的代码
这一段是一个小型界面的程序package jre;
import java.awt.*;
import java.awt.Event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.*;
public class TestDemo1 extends JFrame{
JPanel panel;
JButton button1,button2;
JTextField text1,text2;
JTextArea text3;
   public TestDemo1(){
   super("test");
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   panel = new JPanel();
   getContentPane().add(panel);
   button1 = new JButton("录入");
   button2 = new JButton("查询");
   text1 = new JTextField(10);
   text2 = new JTextField(10);
   text3 = new JTextArea(1,15);
       ButtonAction buttonAction1 = new ButtonAction();
   button1.addActionListener(buttonAction1);
   button2.addActionListener(buttonAction1);
   panel.add(button1);
   panel.add(text1);
   panel.add(button2);
   panel.add(text2);
   panel.add(text3);
   setSize(200,150);
   setVisible(true);
   }
   class ButtonAction implements ActionListener{
   MyLinkedList list1 = new MyLinkedList();
   public void actionPerformed(ActionEvent e){
   if(e.getSource() == button1){
     list1.add(list1.size(),text1.getText());
     text3.setText((String)list1.toString());
   }
   if(e.getSource() == button2){
 int n = list1.getSection(text2.getText());//问题就出在这段代码上,不知道错在那里
             text3.setText((String)list1.getElement(n));
   }
   }
   }
   public static void main(String[] args){
   new TestDemo1();
   }
}

解决方案 »

  1.   

    地方太小 
    下面是MyLinkedList()函数的代码package jre;
    public class MyLinkedList extends MyAbstractList{
      private Node first,last;
      public MyLinkedList(){}
      public MyLinkedList(Object[] objects){
      super(objects);
      }
      public Object getFirst(){
      if(size ==0)
      return null;
      else
      return first.element;
      }
      public Object getLast(){
      if(size == 0)
      return null;
      else
      return last.element;
      }
      public void addFirst(Object o){
      Node newNode = new Node(o);
      newNode.next = first;
      first = newNode;
      size++;
      if(last == null)
      last = first;
      }
      public void addLast(Object o){
      if(last == null){
      last = first = new Node(o);
      }
      else{
      last.next = new Node(o);
      last = last.next;
      }
      size++;
      }
      public void add(int index,Object o){
      if(index == 0)
      addFirst(o);
      else if(index >= size)
      addLast(o);
      else{
      Node current = first;
      for(int i = 1;i<index;i++)
      current = current.next;
      Node temp = current.next;
      current.next = new Node(o);
      (current.next).next = temp;
      size++;
      }
      }
      public Object removeFirst(){
      if(size == 0)
      return null;
      else{
      Node temp = first;
      first = first.next;
      size--;
      if(first == null) 
      last = null;
      return temp.element;
      }
      }
      public Object removeLast(){
      return null;
      }
      public Object remove(int index){
      if(index < 0 || index >= size)
      return null;
      else if(index == 0)
      return removeFirst();
      else if(index == index - 1)
      return removeLast();
      else{
      Node previous = first;
      for(int i = 0;i < index;i++)
      previous = previous.next;
      Node current = previous.next;
      previous.next = current.next;
      size--;
      return current.element;
      }
      }
      public int size(){
      return size;
      }
      public int getSection(String s){
     int n = -1;
     Node current = first;
         while(n<size){
          if(current.element == s){
          n++;
          break;
          }
          else{
          current = current.next;
          n++;
          }
         }
         return n;
      }
      public void setElement(int n,Object o){
      Node current = first;
      for(int i = 0;i<n;i++)
      current = current.next;
      current.element = o;
      }
      public Object getElement(int n){
      Node current = first;
      for(int i = 0;i<n;i++)
      current = current.next;
      return current.element;
      }
      public String toString(){
      StringBuffer result = new StringBuffer("[");
      Node current = first;
      for(int i = 0;i < size;i++){
      result.append(current.element);
      current = current.next;
      if(current != null)
      result.append(". ");
      else
      result.append("]");
      }
      return result.toString();
      }
      public void clear(){
      first = last = null;
      }
      public boolean contains(Object o){
      return true;
      }
      public Object get(int index){
      return null;
      }
      public int indexOf(Object o){
      return 0;
      }
      public int lastIndexOf(Object o){
      return 0;
      }
      public Object set(int index,Object o){
      return null;
      }
      private static class Node{
      Object element;
      Node next;
      public Node(Object o){
      element = o;
      }
      }
    public Object getElement(Object o) {
    // TODO 自动生成方法存根
    return null;
    }
    public void setElement(Object o) {
    // TODO 自动生成方法存根

    }
    }
      

  2.   

    还有MyAbstractList()函数和接口的代码package jre;public abstract class MyAbstractList implements MyList {
        protected int size = 0;
        protected MyAbstractList(){
        
        }
        protected MyAbstractList(Object[] objects){
         for(int i = 0;i<objects.length;i++)
         this.add(objects[i]);
        }
        public void add(Object o){
         add(size,o);
        }
        public boolean isEmpty(){
         return size == 0;
        }
        public int size(){
         return size;
        }
        public boolean remove(Object o){
         if(indexOf(o) >= 0){
         remove(indexOf(o));
         return true;
        }
         else
         return false;
        }
    }package jre;public interface MyList {
       public void add(Object o);
       public void add(int index,Object o);
       public void clear();
       public boolean contains(Object o);
       public Object get(int index);
       public int indexOf(Object o);
       public boolean isEmpty();
       public int lastIndexOf(Object o);
       public boolean remove(Object o);
       public Object remove(int index);
       public Object set(int index,Object o);
       public int size();
       public int getSection(String o);
       public Object getElement(Object o);
       public void setElement(Object o);
    }
      

  3.   

    问下 你的MyLinkedList   是什么东东  ,他的代码呢?
      

  4.   

    虽然不知道你要做什么功能,但是我把你的错误找出来了,应该很好找的,只是你自己不仔细罢了
    public int getSection(String s)
    {
    int n = -1;
    Node current = first;
    while (n < size)
    {
    if (current.element.equals(s))//这个地方比较可不能用==了,以后认真理解==代表的意思了
    {
    n++;
    break;
    }
    else
    {
    current = current.next;
    n++;
    }
    }
    return n;
    }
      

  5.   

    的确==与equals的区别 楼主要去看一下
      

  6.   

    错误不止一处:public int getSection(String s){
    int n = -1;
    Node current = first;
      while(n<size){//n从-1开始,因此循环次数比size多一次,
      if(current.element == s){//==换为equals
      n++;
      ......因此若ls中不包括查询的字符串,current = current.next之后成为null,
    运行到最后一次current.element出nullPointException,
    将while(n<size)改为size-1试试吧
      

  7.   

    如果希望当查询不存在时返回一个比size大1的数,那么
    if(current.element == s)
    应换为if(null!=current&&null!=current.element&&current.element.equals(s))getElement()中也应增加n是否>size的判定