Java-线程中的异常;AWT-EventQueue-0“;java.lang.NullPointerException

Java-线程中的异常;AWT-EventQueue-0“;java.lang.NullPointerException,java,swing,nullpointerexception,actionlistener,actionevent,Java,Swing,Nullpointerexception,Actionlistener,Actionevent,我的完整代码将被编译,但当我运行它时,我会得到下面的错误,并使用NullPointerException声明。我还包括了错误所指的代码部分,但我不知道如何使链表在第一个条目时不具有空值。请注意-错误行83指的是:if(scores.isEmpty())大多数错误都是未知来源的,我不知道如何追踪 错误: *Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException *at TopTenList$enterB

我的完整代码将被编译,但当我运行它时,我会得到下面的错误,并使用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)
代码:

私有类enterButtonListener实现ActionListener
{
已执行的公共无效操作(操作事件e)
{
字符串名称=”;
整型得分=0;
字符串newScore=name+“”+score.toString();
if(scores.isEmpty())
{
添加(新闻核心);
返回;
}
对于(int i=0;i10)
{
分数。删除(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();
    }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.JList;
导入java.util.*;
导入java.util.Scanner;
导入java.util.LinkedList;
导入java.awt.event.ActionListener;
导入java.awt.event.ActionEvent;
公共类TopTenList扩展了JFrame
{
私人TopTenList tt;
私有区域列表视图;
私有JTextField名称;
私人JTextField评分;
私人LinkedList分数;
私有JButton enterButton;
//这是GUI窗口的代码
公共TopTenList()
{
listView=新的JTextArea();
name=新的JTextField();
分数=新的JTextField();
//将文本区域放在框架的中心
添加(列表视图);
listView.setEditable(false);
listView.setBackground(颜色:白色);
//为“名称”和“分数”文本字段创建面板和标签
JPanel名称面板=新JPanel(新网格布局(2,2));
添加(新的JLabel(“输入用户名:”);
名称面板。添加(名称);
添加(新JLabel(“输入新分数”);
名称面板。添加(分数);
添加(名称面板,边框布局。北);
//创建输入分数按钮
enterButton=新的JButton(“回车”);
添加(enterButton,BorderLayout.SOUTH);
//将操作侦听器添加到按钮
addActionListener(新的enterButtonListener());
//架起架子
setTitle(“前十名得分者”);//窗口标题
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭时的行为
包装();
setVisible(true);//显示窗口
}
//创建链接列表
公共无效TopTenList()
{
分数=新的LinkedList();
}
//填充列表
私有类enterButtonListener实现ActionListener
{
已执行的公共无效操作(操作事件e)
{
字符串名称=”;
整型得分=0;
字符串newScore=name+“”+score.toString();
if(scores.isEmpty())
{
添加(新闻核心);
返回;
}
对于(int i=0;i10)
{
分数。删除(10);
}
}
}
}
//评估分数表上的位置的方法
公共布尔值isOnList(字符串第一,字符串第二)
{
整数firstScore=Integer.parseInt(first.substring(first.lastIndexOf(“”)+1));
Integer secondScore=Integer.parseInt(second.substring(second.lastIndexOf(“”)+1));
返回firstScore>secondScore;
}
//列出要显示的列表
公共字符串toString()
{
字符串scoreList=“”;

对于(inti=0;i我认为,要解决空指针异常,必须初始化LinkedList,在上面的某个地方声明它

LinkedList<Type> scores = new LinkedList<Type>();
LinkedList分数=新建LinkedList();
(Type是存储在LinkedList中的数据类型)

像这样声明LinkedList将创建一个新的空LinkedList对象,该对象可以存储所需的数据,并且随着向其中添加元素,其大小将逐渐增大


您可以在此链接上阅读有关LinkedList的更多信息:

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

public void TopTenList()
{
  scores = new LinkedList<String>();
}
// no void return type here, so this is a constructor
public TopTenList() 
{
  scores = new LinkedList<String>();
}
public void TopTenList()
{
分数=新的LinkedList();
}
为什么是“pseudo”?因为构造函数没有声明返回类型,不是void,不是nothing。因为这个错误,您认为分数在TopTenList构造函数中初始化,但事实上不是,因为同样没有TopTenList构造函数

这是一个构造函数:

public void TopTenList()
{
  scores = new LinkedList<String>();
}
// no void return type here, so this is a constructor
public TopTenList() 
{
  scores = new LinkedList<String>();
}
//这里没有void返回类型,所以这是一个构造函数
公共TopTenList()
{
分数=新的LinkedList();
}
使用此选项,您的LinkedList分数将被初始化。或者您可以在声明的位置初始化它:

private LinkedList<String> scores = new LinkedList<String();

private LinkedList scores=new LinkedList
scores
为空。请在您尚未向我们展示的代码中查看您的代码,以了解原因。可能存在重复的