The form consist of three textfields and two buttons(add and show buttons). User enters the candidate first name and last name and the respective number of votes received in textfields.On clicking the add button the candidate object is created and the candidate object is added to the arraylist object. On clicking the show button gives the actual size of the arraylist which is not happening.


Candidates.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author hp
 */
//import java.io.*;

public class Candidates {
    String firstname;
    String lastname;
    int votesreceived;

   
    public Candidates(String fname,String lname,int votes)
    {
    this.firstname=fname;
    this.lastname=lname;
    this.votesreceived=votes;
    }

    public String getFirstName()
    {
    return firstname;
    }
    
    public String getLastName()
    {
    return lastname;
    
    }
    
    public int getVotesReceived()
    {
    return votesreceived;
    }

}

VotesForm.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
 {
        // TODO add your handling code here:
        String fname=firstname.getText();
        String lname=lastname.getText();
        int votes=Integer.parseInt(votesreceived.getText());

        //System.out.println("Checking the voting system");
        System.out.println("Creating the candidate object");

        CandidateList canlist=new CandidateList();
        canlist.addCandidates(fname,lname,votes);

        firstname.setText("");
        lastname.setText("");
        votesreceived.setText("");

        
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    CandidateList canshow=new CandidateList();
    //canshow.showCandidates();
    //canshow.test();
    canshow.showCandidates();
    }

CandidateList.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author hp
 */
import java.util.*;
import java.util.ArrayList.*;

public class CandidateList {

    //instance variables
    
    //private List canlist;

    ArrayList canlist;
    public CandidateList()
    {
   // canlist=new LinkedList();
     canlist=new ArrayList();
    }

    //adding the candidates in the list
   public void addCandidates(String fname,String lname,int votes)
   {
   Candidates can=new Candidates(fname,lname,votes);

   canlist.add(can);
   
      System.out.println("candidates are added to the arraylist");
      System.out.println(canlist.size());
      
   }

 
   public void showCandidates()
   {

    int i=canlist.size();
    
    //gives the size of the arraylist
    System.out.println(i);
       
       // for(int i=0;i=canlist.size();i++)
        //  {
    
          //}
       //System.out.println(canlist);
       
     

   }
}

Edited 6 Years Ago by peter_budo: Please do not use CODE SNIPPED option to post your questions. Use FORUM THREAD

In the handler for the second button you create a brand new candidate list and show that. Maybe you should show the existing one onstead?
Similarly, in the first button handler you create a new list every time the button is pressed.
You need to create one candidate list when the gui is opened (or the first time the add button is pressed) and use that for all subsequent operations.
ps: Calling the first class "Candidates" is very confusing - it should be "Candidate" otherwize it looks like this is the list, not a single candidate.

CandidateList must be global. Every time you click something you create a new one, which is, of course, empty.

Making the list instance varaible global of the CandidateList class works. Now, I am trying to display the information of the list variable on the JTable using the DefaultTableModel on clicking the show button.But, when I try to to do so I am getting

NullPointerException at 
  at candidateinfo.VotersTableList.setTheCandidateTable(VotersTableList.java:90)
        at candidateinfo.CandidateList.showList(CandidateList.java:60)
        at gui.VotesForm.showJButton2ActionPerformed(VotesForm.java:154)
        at gui.VotesForm.access$200(VotesForm.java:22)
        at gui.VotesForm$3.actionPerformed(VotesForm.java:77)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6134)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
        at java.awt.Component.processEvent(Component.java:5899)
        at java.awt.Container.processEvent(Container.java:2023)
        at java.awt.Component.dispatchEventImpl(Component.java:4501)
        at java.awt.Container.dispatchEventImpl(Container.java:2081)
        at java.awt.Component.dispatchEvent(Component.java:4331)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)
        at java.awt.Container.dispatchEventImpl(Container.java:2067)
        at java.awt.Window.dispatchEventImpl(Window.java:2458)
        at java.awt.Component.dispatchEvent(Component.java:4331)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

I have created VotersTableList to display the data in the JTable.

VotersTableList.java


package candidateinfo;

import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.awt.BorderLayout;



public class VotersTableList
{


    private JTable table=null;
    private JFrame frame=new JFrame("VotersTableList");

    private class VotersList extends DefaultTableModel
    {

        private List list=null;

        public VotersList(List list)
        {
        this.list=list;
        }


        @Override
        public int getColumnCount()
        {
        return 3;

        }

        @Override
        public int getRowCount()
        {

            return list.size();
        }

        @Override
        public Object getValueAt(int row,int column)
        {
            if(row>=list.size())
            {
            return null;
            }

            Candidate canvalues=(Candidate) list.get(row);

            if(column==0)
            {
                return canvalues.getFirstName();
            }

            else if(column==1)
            {

                return canvalues.getLastName();
            }

            else if(column==2)
            {

                return canvalues.getVotesReceived();
            }
            else
            {
             return null;
            }
        }




    }

    public void setTheCandidateTable() throws Exception
    {
           table.setModel(new VotersList(CandidateList.canlist));
           frame.getContentPane().add(table,BorderLayout.CENTER);
           frame.setSize(200, 200);
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           frame.setVisible(true);


    }




 }

I am not finding the solution to this exception.
Any help will be appreciated.

Edited 3 Years Ago by Nick Evan: Fixed formatting

NullPointerException at
at candidateinfo.VotersTableList.setTheCandidateTable(VotersTableList.java:90)
at candidateinfo.CandidateList.showList(CandidateList.java:60)
at gui.VotesForm.showJButton2ActionPerformed(VotesForm.java:154)
at gui.VotesForm.access$200(VotesForm.java:22)
at gui.VotesForm$3.actionPerformed(VotesForm.java:77)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6134)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5899)
at java.awt.Container.processEvent(Container.java:2023)
at java.awt.Component.dispatchEventImpl(Component.java:4501)
at java.awt.Container.dispatchEventImpl(Container.java:2081)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)
at java.awt.Container.dispatchEventImpl(Container.java:2067)
at java.awt.Window.dispatchEventImpl(Window.java:2458)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

The exception tells you at which line something is null.

This article has been dead for over six months. Start a new discussion instead.