My complete code will compile but when I run it I get the errors below stating with the NullPointerException. I have also included the section of code that the error refers to but I have no idea how to make the linked list not have a null when this would be the first entry. Please advise - the error line 83 refers to the line: if(scores.isEmpty()) most of the errors are for an Unknown Source and I have no idea how to track that down.
Errors:
*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)
Code:
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);
}
}
}
}
Here is my complete code:
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();
}
}