我的完整代码将编译但是当我运行它时,我得到以下错误,说明NullPointerException。我还包括错误引用的代码部分,但我不知道如何将链接列表作为第一个条目时没有null。请指教 - 错误行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();
}
}
答案 0 :(得分:1)
嗯,在我看来,要解决空指针异常,你必须初始化在上面某处声明它的LinkedList。
LinkedList<Type> scores = new LinkedList<Type>();
(类型是您在LinkedList中存储的数据的类型)
像这样声明LinkedList将创建一个新的空LinkedList对象,该对象可以存储您想要的数据,并且在向其添加元素时会大小增大。
您可以在此处阅读此链接中有关LinkedList的更多信息:http://www.dreamincode.net/forums/topic/143089-linked-list-tutorial/
答案 1 :(得分: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();