-1

我的完整代码将编译,但是当我运行它时,我收到下面的错误说明 NullPointerException。我还包括了错误所指的代码部分,但我不知道如何使链表在第一个条目时不具有空值。请告知 - 错误行 83 指的是以下行: if(scores.isEmpty()) 大多数错误是针对未知来源的,我不知道如何追踪。

错误:

*Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
      *at TopTenList$enterButtonListener.actionPerformed(TopTenList.java:83)
      *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.Window.dispatchEventImpl(Unknown Source)
      *at java.awt.Component.dispatchEvent(Unknown Source)
      *at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
      *at java.awt.EventQueue.access$200(Unknown Source)
      *at java.awt.EventQueue$3.run(Unknown Source)
      *at java.awt.EventQueue$3.run(Unknown Source)
      *at java.security.AccessController.doPrivileged(Native Method)
      *at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
      *at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
      *at java.awt.EventQueue$4.run(Unknown Source)
      *at java.awt.EventQueue$4.run(Unknown Source)
      *at java.security.AccessController.doPrivileged(Native Method)
      *at java.security.ProtectionDomain$1.doIntersectionPrivilege(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)

代码:

private class enterButtonListener implements ActionListener
{

    public void actionPerformed(ActionEvent e)
    {
        String name = " ";
        Integer score = 0;
        String newScore = name + " "+score.toString();

        if(scores.isEmpty())
        {
            scores.add(newScore);
            return;
        }
        for (int i=0; i<=scores.size(); i++)
        {
            if(i==scores.size())
            {
                scores.add(newScore);
                break;
            }
            if (isOnList(newScore, scores.get(i)))
            {
                scores.add(i,newScore);
                break;
            }
            // Shrink the list to the top ten scores
            while (scores.size()>10)
            {
                scores.remove(10);
            }
        }
    }
}

这是我的完整代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JList;
import java.util.*;
import java.util.Scanner;
import java.util.LinkedList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class TopTenList extends JFrame
{
    private TopTenList tt;
    private JTextArea listView;
    private JTextField name;
    private JTextField score;
    private LinkedList<String> scores;
    private JButton enterButton;


    // This is the code for the GUI Window
    public TopTenList()
    {
        listView = new JTextArea();
        name = new JTextField();
        score = new JTextField();

        // Put the textArea in the center of the frame
        add(listView);
        listView.setEditable(false);
        listView.setBackground(Color.WHITE);


        //Create panel and label for the Name and score text fields
        JPanel namePanel = new JPanel(new GridLayout(2,2));
        namePanel.add(new JLabel ("Enter User Name: "));
        namePanel.add(name);
        namePanel.add(new JLabel ("Enter New Score: "));
        namePanel.add(score);
        add(namePanel, BorderLayout.NORTH);

        //Create Enter score button
        enterButton = new JButton ("Enter");
        add(enterButton, BorderLayout.SOUTH);

        //Add action listener to the button
        enterButton.addActionListener(new enterButtonListener());


        // Set up the frame
        setTitle("Top Ten Scoreholders");  // Window Title
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Behavior on close
        pack();
        setVisible(true);  // Display the window

    }

    // Create the Linked List
    public void TopTenList()
    {
        scores = new LinkedList<String>();
    }

    // Populate the list
    private class enterButtonListener implements ActionListener
    {

        public void actionPerformed(ActionEvent e)
        {
            String name="";
            Integer score=0;
            String newScore = name + " "+score.toString();

            if(scores.isEmpty())
            {
                scores.add(newScore);
                return;
            }
            for (int i=0; i<=scores.size(); i++)
            {
                if(i==scores.size())
                {
                    scores.add(newScore);
                    break;
                }
                if (isOnList(newScore, scores.get(i)))
                {
                    scores.add(i,newScore);
                    break;
                }
                // Shrink the list to the top ten scores
                while (scores.size()>10)
                {
                    scores.remove(10);
                }

            }
        }
    }

    // method to evaluate placement on score list

    public boolean isOnList (String first, String second)
    {
        Integer firstScore = Integer.parseInt(first.substring(first.lastIndexOf(' ')+1));
        Integer secondScore = Integer.parseInt(second.substring(second.lastIndexOf(' ')+1));
        return firstScore > secondScore;
    }

    // make the list for display
    public String toString()
    {
        String scoreList = "";
        for (int i = 0; i <scores.size(); i++)
        {
            scoreList = scoreList + scores.get(i)+"\n";
        }
        return scoreList;
    }

    public static void main(String [ ] args)
    {
        new TopTenList();
    }
}
4

2 回答 2

1

好吧,在我看来,要解决空指针异常,您必须在上面某处声明它的地方初始化 LinkedList。

LinkedList<Type> scores = new LinkedList<Type>();

(类型是您存储在 LinkedList 中的数据的类型)

像这样声明 LinkedList 将创建一个新的空 LinkedList 对象,该对象可以存储您想要的数据,并且会随着您向其中添加元素而动态增长。

您可以在此链接上阅读有关 LinkedList 的更多信息:http ://www.dreamincode.net/forums/topic/143089-linked-list-tutorial/

于 2014-04-30T02:33:37.773 回答
1

这不是构造函数,而是构造函数:

public void TopTenList()
{
  scores = new LinkedList<String>();
}

为什么是“伪”?因为构造函数没有声明返回类型,不是 void,也不是什么都没有。由于这个错误,您认为在 TopTenList 构造函数中正在初始化分数,但实际上并非如此,因为同样没有 TopTenList 构造函数。

这是一个构造函数:

// no void return type here, so this is a constructor
public TopTenList() 
{
  scores = new LinkedList<String>();
}

使用它,您的 LinkedList 分数将被初始化。或者你可以在你声明它的地方初始化它:

private LinkedList<String> scores = new LinkedList<String();
于 2014-04-30T02:46:43.190 回答