-1

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();
    }
}
Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28
user3440353
  • 9
  • 2
  • 5

2 Answers2

1

Well, in my opinion, to solve the null pointer exception, you have to initialize the LinkedList where you declare it somewhere above.

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

(Type is the type of the data you are storing in the LinkedList)

Declaring the LinkedList like this will create a new empty LinkedList object that can store the data you want and will dinamically grow in size as you add elements to it.

You can read more information about the LinkedList on this link here: http://www.dreamincode.net/forums/topic/143089-linked-list-tutorial/

Valy
  • 573
  • 2
  • 12
1

This is not a constructor, but rather a pseudo constructor:

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

Why "pseudo"? Because constructors don't have return types declared, not void, not nothing. Because of this error, you think that scores is being initialized in the TopTenList constructor, but in fact it's not, since again, there is no TopTenList constructor.

THIS is a constructor:

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

Use this and your LinkedList scores will be initialized. Or you could just initialize it where you declare it:

private LinkedList<String> scores = new LinkedList<String();
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373